Last Question, PHP Forms

A

Anonymous

Guest
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
 
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, thanks! How would I do the second one you said? I like my pages using PHP_SELF all in one. Thanks

Ken Cooper
 
Kenneth said:
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 :)
 
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 said:
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:
<?
$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
 
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
 
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 said:
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).
 
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
 
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
 
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!
 
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:
<?

$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> </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"> </p>
<p align="right"><a href="http://www.coopercentral.org">Coopercentral.org</a> 
2002<br>
Email me:  <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
 
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
 
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 said:
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!
 
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 said:
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:
 
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
 
Back
Top