bobahad Posted December 10, 2007 Share Posted December 10, 2007 Hey guys, Why is this code double inserting in the table... I fill in the form, I click submit and it double inserts. $Link = mysql_connect($host,$user,$password); @mysql_select_db($DBname) or die( "Unable to select database"); $query="INSERT INTO Books (Title, Author , Publisher , ISBN , Year , Price) VALUES ($title,$author,$publisher,$isbn,$year,$price);"; if(mysql_db_query($DBname,$query,$Link)){ print("The query was successfully executed!<br>\n"); } else{ print("The query could not be executed!<br>\n"); } print($query); mysql_query($query); mysql_close($Link); Thanks Quote Link to comment Share on other sites More sharing options...
trq Posted December 10, 2007 Share Posted December 10, 2007 Remove... mysql_query($query); Quote Link to comment Share on other sites More sharing options...
bobahad Posted December 10, 2007 Author Share Posted December 10, 2007 ahh, awesome, thanks Was that enough to run the query ? if(mysql_db_query($DBname,$query,$Link)){ print("The query was successfully executed! \n"); } else{ print("The query could not be executed! \n"); } Quote Link to comment Share on other sites More sharing options...
revraz Posted December 10, 2007 Share Posted December 10, 2007 Did it insert the record? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 10, 2007 Share Posted December 10, 2007 mysql_db_query() sends the query, so yes...that is enough. Quote Link to comment Share on other sites More sharing options...
bobahad Posted December 10, 2007 Author Share Posted December 10, 2007 Can you guys please check it again, I don't know what I just did wrong. http://welovepluto.com/AddBook.php <BODY> <h1>Insert Book into Table</h1> <form name="form1" method="Post" action="AddBook.php"> <b>Title:</b><INPUT TYPE="text" NAME="title"><br> <b>Author:</b><INPUT TYPE="text" NAME="author"><br> <b>Publisher:</b><INPUT TYPE="text" NAME="publisher"><br> <b>ISBN:</b><INPUT TYPE="text" NAME="isbn"><br> <b>Year:</b><INPUT TYPE="text" NAME="year"><br> <b>Price:</b><INPUT TYPE="text" NAME="price"><br> <INPUT TYPE="submit" Value="Insert"><br><INPUT TYPE="reset"> </form> <?php //Set Variables for the database access: $host="***"; $user="***"; $password="***"; $DBname="***"; $TableName="Books"; $Link = mysql_connect($host,$user,$password); @mysql_select_db($DBname) or die( "Unable to select database"); $query="INSERT INTO Books (Title,Author,Publisher,ISBN,Year,Price) VALUES ($title,$author,$publisher,$isbn,$year,$price);"; if(mysql_db_query($DBname,$query,$Link)){ print("The book was added successfully!<br>\n"); } else{ print("The book was not added!<br>\n"); } print($query); mysql_close($Link); ?> </BODY> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 10, 2007 Share Posted December 10, 2007 Do this <BODY> <h1>Insert Book into Table</h1> <form name="form1" method="Post" action="AddBook.php"> Title:<INPUT TYPE="text" NAME="title"> Author:<INPUT TYPE="text" NAME="author"> Publisher:<INPUT TYPE="text" NAME="publisher"> ISBN:<INPUT TYPE="text" NAME="isbn"> Year:<INPUT TYPE="text" NAME="year"> Price:<INPUT TYPE="text" NAME="price"> <INPUT TYPE="submit" name="submit" Value="Insert"> <INPUT TYPE="reset"> </form> <?php //Set Variables for the database access: $host="***"; $user="***"; $password="***"; $DBname="***"; $TableName="Books"; $Link = mysql_connect($host,$user,$password); @mysql_select_db($DBname) or die( "Unable to select database"); if (isset($_POST['submit'])){ $query="INSERT INTO Books (Title,Author,Publisher,ISBN,Year,Price) VALUES ($title,$author,$publisher,$isbn,$year,$price);"; if(mysql_db_query($DBname,$query,$Link)){ print("The book was added successfully! \n"); } else{ print("The book was not added! \n"); } print($query); mysql_close($Link); } ?> </BODY> Quote Link to comment Share on other sites More sharing options...
bobahad Posted December 10, 2007 Author Share Posted December 10, 2007 oh awesome, now it won't show the book added successfully till after I click the button, that's cool Thanks man. The only problem now is that it only adds numbers even though all the fields are set to varchar(50)... weird. www.welovepluto.com/AddBook.php Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 10, 2007 Share Posted December 10, 2007 Try changing this line $query="INSERT INTO Books (Title,Author,Publisher,ISBN,Year,Price) VALUES ($title,$author,$publisher,$isbn,$year,$price);"; To $query="INSERT INTO Books (Title,Author,Publisher,ISBN,Year,Price) VALUES ('$title','$author','$publisher','$isbn','$year','$price')"; So it's adding numbers to the database instead of what you typed in? Quote Link to comment Share on other sites More sharing options...
bobahad Posted December 10, 2007 Author Share Posted December 10, 2007 oh no sorry I mis-explained it. If I type for example dwa dwad wad d 2d w it says the book was NOT added but if I type 12 313 41 4 41 51 The book gets added. I have all the fields set to Varchar in the database Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 10, 2007 Share Posted December 10, 2007 Hmmm...did you try changing what I told you to? Quote Link to comment Share on other sites More sharing options...
bobahad Posted December 10, 2007 Author Share Posted December 10, 2007 Yeah the single quotation fixed it, Thanks alot man Much appreciated. Just for knowledge purpose though the if (isset($_POST['submit'])) means that run this if statement if a button called 'submit' is clicked ? Does that mean I can create buttons called Edit, delete and so on and use that same isset ? Thanks again Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 10, 2007 Share Posted December 10, 2007 Yeah the single quotation fixed it, Thanks alot man Much appreciated. Just for knowledge purpose though the if (isset($_POST['submit'])) means that run this if statement if a button called 'submit' is clicked ? Does that mean I can create buttons called Edit, delete and so on and use that same isset ? Thanks again Yes, that is exactly what it does. You can use the same method for an edit button or whatever else =] Quote Link to comment Share on other sites More sharing options...
bobahad Posted December 10, 2007 Author Share Posted December 10, 2007 lol awesome, you just opened for me a world of possibilities. Thanks again and again and again Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 10, 2007 Share Posted December 10, 2007 No problem Don't forget to press "Topic Solved". Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.