chaseman Posted February 25, 2011 Share Posted February 25, 2011 I want the script to check if something already has been submitted into the database before the submission, I tried it to do it with num_rows, but I'm encountering a problem. Here's the script: $con_submit = $_POST['submit']; $user_id = $_SESSION['user_id']; if ($con_submit && isset($user_id)) { $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); // POST variables $knuffix_name = strip_tags(trim($_POST['knuffix_name'])); $knuffix_contribution = $_POST['knuffix_contribution']; $knuffix_category = strip_tags(trim($_POST['sort_category'])); $query = sprintf("SELECT * FROM con WHERE contribution = '%s'", mysqli_real_escape_string($dbc, $knuffix_contribution)); $query_run = mysqli_query ($dbc, $query) or die (mysqli_error ($dbc)); $num_rows = mysqli_num_rows ($query_run) or die (mysqli_error ($dbc)); // $assoc = mysqli_fetch_assoc ($query_run) or die (mysqli_error ($dbc)); echo "num rows" . $num_rows; // and then as an example: if ($num_rows == 0) { // run INSERT INTO script } } When there are entries in the database then num_rows will return me the amount of entries in the echo, BUT if there are ZERO entries, then nothing will happen, and that is because of the query. Since the query looks as follows: SELECT * FROM con WHERE contribution = '$variable_post_submission' Which means if there's no entry in the database at all, then the query has nothing to select, which again makes the rest of the script NOT work. Which means that num_rows does not return any value, the echo will not even get printed out. But I on the other hand would like num_rows to return zero and have the script continue by INSERTING the submitted data into the database. Any ideas how I could accomplish this? Quote Link to comment https://forums.phpfreaks.com/topic/228833-how-to-check-if-something-has-been-submitted-all-time-already-before-submission/ Share on other sites More sharing options...
chaseman Posted February 25, 2011 Author Share Posted February 25, 2011 To make the question even clearer, is there any way I can make num_rows return ZERO, if the query does not find any equal contribution in the database? Quote Link to comment https://forums.phpfreaks.com/topic/228833-how-to-check-if-something-has-been-submitted-all-time-already-before-submission/#findComment-1179705 Share on other sites More sharing options...
Psycho Posted February 25, 2011 Share Posted February 25, 2011 If no results are returned from a SELECT query, mysqli_num_rows() WILL return 0. If your echo statement is not displaying then something else is causing your problem. Since that code is wrapped in an IF statement, I would assume that the condition for that IF statement is returning false and the queries are never being run at all. if ($con_submit && isset($user_id)) { Quote Link to comment https://forums.phpfreaks.com/topic/228833-how-to-check-if-something-has-been-submitted-all-time-already-before-submission/#findComment-1179708 Share on other sites More sharing options...
chaseman Posted February 25, 2011 Author Share Posted February 25, 2011 That's weird, I didn't realize that num_rows would still return ZERO. As I explained in my first post, num_rows WILL return 2 or 3 or even 5, if there are multiple entries in the database, so in that sense the if statement you quoted should be correct. Only if there's no entry at all in the database then the echo statement will never show up. I will try out more and look into this, thanks for letting me know though, at least I know that I can accomplish what I'm trying to achieve. EDIT: I've just tried it again, num_rows 1 will show up but ZERO won't show up. Quote Link to comment https://forums.phpfreaks.com/topic/228833-how-to-check-if-something-has-been-submitted-all-time-already-before-submission/#findComment-1179712 Share on other sites More sharing options...
chaseman Posted February 25, 2011 Author Share Posted February 25, 2011 I found the mistake, the "or die" behind the functions was killing the script for some reason I don't know why. I removed the "or die" and now it's working as expected, I now get a num rows 0. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/228833-how-to-check-if-something-has-been-submitted-all-time-already-before-submission/#findComment-1179728 Share on other sites More sharing options...
Psycho Posted February 25, 2011 Share Posted February 25, 2011 I found the mistake, the "or die" behind the functions was killing the script for some reason I don't know why. I removed the "or die" and now it's working as expected, I now get a num rows 0. Thanks for the help. Ah, I didn't catch that. You would never use an or die() after a num_rows() call (assuming you already validated that the query didn't fail). Since the value was 0 - the code interpreted that as false and performed the on die() action. Since there was no error with the query there was no output displayed from the die() Quote Link to comment https://forums.phpfreaks.com/topic/228833-how-to-check-if-something-has-been-submitted-all-time-already-before-submission/#findComment-1179744 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.