silverglade Posted August 30, 2011 Share Posted August 30, 2011 Hi, I have a php form that I use to try to get matching data from the database that I put into the form. So if I enter date of birth 9-4-80 and first name Dave and lastname Smith. When I submit it the code should pull all of the matching terms out of the database and display. Now I get the following error when I submit the form. Query: Resource id #2 Failed with error: 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 'Resource id #2' at line 1 any help Greatly appreciated. thank you! here is the code <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); $host = " "; $database = " "; $username = " "; $password = " "; $tbl_name = "users"; $conn = mysql_connect($host, $username, $password) or die("Could not connect: " . mysql_error()); if($conn) { mysql_select_db($database); echo "connected to database!!"; } else { echo "failed to select database"; } //include('bouncer.php'); //$currentUser = $_SESSION['myusername']; if(isset($_POST['submit'])) { $first = mysql_real_escape_string( $_POST['first']); $last = mysql_real_escape_string( $_POST['last']); $dob = mysql_real_escape_string( $_POST['dob']); //THE SEARCH FUNCTION $sql = mysql_query ( "SELECT * FROM users WHERE firstname LIKE '%$first%' OR lastname LIKE '%$last%' OR dob LIKE '%$dob%' ") or die(mysql_error()); $result = mysql_query($sql) or die( "<br>Query: $sql<br>Failed with error: " . mysql_error() ); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } while ($row = mysql_fetch_assoc($result)) { echo $row["firstname"]; echo $row["lastname"]; echo $row["dob"]; } }//if(isset($_POST['submit'])) ?> <html> <body> <form action="login_success8.php" method="post"> <p> <input type="text" name="first" size="20" /> First name<br /> <input type="text" name="last" size="20" /> Last name<br /> <input name="dob" type="text" size="20" id="dob" /> Date of Birth<br /> <input type="submit" name="submit" value="Search" /> <input type="reset" value="Reset fields" /> </p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/246014-simple-php-form-not-working/ Share on other sites More sharing options...
kney Posted August 30, 2011 Share Posted August 30, 2011 change to: <?php $sql = mysql_query ("SELECT * FROM users WHERE firstname LIKE '%" . $first . "%' OR lastname LIKE '%" . $last . "%' OR dob LIKE '%" . $dob. "%' ") or die(mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/246014-simple-php-form-not-working/#findComment-1263425 Share on other sites More sharing options...
silverglade Posted August 30, 2011 Author Share Posted August 30, 2011 thank you. I get the same error. many sites use the LIKE "%$first%" format so I think that might be right. Quote Link to comment https://forums.phpfreaks.com/topic/246014-simple-php-form-not-working/#findComment-1263426 Share on other sites More sharing options...
kney Posted August 30, 2011 Share Posted August 30, 2011 You use mysql_query 2 times, that shouldn't work <?php //THE SEARCH FUNCTION $sql = mysql_query ( "SELECT * FROM users WHERE firstname LIKE '%$first%' OR lastname LIKE '%$last%' OR dob LIKE '%$dob%' ") or die(mysql_error()); $result = mysql_query($sql) or die( "<br>Query: $sql<br>Failed with error: " . mysql_error() ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/246014-simple-php-form-not-working/#findComment-1263433 Share on other sites More sharing options...
silverglade Posted August 30, 2011 Author Share Posted August 30, 2011 thank you for helping. should it be like this? $result = mysql_query ( "SELECT * FROM users WHERE firstname LIKE '%$first%' OR lastname LIKE '%$last%' OR dob LIKE '%$dob%' ") or die(mysql_error()); //I got rid of the bottom line and changed $sql to $result. Quote Link to comment https://forums.phpfreaks.com/topic/246014-simple-php-form-not-working/#findComment-1263436 Share on other sites More sharing options...
kney Posted August 30, 2011 Share Posted August 30, 2011 That should work better Quote Link to comment https://forums.phpfreaks.com/topic/246014-simple-php-form-not-working/#findComment-1263437 Share on other sites More sharing options...
silverglade Posted August 30, 2011 Author Share Posted August 30, 2011 thank you. The query is not supposed to run until I hit the form button, but I get the following error now. No rows found, nothing to print so am exiting Any more help I would appreciate it . thanks. here is the newcode <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); $host = " "; $database = " "; $username = " "; $password = " "; $tbl_name = "users"; $conn = mysql_connect($host, $username, $password) or die("Could not connect: " . mysql_error()); if($conn) { mysql_select_db($database); echo "connected to database!!"; } else { echo "failed to select database"; } //include('bouncer.php'); //$currentUser = $_SESSION['myusername']; if(isset($_POST['submit'])) { $first = mysql_real_escape_string( $_POST['first']); $last = mysql_real_escape_string( $_POST['last']); $dob = mysql_real_escape_string( $_POST['dob']); //THE SEARCH FUNCTION //$sql = mysql_query ( "SELECT * FROM users WHERE firstname LIKE '%$first%' //OR lastname LIKE '%$last%' OR dob LIKE '%$dob%'") or die(mysql_error()); $result = mysql_query ("SELECT * FROM users WHERE firstname LIKE '%" . $first . "%' OR lastname LIKE '%" . $last . "%' OR dob LIKE '%" . $dob. "%' ") or die(mysql_error()); //$result = mysql_query($sql) or die( "<br>Query: $sql<br>Failed with error: " . mysql_error() ); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } while ($row = mysql_fetch_assoc($result)) { echo $row["firstname"]; echo $row["lastname"]; echo $row["dob"]; } }//if(isset($_POST['submit'])) ?> <html> <body> <form action="login_success8.php" method="post"> <p> <input type="text" name="first" size="20" /> First name<br /> <input type="text" name="last" size="20" /> Last name<br /> <input name="dob" type="text" size="20" id="dob" /> Date of Birth<br /> <input type="submit" name="submit" value="Search" /> <input type="reset" value="Reset fields" /> </p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/246014-simple-php-form-not-working/#findComment-1263438 Share on other sites More sharing options...
erdem Posted August 30, 2011 Share Posted August 30, 2011 <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); $host = " "; $database = " "; $username = " "; $password = " "; $tbl_name = "users"; $conn = mysql_connect($host, $username, $password) or die("Could not connect: " . mysql_error()); if($conn) { mysql_select_db($database); echo "connected to database!!"; } else { echo "failed to select database"; } //include('bouncer.php'); //$currentUser = $_SESSION['myusername']; if(isset($_POST['submit'])) { $first = mysql_real_escape_string( $_POST['first']); $last = mysql_real_escape_string( $_POST['last']); $dob = mysql_real_escape_string( $_POST['dob']); //THE SEARCH FUNCTION //$sql = mysql_query ( "SELECT * FROM users WHERE firstname LIKE '%$first%' //OR lastname LIKE '%$last%' OR dob LIKE '%$dob%'") or die(mysql_error()); $result = mysql_query ("SELECT * FROM users WHERE firstname LIKE '%" . $first . "%' OR lastname LIKE '%" . $last . "%' OR dob LIKE '%" . $dob. "%' ") or die(mysql_error()); //$result = mysql_query($sql) or die( "<br>Query: $sql<br>Failed with error: " . mysql_error() ); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } while ($row = mysql_fetch_assoc($result)) { echo $row["firstname"]; echo $row["lastname"]; echo $row["dob"]; } }else{ ?> <html> <body> <form action="login_success8.php" method="post"> <p> <input type="text" name="first" size="20" /> First name<br /> <input type="text" name="last" size="20" /> Last name<br /> <input name="dob" type="text" size="20" id="dob" /> Date of Birth<br /> <input type="submit" name="submit" value="Search" /> <input type="reset" value="Reset fields" /> </p> </form> </body> </html> <?php } ?> change to this. you should use else. Quote Link to comment https://forums.phpfreaks.com/topic/246014-simple-php-form-not-working/#findComment-1263453 Share on other sites More sharing options...
silverglade Posted August 30, 2011 Author Share Posted August 30, 2011 Thank you so much erdem, that is definitely an interesting solution, it is going to take me a while to figure out why that worked. LOL. thank you!! Quote Link to comment https://forums.phpfreaks.com/topic/246014-simple-php-form-not-working/#findComment-1263557 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.