2

I have a registration form in PhP submitting various entries into a MySQL database.

The code is as:

if($conn)
    {

    $sql = "INSERT INTO Participants
       (ID,
        title, 
        surname,
        name,
        organization1,
        organization2,
        address1,
        address2,
        country,
        email,
        phone,
        type_reg,
        abstract,
        bm1,
        bm2,
        bm3,
        bm4                 )

        VALUES         
       ('$auth_id',
        '$auth_title', 
        '$auth_sname',
        '$auth_name',
        '$auth_org_line1',
        '$auth_org_line2',
        '$auth_add_line1',
        '$auth_add_line2',
        '$auth_country',
        '$auth_email',
        '$auth_phonen',
        '$reg_type',
        '$new_file_name',
        '$bm1',
        '$bm2',
        '$bm3',
        '$bm4'          )";


    mysql_query($sql);
    if( mysql_errno() !== 1062) {
        print '<script type="text/javascript">'; 
        print 'alert("Your pre-registration has been submitted successfully. You will receive a confirmation e-mail soon.")';
        print '</script>';
        print '<script type="text/javascript">'; 
        print 'parent.location.href = "Success.html"';
        print '</script>';
mysql_close($conn);
    }
    else
    {
    echo 'Data base error. Try later.';
    }

Because I am sending confirmation e-mails with cc to me, I am aware of the people registering and when I check the database I find that some of them are not being inserted into the database.

What I'm looking for is for a way to only validate the registration IF the entries were inserted into the MySQL table.

Is there a simple way?

Should I verify if the query was successful? How?

Should I look for the ID on the table and verify if it exists? How? (the entry ID is set as unique)

1
  • 3
    MySQL_Query functions are deprecated, you should use PDO instead Commented May 29, 2013 at 0:01

1 Answer 1

3

Yes. mysql_affected_rows() will return the number of rows affected by your query. If zero, it was not inserted. If >= 1, it was.

So:

if ( mysql_affected_rows() >= 1 ){ /* inserted! now do something... */ }

If you are using an auto-incrementing column for row ID, you can use mysql_insert_id() as well:

if ( mysql_insert_id() > 0 ) { /* inserted! now do something... */ }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.