Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Last Question, PHP Forms

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Last Question, PHP Forms

Postby Kenneth » Fri Aug 09, 2002 1:48 pm

Hi:

My name is Ken Cooper. This is my last question. Well, I have a form on my site. It inserts stuff into that database, and it selects it from the database. The page is - http://www.coopercentral.org/opinions.php
Well if I type something in, and submit it, I use PHP_SELF, and it displays the stuff. Well, right after I press submit, it displays the stuff, and if I press refresh, the same exact stuff submits it into the database. I use the ($empty) and the NULL and it still does it. Can anyone help me. Thanks a lot, I really appreciate it!!

Ken Cooper
Kenneth
New php-forum User
New php-forum User
 
Posts: 40
Joined: Fri Aug 09, 2002 11:53 am
Location: Rochester, NY

Postby Jay » Fri Aug 09, 2002 2:58 pm

When you've submitted a form, the page you are viewing is specific to the data you've submitted. It's not a standard page. Therefore, when you're refreshing it, in an attempt to get the same page up your browser is resubmitting the information!

There are two ways around this. Change the action of your page to a different page, and use this page to send you back to the form page and display the results (much like this forum works). In this way when you refresh the page you're not submitting variables again.

Or you could get your database to check whether or not the data has already been entered, and if not then it'll enter it.
Jay
 

Postby Kenneth » Fri Aug 09, 2002 3:08 pm

Jay, thanks! How would I do the second one you said? I like my pages using PHP_SELF all in one. Thanks

Ken Cooper
Kenneth
New php-forum User
New php-forum User
 
Posts: 40
Joined: Fri Aug 09, 2002 11:53 am
Location: Rochester, NY

Postby Jay » Fri Aug 09, 2002 5:02 pm

Kenneth wrote:Jay, thanks! How would I do the second one you said? I like my pages using PHP_SELF all in one. Thanks

Ken Cooper

Me too.

Do your records have certain ID numbers? Or certain characteristics that separate them? Worst come to worse just check if each field matches what you're about to insert. All you want to do is count how many rows match the data you're about to insert, if you find none then it's new so go ahead. If you find a row, then chances are it's been inserted and you don't insert. Simple :)
Jay
 

Postby Kenneth » Fri Aug 09, 2002 5:23 pm

Thanks a lot Jay, I really appreciate. But believe it or not, I'm only 14, and I'm a kid that is just learning PHP. If its not too much trouble, could you possibly make up some code so I could see an example. Like I was talking about the Average thing before, I got code for example. Yes, I do have id's to seperate themselves. Thanks a lot, I really appreciate it.

Ken Cooper
Kenneth
New php-forum User
New php-forum User
 
Posts: 40
Joined: Fri Aug 09, 2002 11:53 am
Location: Rochester, NY

Postby Jay » Sat Aug 10, 2002 12:21 am

Kenneth wrote:Thanks a lot Jay, I really appreciate. But believe it or not, I'm only 14, and I'm a kid that is just learning PHP. If its not too much trouble, could you possibly make up some code so I could see an example. Like I was talking about the Average thing before, I got code for example. Yes, I do have id's to seperate themselves. Thanks a lot, I really appreciate it.

Ken Cooper

Basically you want something along the lines of:
Code: Select all
<?
$query = "Select * from thisTable where Field=Value";
$result = query($query); //(I made my own function query which connects and executes the query, basically thats all it is)
if ($result) {
print "Results already in database";
} else {
$query = "Insert results into Table";
$inserted = query($query)
print ($inserted)? "Insert Successful":"Insert Failed";
}

That's basically it. You'll have to fill in all the gaps yourself though
Jay
 

Postby Kenneth » Sat Aug 10, 2002 11:59 am

Thanks a lot Jay, I really appreciate it. I shall try that. What I'm going to do is, well, I don't want to echo any message, I just want it to show nothing, just the HTML form. On my opinions page, if nothing is submitted, then it just shows the form, if something is submitted, it shows the results in the table. Thanks a lot, I really appreciate it!!!

Ken Cooper
Kenneth
New php-forum User
New php-forum User
 
Posts: 40
Joined: Fri Aug 09, 2002 11:53 am
Location: Rochester, NY

Postby Kenneth » Sat Aug 10, 2002 6:55 pm

Sorry, I tried it, and it still entered the same data. Is there any other way? Well one more thing, if its not to much to ask, if its still possible, when you do an if else statement, doesn't echo a message, because I just want it to show the form both ways. Is there a way? Thanks a lot, I really appreciate it!!

Ken Cooper
Kenneth
New php-forum User
New php-forum User
 
Posts: 40
Joined: Fri Aug 09, 2002 11:53 am
Location: Rochester, NY

Postby Jay » Sat Aug 10, 2002 11:01 pm

