2011-04-16 188 views
1

可能重复:
insert contacts into database but does not want to duplicate already existing contact检查记录是否已经存在于MySQL数据库

我打算做的是,检查是否有用户的帐户已存储在我的数据库是什么,重定向到另一个url,否则将其数据存储在数据库中。以下是我写的查询。它不工作,请建议我出错的地方。 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'); 

回答

0

希望这个作品

$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(); 
} 
1

首先,您所遇到的错误与此代码

逃生绳及以下使用此代码。

并定义代码的哪部分仍然不起作用。

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; 
+0

遗憾地这样说,但我意识到这一点,只是没有在实际的程序中使用的消息。不管怎么说,还是要谢谢你。 – whatf 2011-04-16 16:32:02

相关问题