[RESOLVED] PHP keeps inserting twice too many rows in MYSQL database
Hi, guys.
I'm working on a section of code that inserts values from an HTML form into a MYSQL database. However, each time I run the code, two rows of identical data are inserted into the database.
Can anybody help me resolve this problem?
Here is the code mentioned above. Even running this code by itself, without using the html form first, has the same problem.
@mysql_select_db($database) or die("Unable to select database");
//Command the Server to Insert Values
$query="INSERT INTO webpage VALUES ('','$title','$meta','$header_1','$paragraph_1','$header_2','$paragraph_2','$author')";
mysql_query($query);
$id = mysql_insert_id();
//Check if Insertation of Values Was Successful
if(mysql_query($query)) {
echo "You have successfully saved a webpage with author $author and title $title. The ID for this webpage is $id.";
} else {
echo "There was a problem processing the information. Please contact us at support[@]thesiteguru.com.";
}
Now, INSERT commands do not return a result set, so I normally check
how many affected rows there were (mysql_affected_rows()) - for example:
PHP Code:
@mysql_query($query, $conn);
if(mysql_affected_rows($conn) == 1) {
//insert was successful
} else {
//insert failed
}
Whether INSERT queries generate a resultset or not doesn't matter, because mysql_query does not return a resultset, it returns a resource identifier or false, and a resource identifier will always evaluate true. Criterion's code is perfectly valid (not that there's anything wrong with checking affected_rows either, but I just wanted to clarify as the reasoning for doing so seemed to be confused).
The first rule of Tautology Club is the first rule of Tautology Club.
I didn't expect to get three comprehensive solutions and explanations in 12 hours!
criterion9, I shall probably use your excellent solution. I see that instead of saying, "IF query is true," you assign the result of the query to a variable and then ask if THAT is true.
I didn't realize that a query would run if you put it inside an IF statement!
tirna and Mindzai, thanks for the information on INSERT and mysql_affected_rows(). That last function is a great tool to know.
I'm quite new to PHP, (as you've probably noticed by now) so you'll probably be seeing more of me!
(By the way, criterion9, can I give the $result variable its value at the beginning of the entire page of code, or do I need to wait to give it its value until after I run the query?)
That really depends on your logical flow in the script...give it a shot and post back if you get stuck. I'm sure someone will be able to help you sort it out.
I thought that I'd be able to fix it with the information I got. But when I tried it again after fixing it, it still generates two identical rows of data every time I run this code!
@mysql_select_db($database) or die("Unable to select database");
//Command the Server to Insert Values
$query="INSERT INTO webpage VALUES ('','$title','$meta','$header_1','$paragraph_1','$header_2','$paragraph_2','$author')";
mysql_query($query);
$id = mysql_insert_id();
//Check if Insertation of Values Was Successful
$result = mysql_query($query);
if($result) {
echo "You have successfully saved a webpage with author $author and title $title. The ID for this webpage is $id.";
} else {
echo "There was a problem processing the information. Please contact us at support[@]thesiteguru.com.";
}
Bookmarks