verifying sql INSERTs
| From: | Chris W. Parker | Date: | Mon, 25 Aug 2003 23:46:47 +0000 |
| Subject: | verifying sql INSERTs | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-160689@lists.php.net to get a copy of this message | ||
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.