wikedawsum Posted January 13, 2007 Share Posted January 13, 2007 I'm trying to make a website to track all of the movies I have. I have a page that displays the movies by category with title and a description if I choose to put one. I'm having trouble with setting up the form to add a new movie. Here is the code I have so far:Form to add new movies:[code]<form name='add_movie' action='add_movie.php' method='post'><table width=250 cellpadding=2 cellspacing=0 bgcolor='#cccccc'><tr> <td><p>Title</p></td> <td><input type='text' name='title' size=30 maxlength=255></td></tr><tr> <td><p>Description</p></td> <td><input type='text' name='description' size=30 maxlength=255></td></tr><tr> <td><p>Category</p></td> <td><input type='text' name='category' size=30 maxlength=255></td></tr><tr> <td><input type='submit' value='Add Movie'></td></tr></table></form>[/code]and add_movie.php:[code]<?php$conn = mysql_connect("localhost", "brandi", "bc5106") or die($msg_no_connect);mysql_select_db("movies") or die(mysql_error()); $res = mysql_query("INSERT INTO dvd (title, description, category) VALUES ($_POST['title'], $_POST['description'], $_POST['category'];"); if (mysql_affected_rows($res) > 0) { echo 'Your movie was successfully added.' } else { echo 'This movie already exists..' }?>[/code]I want to be able to add the movie if it doesn't exist, or tell me that it's already there if the movie does exist. Can anyone help me with this code?Thank you!! Quote Link to comment https://forums.phpfreaks.com/topic/34065-help-with-insert-statement/ Share on other sites More sharing options...
Psycho Posted January 13, 2007 Share Posted January 13, 2007 <?php<?php$conn = mysql_connect("localhost", "brandi", "bc5106") or die($msg_no_connect);mysql_select_db("movies") or die(mysql_error());$sql = "SELECT * FROM dvd WHERE title = '{$_POST['title']}':;$res = mysql_query($sql);if (mysql_num_rows($res)) { echo 'This movie already exists..'} else { $sql = "INSERT INTO dvd (title, description, category) VALUES ($_POST['title'], $_POST['description'], $_POST['category'];" $res = mysql_query($sql); if (mysql_affected_rows($res) > 0) { echo 'Your movie was successfully added.' } else { echo 'The insert failed.' }}?>?> Quote Link to comment https://forums.phpfreaks.com/topic/34065-help-with-insert-statement/#findComment-160137 Share on other sites More sharing options...
wikedawsum Posted January 13, 2007 Author Share Posted January 13, 2007 Thanks for the help. All I got was a blank page after clicking submit. Quote Link to comment https://forums.phpfreaks.com/topic/34065-help-with-insert-statement/#findComment-160139 Share on other sites More sharing options...
wikedawsum Posted January 14, 2007 Author Share Posted January 14, 2007 I see what the code is supposed to be doing, I'm just not sure why it's not returning any information.. even an error would help! Anyone have any other suggestions?Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/34065-help-with-insert-statement/#findComment-160144 Share on other sites More sharing options...
Psycho Posted January 14, 2007 Share Posted January 14, 2007 Then add some debugging code. Does this produce any errors?[code]<?php$conn = mysql_connect("localhost", "brandi", "bc5106") or die($msg_no_connect);mysql_select_db("movies") or die(mysql_error());$sql = "SELECT * FROM dvd WHERE title = '".$_POST['title']"'";$res = mysql_query($sql) or die($sql."<br><br>".mysql_error());if (mysql_num_rows($res)) { echo "This movie already exists.";} else { $sql = "INSERT INTO dvd (title, description, category) VALUES ($_POST['title'], $_POST['description'], $_POST['category'];" $res = mysql_query($sql) of die($sql."<br><br>".mysql_error()); if (mysql_affected_rows($res) > 0) { echo "Your movie was successfully added."; } else { echo "The insert failed."; }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/34065-help-with-insert-statement/#findComment-160302 Share on other sites More sharing options...
Philip Posted January 14, 2007 Share Posted January 14, 2007 Taken from the code above (with debugging.) The problem was a semicolon (;) inside the "[code]<?php$conn = mysql_connect("localhost", "brandi", "bc5106") or die($msg_no_connect);mysql_select_db("movies") or die(mysql_error());$sql = "SELECT * FROM dvd WHERE title = '".$_POST['title']"'";$res = mysql_query($sql) or die($sql."<br><br>".mysql_error());if (mysql_num_rows($res)) { echo "This movie already exists.";} else { $sql = "INSERT INTO dvd (title, description, category) VALUES ($_POST['title'], $_POST['description'], $_POST['category']"; $res = mysql_query($sql) of die($sql."<br><br>".mysql_error()); if (mysql_affected_rows($res) > 0) { echo "Your movie was successfully added."; } else { echo "The insert failed."; }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/34065-help-with-insert-statement/#findComment-160347 Share on other sites More sharing options...
wikedawsum Posted January 14, 2007 Author Share Posted January 14, 2007 Thank you both for the help. Unfortunately I'm still getting a blank page.. >:( Quote Link to comment https://forums.phpfreaks.com/topic/34065-help-with-insert-statement/#findComment-160778 Share on other sites More sharing options...
Philip Posted January 14, 2007 Share Posted January 14, 2007 Hmm... okay, try this:[code]<?php$conn = mysql_connect("localhost", "brandi", "bc5106") or die(mysql_error());mysql_select_db("movies") or die(mysql_error());$sql = "SELECT * FROM dvd WHERE title = '".$_POST['title']"'";$res = mysql_query($sql) or die($sql."<br><br>".mysql_error());if (mysql_num_rows($res) > 0) { echo "This movie already exists.";} else { $sql = "INSERT INTO dvd (title, description, category) VALUES ($_POST['title'], $_POST['description'], $_POST['category']"; $res = mysql_query($sql) of die($sql."<br><br>".mysql_error()); if (mysql_affected_rows($res) > 0) { echo "Your movie was successfully added."; } else { echo "The insert failed."; }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/34065-help-with-insert-statement/#findComment-160821 Share on other sites More sharing options...
wikedawsum Posted January 15, 2007 Author Share Posted January 15, 2007 Still nothing.. very frustrating! Quote Link to comment https://forums.phpfreaks.com/topic/34065-help-with-insert-statement/#findComment-160846 Share on other sites More sharing options...
Philip Posted January 15, 2007 Share Posted January 15, 2007 I see it:THe line with this:$sql = "SELECT * FROM dvd WHERE title = '".$_POST['title']"'";Should be:$sql = "SELECT * FROM dvd WHERE title = '".$_POST['title']."'"; Quote Link to comment https://forums.phpfreaks.com/topic/34065-help-with-insert-statement/#findComment-160852 Share on other sites More sharing options...
wikedawsum Posted January 15, 2007 Author Share Posted January 15, 2007 Changed to correct code... still nothing! Quote Link to comment https://forums.phpfreaks.com/topic/34065-help-with-insert-statement/#findComment-160860 Share on other sites More sharing options...
Philip Posted January 15, 2007 Share Posted January 15, 2007 Also,$sql = "INSERT INTO dvd (title, description, category) VALUES ($_POST['title'], $_POST['description'], $_POST['category']";should be$sql = "INSERT INTO dvd (title, description, category) VALUES (".$_POST['title'].", ".$_POST['description'].", ".$_POST['category'].")"; Quote Link to comment https://forums.phpfreaks.com/topic/34065-help-with-insert-statement/#findComment-160863 Share on other sites More sharing options...
wikedawsum Posted January 15, 2007 Author Share Posted January 15, 2007 Finally got something back!INSERT INTO dvd (title, description, category) VALUES (Shall We Dance, , romance)You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'We Dance, , romance)' at line 1Now I just need to find where the error is. Thank you!! Quote Link to comment https://forums.phpfreaks.com/topic/34065-help-with-insert-statement/#findComment-160872 Share on other sites More sharing options...
Philip Posted January 15, 2007 Share Posted January 15, 2007 Try adding single quotes around itVALUES ('".$_POST['title']."', '".$_POST['description']."', '".$_POST['category']."') Quote Link to comment https://forums.phpfreaks.com/topic/34065-help-with-insert-statement/#findComment-160873 Share on other sites More sharing options...
wikedawsum Posted January 15, 2007 Author Share Posted January 15, 2007 Thank you, KingPhillip! I'm finally able to insert the movies into my db. One more quick question if you don't mind.. when I click add movie I keep getting "the insert failed", but when I got back to my main page that displays the movies, it shows that it is there. Any idea why that may be happening? It's this part of the code:[code]if (mysql_num_rows($res) > 0) { echo "This movie already exists.";} else { $sql = "INSERT INTO dvd (title, description, category) VALUES ('".$_POST['title']."', '".$_POST['description']."', '".$_POST['category']."')"; $res = mysql_query($sql) or die($sql."<br><br>".mysql_error()); if (mysql_affected_rows($res) > 0) { echo "Your movie was successfully added."; } else { echo "The insert failed."; }[/code]Thanks again for all your help. I have a better understanding of using insert statements now. :) Quote Link to comment https://forums.phpfreaks.com/topic/34065-help-with-insert-statement/#findComment-161172 Share on other sites More sharing options...
Philip Posted January 15, 2007 Share Posted January 15, 2007 Normally, I do [code]<?phpif(!$res) { echo "The insert failed!";} else { echo "The movie was added!";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/34065-help-with-insert-statement/#findComment-161183 Share on other sites More sharing options...
wikedawsum Posted January 15, 2007 Author Share Posted January 15, 2007 That worked perfectly!! Thank you!! Quote Link to comment https://forums.phpfreaks.com/topic/34065-help-with-insert-statement/#findComment-161189 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.