Jump to content

How to Check if Something Has Been Submitted ALL TIME Already Before Submission?


chaseman

Recommended Posts

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?

Link to comment
Share on other sites

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)) {

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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()

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.