Kenneth wrote:Sorry, I tried it, and it still entered the same data. Is there any other way? Well one more thing, if its not to much to ask, if its still possible, when you do an if else statement, doesn't echo a message, because I just want it to show the form both ways. Is there a way? Thanks a lot, I really appreciate it!!

Ken Cooper

Your query checking whether or not the values have been inserted before is obviously flawed then. Check it carefully (and logically).

If you want to show the form both ways, leave the form outside the if statement (after it preferably).
Jay
 

Postby Kenneth » Sun Aug 11, 2002 8:04 am

I'm sorry Jay, I still couldn't get it to work. I don't know whats wrong. I did the entire code, and it inserted well, its just that when I refresh, the SAME THING keeps going in. I really don't know how it still does. I guess it's just not possible then, I don't know. But you don't have to take up your time to do this. Thanks a lot for your help, I really appreciate it!!!

Ken Cooper
Kenneth
New php-forum User
New php-forum User
 
Posts: 40
Joined: Fri Aug 09, 2002 11:53 am
Location: Rochester, NY

Postby DoppyNL » Sun Aug 11, 2002 8:38 am

try "splitting" you're php-file into 3 parts or pages:

1. the form (post data to part 2)
2. - the query wich enters the data into the page
- some text saying "data entered in database"
- a link to part 3 of you're file
3. display the list (the part you're refreshing)

how to do this??
could be something like this:
a variable called "subpage", if it's not set, display the form. if it is "post" proces the form and add the data into the database. and if the variable is "display" display you're list.

Greetz Daan
DoppyNL
 

Postby Kenneth » Sun Aug 11, 2002 11:21 am

Thanks I'll try that as well.
Kenneth
New php-forum User
New php-forum User
 
Posts: 40
Joined: Fri Aug 09, 2002 11:53 am
Location: Rochester, NY

Postby Jay » Mon Aug 12, 2002 2:30 am

What sort of data are you trying to insert, and what query are you using to check if the data has been inserted or not. Give me some details as to why you're checking that particular field or not!
Jay
 

Postby Kenneth » Mon Aug 12, 2002 12:07 pm

Alright thanks. Well, I don't really want this topic to get to be huge. Here's what I'm going to do. I'm going to give you the address of the page I want, and the code + tables I use. Experiment with it, and tell me an answer, if you come up with one. I really appreciate it!!!

address - http://www.coopercentral.org/opinions.php - Please go there, and just type in something. Press post!. Then press refresh, the same data goes in. I don't want that.

tables:

Topics - Fields: - id - Topic - Date

Opinions - Fields: - oid - id - Opinion - Date

The Code:

Code: Select all
<?

$connection = @mysql_connect("localhost", "cooperce_poll", "hermie") or die("Couldn't connect.");

$db = @mysql_select_db(cooperce_poll, $connection) or die("Couldn't select database.");

$sql = "SELECT *
   FROM topics
   ORDER BY id DESC
   ";

$result = @mysql_query($sql,$connection) or die("Couldn't execute query.");

$contact_list = "<ul>";
   
while ($row = mysql_fetch_array($result)) {
   $id5 = $row['id'];
   $topic5 = $row['topic'];
   $date5 = $row['date'];

   $contact_list .= "<li><a href=\"old_opinions.php?theID=$id5\">$topic5</a> -- $date5";
}

$contact_list .= "</ul>";


?>

<?
include("connect.php");
$sql2 = "SELECT * FROM topics ORDER BY id";
$query2 = mysql_query($sql2) or die("Cannot query the database.<br>
" . mysql_error());
while($result2 = mysql_fetch_array($query2)) {
$topic = stripslashes($result2["topic"]);
$tid = stripslashes($result2["id"]);
$date = stripslashes($result2["date"]);
$display_past = "<ul>";
$display_past .= "<li><a href=old_opinions.php?id=$tid>$topic</a> -- $date</li>";
$display_past .= "</ul>";
}

$sql_count = "SELECT count(oid) FROM opinions WHERE id = \"$tid\"";

$result_count = @mysql_query($sql_count) or die("Couldn't execute query.");
   
$count = mysql_result($result_count,0,"count(oid)");

?>
<p><a href="http://www.coopercentral.org"><img src="images/cclogo.gif" width="222" height="132"></a></p>
<body link="#0000FF" vlink="#800080" alink="#FF0000" text="#000000" bgcolor="#FFFFF0">
<table border=1>
<tr>
<td bgcolor="#C7DFFC" valign="top">
<?php
$title = "Give us your Opinion! - $count so far!";
$titlecolor = "800000";
$titlefont = "/home/cooperce/public_html/fonts/COMIC.ttf";
$titlesize = "24";
$rotation = "90";
echo "<IMG SRC=\"/includes/graftext.php?title=$title&titlecolor=$titlecolor&titlefont=$titlefont&titlesize=$titlesize&rotation=$rotation\" ALT=\"$title\">";
?><td valign="top">Write a <b><i>brief</i></b> opinion of the topic below.
<table border=1>
<tr>
<td bgcolor=#CCFFB1 align=center><font size=4><b><? echo $topic; ?></b></font>&nbsp;</td>
</tr>
<tr>
<td>
<form method=POST action=<?php echo $PHP_SELF; ?>>
<p><input type=text name=text size=100 maxlength=225><input type=submit value=Post!></p>
</form>
</td>
</tr><td>


<?php
include("connect.php");

$today = date("M j, Y, h:i");
if(!empty($text)) {
$text = addslashes($text);

$sql = "INSERT INTO opinions SET oid='', id='$tid', opinion='$text', date='$today'";
$query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());

$text=NULL;
$oid=NULL;
$id=NULL;
$date=NULL;
$today=NULL;
} else {
}
$sql3 = "SELECT * FROM opinions WHERE id = \"$tid\" ORDER BY oid DESC";
$query3 = mysql_query($sql3) or die("Cannot query the database.<br>" . mysql_error());
while($result3 = mysql_fetch_array($query3)) {
$opinion = stripslashes($result3["opinion"]);
$today5 = stripslashes($result3["date"]);
echo "
<font size=2>$opinion</font> <font size=1>-- <b>$today5</b><hr></font>
";
}
?>
</td>
</tr>
</hr>
</table>
</table>
<p></p>
<h2>Past Opinions</h2>
<p><?php echo "$contact_list"; ?></p>
<p></p>
<p align="right">&nbsp;</p>
<p align="right"><a href="http://www.coopercentral.org">Coopercentral.org</a>
2002<br>
Email me:&nbsp; <a href="mailto:Kenny@coopercentral.org">Kenneth L. Cooper III</a><br>
<a href="privacy.php">Coopercentral Privacy Policy</a></p>


Thats the code. PLease. like I said, just go to the page above, I show you, and just type in something. I have a weekly opinion, and just type something in. It shows, the press refresh, it shows the same thing. I don't want that. My friend told me to use the $variable=NULL; to empty out the variables, but that didn't work here. Can you find a solution? If you want, and if you have time, using a Personal Web server, just test that script, and find something that will make it not show it twice. Like I said, if someone types something in or not, I don't want to display any message. Thanks a lot, I really appreciate it!!!

Ken Cooper
Kenneth
New php-forum User
New php-forum User
 
Posts: 40
Joined: Fri Aug 09, 2002 11:53 am
Location: Rochester, NY

Postby Jay » Mon Aug 12, 2002 1:59 pm

2 ways around this. You can either count * from opinions where opinion='$text' and only insert if that resolves to 0, or set a session value. Sessions are not affected by refreshes, same with cookies
Jay
 

Postby Kenneth » Mon Aug 12, 2002 2:21 pm

I like the first one, where I count. Sorry, but could you possibly just give me an example? Don't go way out of your way, but your awesome at this stuff. Could you just give me a little example, I know what you mean, just general code of that. Thanks a lot, I really appreciate it!!

Ken Cooper
Kenneth
New php-forum User
New php-forum User
 
Posts: 40
Joined: Fri Aug 09, 2002 11:53 am
Location: Rochester, NY

Postby Jay » Tue Aug 13, 2002 3:15 am

Kenneth wrote:I like the first one, where I count. Sorry, but could you possibly just give me an example? Don't go way out of your way, but your awesome at this stuff. Could you just give me a little example, I know what you mean, just general code of that. Thanks a lot, I really appreciate it!!

Ken Cooper

I did give you an example, read between the bold tags!! That'll return how many rows contain that value, and you only need to insert your comment if it resolves to 0!
Jay
 

Postby Kenneth » Tue Aug 13, 2002 6:22 am

Ya, I know, what I mean is, the resolve the 0 part, or do I just put that code in, and it does it? I don't know, just learning. Thanks a lot, I really appreciate it.!!

Ken Cooper
Kenneth
New php-forum User
New php-forum User
 
Posts: 40
Joined: Fri Aug 09, 2002 11:53 am
Location: Rochester, NY

Postby Jay » Tue Aug 13, 2002 11:45 am

Kenneth wrote:Ya, I know, what I mean is, the resolve the 0 part, or do I just put that code in, and it does it? I don't know, just learning. Thanks a lot, I really appreciate it.!!

Ken Cooper

No, you need to check if there are any rows returned! If there are no rows returned, there's no records!! And how do you check the rows returned? I'll give you a clue mysql_num_rows()

Come on now, this is getting into basic stuff :wink:
Jay
 

Postby Kenneth » Tue Aug 13, 2002 11:46 am

Ya thanks, just wanted the function, now I can guess what to do know. Thanks for your time, and sorry for not knowing stuff.

Ken Cooper
Kenneth
New php-forum User
New php-forum User
 
Posts: 40
Joined: Fri Aug 09, 2002 11:53 am
Location: Rochester, NY


Return to PHP coding => General

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.