Jump to content

Archived

This topic is now archived and is closed to further replies.

bilis_money

Incorporating condition statement into MySQL INSERT? how?

Recommended Posts

Yo! brothers...

i have codes here,

[code]
//insert new record now
$query = "INSERT INTO topics (title, description, url)
VALUES ('$title_tmp', '$descrip_tmp', '$url_tmp'";
mysql_query($query) or die('Error, insert query failed');

$COerr_msgs->err_msgs('add1');
[/code]

I would like to add conditional statement with the MySQL query,
let say if the insertion of the new record into the database is successful,
then i want to add a successful message that it was successfully added.

Now how could i determine if the new record was successfully inserted?
I guess i have already known this before but maybe i have merely forgotten so.

thank you very much in advance.

Share this post


Link to post
Share on other sites
if(mysql_query($query)){
echo "success. it worked. good job.";
}else{
echo "it failed because: ".mysql_error();
}

Share this post


Link to post
Share on other sites
after you insterted the data into the database, you could query and fetch it again.
and if one of the array field is empty you could determin if it went ok or not!
i think something like this would do it.
just fill in the conditional statement with what you want and thats it!
[CODE]
$query ="SELECT * FROM topics WHERE title = $title_tmp";
$data = mysql_query($query);
$fetch = mysql_fetch_array($data);
if ( (empty($fetch['title'])) || (empty($fetch['description'])) || (empty($fetch['url'])) ){
}
[/CODE]

Share this post


Link to post
Share on other sites
Use  [code]mysql_affected_rows ( );[/code]
http://www.php.net/manual/en/function.mysql-affected-rows.php

Share this post


Link to post
Share on other sites

×

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.