gpt4 book ai didi

PHP mySQL 更新行函数未按预期工作 - SQLSTATE[42000] : Syntax error or access violation: 1064

转载 作者:行者123 更新时间:2023-11-29 17:48:21 26 4
gpt4 key购买 nike

抱歉,我知道周围有几个类似的问题,但我找不到一个足够接近的问题。我有一个编辑和更新表单,它正在获取所提取的所选行的条目,该表单工作正常。然而,当尝试调用更新函数时,它不起作用,但根据我尝试修复它的方式,给我返回了各种错误。当前版本抛出上述错误代码,更具体的是指 SQL 代码,我看不出有什么问题:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(event_name='test', event_description='test', event_cate' at line 2

根据上面的内容,变量显然是按预期传递的,但事件没有被更新。我已经尝试过传递 event_id 和不传递 event_id,event_id 是自动增量的并且不应更新,此外还有一个 user_id_fk 我认为这里与此无关。我的 createevent 函数几乎相同并且有效。我是否需要传递数组才能更新现有行?

我的页面 php:

if(isset($_POST['update']))
{
$eventid=($_POST['eventid']);
$eventname=strip_tags($_POST['eventname']);
$eventdescription=strip_tags($_POST['eventdescription']);
$eventcategory=strip_tags($_POST['eventcategory']);
$eventdate = $_POST["eventdate"];
$sqlDate = date("Y-m-d",strtotime($eventdate));
$eventvenue=strip_tags($_POST['eventvenue']);
$eventlocation=strip_tags($_POST['eventlocation']);


if($eventname=="") {
$error[] = "provide eventname !";
}
if($eventcategory=="") {
$error[] = "provide eventcategory !";
}
if($eventdescription=="") {
$error[] = "provide eventdescription !";
}
if($eventlocation=="") {
$error[] = "provide eventlocation !";
}
else if($eventdate=="") {
$error[] = "provide a date !";
}
else if($eventvenue=="") {
$error[] = "provide eventvenue !";
}


else
{

if($event->updateevent($eventid,$eventname,$eventdescription,$eventcategory,$eventdate,$eventvenue,$eventlocation))
{
$event->redirect('members.php');
echo "Your event has been successfully updated.";
}

}

}

引用的html表单:

    <div id="updateevent">
<form action="" method="POST">
<input type='hidden' name="eventid" required="required" value="<?php echo $row['event_id']; ?>">
<label>Event title: </label>
<input type="text" name="eventname" required="required" value="<?php echo $row['event_name']; ?>"/><br /><br />
<label>Description: </label>
<input type="text" name="eventdescription" required="required" value="<?php echo $row['event_description']; ?>"/><br /><br />
<label>Category: </label>
<input type="text" name="eventcategory" required="required" value="<?php echo $row['event_category']; ?>"/><br /><br />
<label>Venue: </label>
<input type="text" name="eventvenue" required="required" value="<?php echo $row['event_venue']; ?>"/><br /><br />
<label>Event date: </label>
<input type="date" name="eventdate" required="required" value="<?php echo $row['event_date']; ?>"/><br /><br />
<label>Event location: </label>
<input type="text" name="eventlocation" required="required" value="<?php echo $row['event_location']; ?>"/><br /><br />
<input type="submit" class="button" name="update" value="Update" /></form>
</form>
</div>

我的 events.php 类中被调用的函数:

public function updateevent($event_id,$event_name,$event_description,$event_category,$event_date,$event_venue,$event_location)
{
try
{

$stmt = $this->conn->prepare("UPDATE events
SET event_name=:eventname, //UPDATED
event_description=:eventdescription,
event_category=:eventcategory,
event_date=:sqldate,
event_venue=:eventvenue,
event_location=:eventlocation
WHERE event_id=:eventid");

$stmt->bindparam(":eventid", $event_id);
$stmt->bindparam(":eventname", $event_name);
$stmt->bindparam(":eventdescription", $event_description);
$stmt->bindparam(":eventcategory", $event_category);
$stmt->bindparam(":sqldate", $event_date);
$stmt->bindparam(":eventvenue", $event_venue);
$stmt->bindparam(":eventlocation", $event_location);

$stmt->execute();

return $stmt;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}

感谢您对此提供的任何帮助

**

UPDATE: Upon removing the brackets within the UPDATE statement I get not error back but the row still is not updating.

**

最佳答案

尝试删除():

UPDATE events 
SET event_name=:eventname,
event_description=:eventdescription,
event_category=:eventcategory,
event_date=:sqldate,
event_venue=:eventvenue,
event_location=:eventlocation
WHERE event_id=:eventid

SET 没有 ():https://dev.mysql.com/doc/refman/5.7/en/update.html

关于PHP mySQL 更新行函数未按预期工作 - SQLSTATE[42000] : Syntax error or access violation: 1064,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49615046/

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