正确的语法查询插入表(MySql)

时间:2014-05-01 23:15:35

标签: php mysql sql

我知道这是mysqli,但我想知道如何使这个查询正确。 我想在表中插入用户名,besvarelse和modulid。 但如果besvarelse是空的或null我想写回消息,它已经完成了。

$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 "<h1>Allready delivered</h1>";
}else{
      echo "<h1>Delivery ok</h1>";
}
?>

1 个答案:

答案 0 :(得分:0)

//«if besvarelse is empty or null i want to write back message that its allready delivered.»
//To check besvarelse we shold not use SQL, it is much better to check it with PHP

if ($besvarelse=='' or is_null($besvarelse)) {
    $resp = "<h1>Allready delivered</h1>";    
} else {
    $sql  = ......;
    $result=mysql_query($sql, $tilkobling);
    if(mysql_affected_rows()==0){
        $resp="<h1>Allready delivered</h1>";
    }else{
        $resp="<h1>Delivery ok</h1>";
}
echo $resp;