Jump to content

Recommended Posts

What is the proper way to check a table to see if something exists before inserting it? If it does exist, update the information, if it doesn't . . insert the info? This would be php into mySQL. . . . I have tried before and been unsuccessful .. .

 

something like this

if name='john' and week='22' - update row info ELSE insert row info

 

anyone help me out?

Link to comment
https://forums.phpfreaks.com/topic/62754-check-before-insert/
Share on other sites

<?php
$one = mysql_num_rows(mysql_query("SELECT * FROM tbl WHERE name='$name' AND week='$week'")) or die(mysql_error());
if($one == 0){
//make new row
}else{
//edit current row
}
?>

If you don't know how to make rows or update them I will help with that too

Link to comment
https://forums.phpfreaks.com/topic/62754-check-before-insert/#findComment-312359
Share on other sites

$update = mysql_query("UPDATE tbl SET column1 = '$column1' AND column2 = '$column2'") or die(mysql_error());

 

They can be nasty, sometimes I get them wrong still...

If you want to check it updated ok you can add:

if(!$update){
echo "Error!?!?!?";
}else{
echo "Worked perfectly! ";
}

Link to comment
https://forums.phpfreaks.com/topic/62754-check-before-insert/#findComment-312366
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.