Jump to content

Recommended Posts

Hi, I need to insert data into a table but only if it doesnt already exist.

 

I have tried numerous ways including counting rows and nothing works so help desperately needed here. ???

 

I have a form that submits the information to this page and then redirects back to the form page with the $errorMessage but for the life of me, it keeps on duplicating the data.

$query = fetch("SELECT * FROM avatar_blank WHERE `user_name` = '$userid' AND `avatar_name = 'default'");

if($query[avatar_name] == "default"){

$errorMessage = "You+already+have+that+avatar!";


header("Location: avatar.php?&error=$errorMessage");

}

else
mysql_query("INSERT INTO avatar_blank (user_name , avatar_url, avatar_name) VALUES ('$userid','$avatar_url','$avatar_name')");

$errorMessage = "You+now+have+a+$avatar_name+avatar!";


header("Location: avatar.php?&error=$errorMessage");


?>

 

Any help is appreciated

 

Here is another example Ive tried without all the values added

 

$query = mysql_query("SELECT * FROM avatar_blank WHERE `user_name` = '$userid' AND `avatar_name = ''");
$num = mysql_numrows($query);
if($num < 1){

INSERT INTO `avatar_blank` ( `user_name` , `avatar_url` , `avatar_name` )
VALUES (
'$userid', '', ''
);

}

Link to comment
https://forums.phpfreaks.com/topic/54774-if-not-exist-insert/
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.