checking if a record is already present in mySQL database [duplicate]
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');
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;
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();
}
精彩评论