Re: verifying sql INSERTs

From: Date: Tue, 26 Aug 2003 10:58:45 +0000
Subject: Re: verifying sql INSERTs
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-160744@lists.php.net to get a copy of this message
mysql_query() will return true for queries that don't return rows and were executed without error, so you can use return mysql_query(); You should also make a UNIQUE index on email column, then you can check the number returned by mysql_errno(), one number I don't remember signals duplicate entry error. Chris W. Parker wrote:
Hi! (MySQL) What's the best way to determine a sql INSERT statement has executed successfully? What I've got is a function whose sole purpose is to add new staff members to an app. I've written. There's a basic form that the user fills out (first name, last name, email address) and submits. Only one record per email address is allowed. Here is a pseudo code version of what I have right now. function insertStaffer($fname, $lname, $email) { $sql = "SELECT table WHERE email = '$email'"; if(1 or more records are found) { return 0; // means email address is already used } else { $sql = "INSERT INTO table ..."; return 1; // assume record inserted correctly } } Now for my question: Should I just assume the INSERT INTO has worked correctly (like I'm currently doing) and return a 1? Or should I verify that the INSERT worked correctly (by using the MySQL last_insert_id() function)? Alternatively... maybe the mysql_query() function returns a true/false status based on what happens? Hmm... I should look into that. I'd appreciate any advice you have to give. Thanks, Chris.


« previous php.general (#160744) next »