gpt4 book ai didi

php - Mysql插入和更新未知错误

转载 作者:太空宇宙 更新时间:2023-11-03 12:03:38 25 4
gpt4 key购买 nike

使用以下代码从 html 表单插入和更新数据库中的一些行。当我提交它们时,insert form 不执行任何操作,既不插入也不显示任何错误,update form 说: 数据库访问失败:您的 SQL 语法有误;查看与您的 MySQL 服务器版本对应的手册,了解在第 1 行的“Craciun, Perioada_eveniment=2014-12-25, Tip_even=Muzical, Locatie_eveniment=Bucu”附近使用的正确语法,即使我有删除查询以上,效果很好。我怎样才能解决这个问题?PS:已正确建立与数据库的连接:),谢谢!

HTML
<div class="update_row">
Update
<form method="post" action="">
*<input type="text" name="ID_even" Placeholder="Id eveniment"><br>
*<input type="text" name="nume" Placeholder="Nume eveniment"><br>
*<input type="text" name="perioada" Placeholder="Perioada eveniment"><br>
*<input type="text" name="tip" Placeholder="Tip eveniment"><br>
*<input type="text" name="locatie" Placeholder="Locatie eveniment"><br>
*<input type="text" name="id_organizator" Placeholder="ID Organizator"><br>
<input type="submit" name="submit" value="Modifica">
</form>
</div>

<div class="add_row">
Add
<form method="post" action="">
*<input type="text" name="id_add" Placeholder="Id eveniment"><br>
*<input type="text" name="nume_add" Placeholder="Nume eveniment"><br>
*<input type="text" name="perioada_add" Placeholder="Perioada eveniment"><br>
*<input type="text" name="tip_add" Placeholder="Tip eveniment"><br>
*<input type="text" name="locatie_add" Placeholder="Locatie eveniment"><br>
*<input type="text" name="id_org_add" Placeholder="ID Organizator"><br>
<input type="submit" name="submit_add" value="Adauga">
</form>
</div>


PHP
if (isset($_POST["id_add"]))
$id_add=$_POST["id_add"];
if (isset($_POST["nume_add"]))
$nume_add=$_POST["nume_add"];
if (isset($_POST["perioada_add"]))
$perioada_add=$_POST["perioada_add"];
if (isset($_POST["tip_add"]))
$tip_add=$_POST["tip_add"];
if (isset($_POST["locatie_add"]))
$locatie_add=$_POST["locatie_add"];
if (isset($_POST["id_org_add"]))
$id_org=$_POST["id_org_add"];
$submitcheck_add=isset($_POST["submit_add"]);


if($submitcheck_add && $nume_add !=0 && $perioada_add !=0 && $locatie_add !=0 && $id_org !==0){
$sql = "INSERT INTO evenimente (ID_even, Nume_eveniment, Perioada_eveniment, Tip_even, Locatie_eveniment, ID_Org)
VALUES ($id_add, $nume_add, $perioada_add, $tip_add, $locatie_add, $id_org )";
$result=query_mysql($sql);
}



if (isset($_POST["ID_even"]))
$id=$_POST["ID_even"];
if (isset($_POST["nume"]))
$nume=$_POST["nume"];
if (isset($_POST["perioada"]))
$perioada=$_POST["perioada"];
if (isset($_POST["tip"]))
$tip=$_POST["tip"];
if (isset($_POST["locatie"]))
$locatie=$_POST["locatie"];
if (isset($_POST["id_organizator"]))
$id_organizator=$_POST["id_organizator"];
$submitcheck=isset($_POST["submit"]);

if($submitcheck && $id !==0 && $nume !==0 && $perioada !==0 && $tip !==0 && $locatie !==0 && $id_organizator !==0 ){
$sql = "UPDATE evenimente SET Nume_eveniment=$nume, Perioada_eveniment=$perioada, Tip_even=$tip, Locatie_eveniment=$locatie, ID_org=$id_organizator WHERE ID_even=$id";
$result= query_mysql($sql);
}

乐:

PDO 示例:

   if (isset($_POST["ID_even"]))
$id=$_POST["ID_even"];
if (isset($_POST["nume"]))
$nume=$_POST["nume"];
if (isset($_POST["perioada"]))
$perioada=$_POST["perioada"];
if (isset($_POST["tip"]))
$tip=$_POST["tip"];
if (isset($_POST["locatie"]))
$locatie=$_POST["locatie"];
if (isset($_POST["id_organizator"]))
$id_organizator=$_POST["id_organizator"];
$submitcheck=isset($_POST["submit"]);

if($submitcheck && $id !==0 && $nume !==0 && $perioada !==0 && $tip !==0 && $locatie !==0 && $id_organizator !==0 ){
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "organizator_evenimente";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "UPDATE evenimente SET Nume_eveniment=$nume, Perioada_eveniment=$perioada, Tip_even=$tip, Locatie_eveniment=$locatie, ID_Org=$id_organizator WHERE ID_even=$id";

// Prepare statement
$stmt = $conn->prepare($sql);

// execute the query
$stmt->execute();

// echo a message to say the UPDATE succeeded
echo $stmt->rowCount() . " records UPDATED successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
}

错误:UPDATE evenimente SET Nume_eveniment=Concert Craciun,Perioada_eveniment=2014-12-25,Tip_even=Muzical,Locatie_eveniment=Bucuresti,ID_Org=2 WHERE ID_even=2
SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有误;查看与您的 MySQL 服务器版本对应的手册,了解在 'Craciun, Perioada_eveniment=2014-12-25, Tip_even=Muzical,

附近使用的正确语法

拜托,我卡在这一点上了!

最佳答案

您在查询中的字符串值周围缺少引号:

$sql = "INSERT INTO evenimente (ID_even, Nume_eveniment, Perioada_eveniment, Tip_even, Locatie_eveniment, ID_Org)
VALUES ('$id_add', '$nume_add', '$perioada_add', '$tip_add', '$locatie_add', '$id_org' )";

关于php - Mysql插入和更新未知错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27750556/

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