例如,查询如下:

<?php
$sql  = "INSERT INTO oppgave
    (username, besvarelse, modulid)
    VALUES
    ('$username', '$besvarelse', '$modulid');
    ";

    mysql_query($sql, $tilkobling);
?>

我该怎么做:
如果modulid行不为空,则echo“您以前发送过此模块”;

最佳答案

试试这个:

<?php
$sql  = "INSERT INTO oppgave(username, besvarelse, modulid)
         select '$username', '$besvarelse', '$modulid'
         from dual
         where not exists (select modulid from oppgave where modulid='$modulid')";

$result=mysql_query($sql, $tilkobling);
if(mysql_affected_rows()==0){
      echo "Failed to insert duplicate modulid";
}else{
      echo "inserted";
}
?>

在SQL查询中,只有在数据库表中不存在“cc>”时才允许插入。这种情况也可以通过对表列应用$modulid约束来处理(但请记住,它允许同时使用UNIQUE值)

10-08 06:57