doudouchan5830 2012-07-18 01:51
浏览 25
已采纳

在添加到数据库之前重复检查

I have a code which kinda works, but not really i can't figure out why, what im trying to do is check inside the database if the URL is already there, if it is let the user know, if its not the go ahead and add it.

The code also makes sure that the field is not empty. However it seems like it checks to see if the url is already there, but if its not adding to the database anymore. Also the duplicate check seems like sometimes it works sometimes it doesn't so its kinda buggy. Any pointers would be great. Thank you.

    if(isset($_GET['site_url']) ){

    $url= $_GET['site_url'];


    $dupe = mysql_query("SELECT * FROM $tbl_name WHERE URL='$url'");
    $num_rows = mysql_num_rows($dupe);
    if ($num_rows) {
    echo 'Error! Already on our database!';
    }
    else {
    $insertSite_sql = "INSERT INTO $tbl_name (URL) VALUES('$url')";
    echo $url;
    echo ' added to the database!';

    }

}
else {
echo 'Error! Please fill all fileds!';
}
  • 写回答

3条回答 默认 最新

  • dougouqin0763 2012-07-18 01:58
    关注

    You should take PhpMyCoder's advice on the UNIQUE field type.

    Also, you're not printing any errors.

    Make sure you have or die (mysql_error()); at the end of your mysql_* function(s) to print errors.

    You also shouldn't even be using mysql_* functions. Take a look at PDO or MySQLi instead.

    You're also not executing the insert query...

    Try this code:

    if(isset($_GET['site_url']) ){
    
    $url= $_GET['site_url'];
    
    
    $dupe = mysql_query("SELECT * FROM $tbl_name WHERE URL='$url'") or die (mysql_error());
    $num_rows = mysql_num_rows($dupe);
    if ($num_rows > 0) {
    echo 'Error! Already on our database!';
    }
    else {
    $insertSite_sql = "INSERT INTO $tbl_name (URL) VALUES('$url')";
    mysql_query($insertSite_sql) or die (mysql_error());
    echo $url;
    echo ' added to the database!';
    
    }
    
    }
    else {
    echo 'Error! Please fill all fileds!';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大