1

Possible Duplicate:
insert contacts into database but does not want to duplicate already existing contact

what i intend to do is, check if a user's account is already stored in my database, redirect to another url else store his data in the database. following is the query i wrote. it is not working please suggest where i went wrong. thaks.

$result = mysql_query("SELECT * FROM centraluser where id = '$id'");
$row = mysql_fetch_row($result);
if($row) {
    mysql_query("UPDATE central SET time = '$time' WHERE id = '$id'");
    $url = "http://www.somesite.com";
    echo '<script type="text/javascript"> alert("Sorry! you can't register twice.");</script>';
    echo '<script type="text/javascript">top.location.href = "'.$url.'";</script>';die;exit;

}
else  {
    mysql_query("INSERT INTO centraluser VALUES ('$id','$name','$email','0','5000','0','0','$birthday','$time')");
    echo('welcome new user');
Community
  • 1
  • 1
whatf
  • 6,378
  • 14
  • 49
  • 78

2 Answers2

1

First of all you are having the error with this code

escape string and use this code below.

and define what part of your code is still not working.

echo '<script type="text/javascript"> alert("Sorry! you can\'t register twice.");</script>';
echo '<script type="text/javascript">top.location.href = "'.$url.'";</script>';die;exit;
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
  • sorry to put it this way, but i am aware of it, was just a message not being used in the actual program. thanks anyway. – whatf Apr 16 '11 at 16:32
0

Hope this works

$result = mysql_query("SELECT * FROM centraluser WHERE id = '$id'");
$user_data = mysql_fetch_row($result);
if(empty($user_data)) {
    $qry = "INSERT INTO centraluser VALUES    ('$id','$name','$email','0','5000','0','0','$birthday','$time')";
    mysql_query($qry);
    $_SESSION['msg'] = 'welcome new user';
    header("Location:dashboard.php"); // write your main page after login
    exit();
}
else {
     $qry="UPDATE central SET time = '$time' WHERE id = '$id'";
     mysql_query($qry);
     $url = "http://www.somesite.com";
     echo '<script type="text/javascript"> alert("Sorry! you can\'t register twice.");</script>';
     echo '<script type="text/javascript">top.location.href ="'.$url.'";</script>';
    exit();
}
xkeshav
  • 53,360
  • 44
  • 177
  • 245