gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:18:49 25 4
gpt4 key购买 nike

我知道这是 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>";
}
?>

最佳答案

//«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;

关于php - 正确的语法查询插入表(MySql),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23418278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com