Jump to content


Photo

Incorporating condition statement into MySQL INSERT? how?


  • Please log in to reply
4 replies to this topic

#1 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 10 August 2006 - 12:38 PM

Yo! brothers...

i have codes here,

//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');	

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.



#2 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 10 August 2006 - 12:56 PM

if(mysql_query($query)){
echo "success. it worked. good job.";
}else{
echo "it failed because: ".mysql_error();
}
"Confidence in the face of risk."

#3 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 10 August 2006 - 12:59 PM

thanks miccah


#4 Silverado_NL

Silverado_NL
  • Members
  • PipPipPip
  • Advanced Member
  • 105 posts
  • LocationZaandam (Netherlands)

Posted 10 August 2006 - 12:59 PM

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!
$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'])) ){
}

The Force is like ductape, it has a light side, it has a dark side, and it combines the universe together!

#5 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 10 August 2006 - 01:36 PM

Use 
mysql_affected_rows ( );
http://www.php.net/m...fected-rows.php




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users