grahamb314 Posted July 26, 2008 Share Posted July 26, 2008 Hi all, I want to check to see if "Show" (From the previous form) is already in the database I have. If it is, then echo something, if it isnt, then add it I think i`m nearly there ! <?php require_once 'mysql_connect.php'; $result = mysqli_query($mysqli, "SELECT show FROM shows WHERE show= '".$_POST["Show"]."'"); if (mysqli_num_rows($result) == 1) { echo "Show name already exists"; } else {$sql = mysqli_query($mysqli, "INSERT INTO `grahamb_test`.`shows` (`id`, `show`) VALUES (NULL,'".$_POST["Show"]."')"); } ?> Thanks! Graham Quote Link to comment Share on other sites More sharing options...
ratcateme Posted July 26, 2008 Share Posted July 26, 2008 whats the problem?? Scott. Quote Link to comment Share on other sites More sharing options...
grahamb314 Posted July 26, 2008 Author Share Posted July 26, 2008 Oh yea, sorry. The error: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /URL HERE on line 5 Quote Link to comment Share on other sites More sharing options...
ratcateme Posted July 26, 2008 Share Posted July 26, 2008 it looks like you first query is failing try changing it to $result = mysqli_query($mysqli, "SELECT show FROM shows WHERE show= '".$_POST["Show"]."'") or die(mysqli_error($mysqli)); and see what the output is Scott. Quote Link to comment Share on other sites More sharing options...
grahamb314 Posted July 26, 2008 Author Share Posted July 26, 2008 Thanks, Now we have: 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 'show FROM shows WHERE show= ''' at line 1 Quote Link to comment Share on other sites More sharing options...
grahamb314 Posted July 26, 2008 Author Share Posted July 26, 2008 Its the POST thing thats a bit odd. - The quotes or soemthing? - Any thoughts? $result = mysqli_query($mysqli, "SELECT show FROM shows WHERE show= '".$_POST["Show"]."'") or die(mysqli_error($mysqli)); Quote Link to comment Share on other sites More sharing options...
ratcateme Posted July 26, 2008 Share Posted July 26, 2008 try changing it to $query = "SELECT * FROM `shows` WHERE `show` = '{$_POST["Show"]}';"; $result = mysqli_query($mysqli, $query) or die("Query:{$query} <br>Error:".mysqli_error($mysqli)); are you sure that there is something in $_POST["Show"] even though that shouldn't matter Scott. Quote Link to comment Share on other sites More sharing options...
grahamb314 Posted July 26, 2008 Author Share Posted July 26, 2008 Thanks again, I put something and show and get 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 'show FROM shows WHERE show= '123'' at line 1 Let me try your suggestion Quote Link to comment Share on other sites More sharing options...
grahamb314 Posted July 26, 2008 Author Share Posted July 26, 2008 Okay, it now adds post the table in the DB, but it will still accept duplicates like 123 will still be accepted even if 123 is already in the DB Its gotta be something small, Quote Link to comment Share on other sites More sharing options...
ratcateme Posted July 26, 2008 Share Posted July 26, 2008 i copied your code and added a little output what happens now require_once 'mysql_connect.php'; $query = "SELECT * FROM `grahamb_test`.`shows` WHERE `show` = '{$_POST["Show"]}';"; $result = mysqli_query($mysqli, $query) or die("Query:{$query} <br>Error:".mysqli_error($mysqli)); echo "rows = ".mysqli_num_rows($result); if (mysqli_num_rows($result) == 1) { echo "Show name already exists"; } else { $sql = mysqli_query($mysqli, "INSERT INTO `grahamb_test`.`shows` (`id`, `show`) VALUES (NULL,'" . $_POST["Show"] . "')"); } Quote Link to comment Share on other sites More sharing options...
grahamb314 Posted July 26, 2008 Author Share Posted July 26, 2008 Looking good... rows = 1Show name already exists What was the problem you think? - So I don't do it again Quote Link to comment Share on other sites More sharing options...
ratcateme Posted July 26, 2008 Share Posted July 26, 2008 i am not sure what database does mysql_connect.php connect to. the only other thing i changed was i added `grahamb_test`. to the first query Scott. Quote Link to comment Share on other sites More sharing options...
grahamb314 Posted July 26, 2008 Author Share Posted July 26, 2008 Interesting! mysql_connect.php just connects to the database (With user and pass) - It's a seperate php page that I call in all Database related php pages All I can say now is thank's for your help. It's people like you that keep people wanting to learn! - If it wasnt for forums like this, I'd have given up on PHP a while ago! Thanks again! Graham 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.