显示数据已存在于数据库中的文本

时间:2015-01-20 02:02:16

标签: php

我正在尝试学习php。我决定做一个简单的系统作为练习。我能够将数据添加到我的数据库。现在,如果项目的id已存在于数据库中,我想阻止用户添加数据。我也能做到这一点。我的问题是文本总是表明交易是“成功的”,尽管事实并非如此。任何回复将不胜感激。任何回复将不胜感激。这是我的代码:

$query = "select * from setup_project where spin='$sspin' order by title"; 

 $numresults=mysql_query($query);
 $numrows=mysql_num_rows($numresults);



if ($numrows == 0)
 $query = mysql_query("insert into setup_project value ('$sspin','$stitle','$sfirm_id','$sequip','$sdateapp','$samnt','$srem');");



else{
 echo "SPIN already Exists!";
}

$message = "Successfully Added Project  to Database!";  



?>
<br><br><br><br><br><br>
<center><font size="6"><? echo "$message" ?></font></center>

1 个答案:

答案 0 :(得分:0)

这更容易:

  $query = mysql_query("insert into setup_project value ('$sspin','$stitle','$sfirm_id','$sequip','$sdateapp','$samnt','$srem');");


 if (mysql_errno==0){
    $message = "Successfully Added Project  to Database!";
  }
  elseif (mysql_errno==1062){
    echo "SPIN already Exists!";
  }

我想要多次插入INSERT,如果INSERT失败,我会按照插入更新。

然后使用$ affected = mysql_affected_rows();

检查UPDATE
 if (mysql_errno==0){
   $message = "Successfully Added Project  to Database!";
 }
 elseif (mysql_errno==1062){
   UPDATE setup_project SET (...) VALUES(...) WHERE ????
   $affected = mysql_affected_rows();
   if ($affected == 0){
     echo "Record was updated";
   }
   else{
     echo 'Record Exists, No Updates';
   }
 }
 else{
   echo mysql_error();
 }