sql更新未执行


sql update not executing

很抱歉,如果有另一个提要出现同样的问题,我尝试了不同的建议解决方案,但仍然出现错误,我不明白为什么!

我想使用html表单更新表中的一行。我已经用现有值填充了表单,并希望能够编辑这些值,并在提交表单时更新它们,但我收到了以下错误:

致命错误:未捕获异常"PDOException",消息为'SQLSTATE[HY093]:参数编号无效:未定义参数'在里面/Applications/XAMPP/examplefiles/htdocs/love-deals/admin/update_offer.php:46堆栈跟踪:#0/Applications/XAMPP/examplefiles/htdocs/love-deals/admin/update_offer.php(46):PDOStatement->execute(Array)#1{main}抛出/Applications/XAMPP/examplefiles/htdocs/love-deals/admin/update_offer.php在线46

以下是php/sql代码:

if(isset($_POST['update'])) {
$updateTitle = trim($_POST['title']);
$updateDesc = trim($_POST['desc']);
$updateRedeem = trim($_POST['redeem']);
$updateStart = trim($_POST['start']);
$updateExpiry = trim($_POST['expiry']);
$updateCode = trim($_POST['code']);
$updateTerms = trim($_POST['terms']);
$updateImage = trim($_POST['image']);
$updateUrl = trim($_POST['url']);
$updateSql = 'UPDATE codes SET (title,description,redemption,start,expiry,textcode,terms,image,url) = (:title,:description,:redeem,:start,:exp,:code,:terms,:image,:url) WHERE id=:offerid';
$update = $db->prepare($updateSql);
$update->execute(array(':title'=>$updateTitle,':description'=>$updateDesc,':redeem'=>$updateRedeem,':start'=>$updateStart,':exp'=>$updateExpiry,':code'=>$updateCode,':terms'=>$updateTerms,':image'=>$updateImage,':url'=>$updateUrl,':id'=>$offerID));
}

和html格式:

<form id="update_offer" class="col-md-6 col-md-offset-3" method="post" action="update_offer.php?id=<?php echo $offerID; ?>">
        <div class="form-group col-md-12">
            <label class="col-md-12" for="title">Title</label>
            <input id="title" class="form-control col-md-12" type="text" name="title" placeholder="Offer Title" value="<?php echo $title; ?>" required>
        </div>
        <div class="form-group col-md-12">
            <label class="col-md-12" for="desc">Description</label>
            <textarea id="desc" class="form-control col-md-12" name="desc" placeholder="Description" value="<?php echo $desc; ?>"></textarea>
        </div>
        <div class="form-group col-md-12">
            <label class="col-md-12" for="redeem">Redemption</label>
            <input id="redeem" class="form-control col-md-12" type="text" name="redeem" placeholder="Where to redeem" value="<?php echo $redeem; ?>" required>
        </div>
        <div class="form-group col-md-12">
            <label class="col-md-12" for="start">Start Date</label>
            <input id="start" class="form-control col-md-12" type="date" name="start" value="<?php echo $startDate->format('Y-m-d'); ?>" min="<?php echo date('Y-m-d') ?>" max="2021-12-31" required>
        </div>
        <div class="form-group col-md-12">
            <label class="col-md-12" for="expiry">Expiry Date</label>
            <input id="expiry" class="form-control col-md-12" type="date" name="expiry" value="<?php echo $expDate->format('Y-m-d'); ?>" min="<?php echo date('Y-m-d') ?>" max="2021-12-31" required>
        </div>
        <div class="form-group col-md-12">
            <label class="col-md-12" for="code">Code</label>
            <input id="code" class="form-control col-md-12" type="text" name="code" placeholder="Code (if applicable)" value="<?php echo $code; ?>">
        </div>
        <div class="form-group col-md-12">
            <label class="col-md-12" for="terms">Terms</label>
            <textarea id="terms" class="form-control col-md-12" name="terms" placeholder="Terms & Conditions" value="<?php echo $terms; ?>" required></textarea>
        </div>
        <div class="form-group col-md-12">
            <label class="col-md-12" for="url">Offer URL</label>
            <input id="url" class="form-control col-md-12" type="text" name="url" placeholder="Offer URL (if applicable)" value="<?php echo $url; ?>">
        </div>
        <div class="form-group col-md-12">
            <label class="col-md-8" for="image">Image <img src="../images/offers/<?php echo $image; ?>" alt="" style="width: 200px;" /></label>
            <input id="image" class="form-control col-md-4" type="file" name="image">
        </div>
        <div class="form-group col-md-12 pull-right">
            <button id="update" type="submit" name="update" class="btn btn-primary"><i class="glyphicon glyphicon-refresh"></i> Update</button>
        </div>
    </form>

我做错了什么?!我还在学习php等,所以请温柔一点,任何帮助都将不胜感激。

首先,正如其他人已经提到的,更新语句的语法错误,请更改:

UPDATE codes SET (title,description,redemption,start,expiry,textcode,terms,image,url) = (:title,:description,:redeem,:start,:exp,:code,:terms,:image,:url) WHERE id=:offerid

进入

UPDATE `codes`   
   SET `title` = :title,
       `description` = :description,
       `redemption` = :redeem,
       `start` = :start 
       `expiry` = :expiry 
       `textcode` = :code 
       `terms` = :terms 
       `image` = :image 
       `url` = :url 
 WHERE `id` = :offerid

在此处了解有关SQL Update语法的详细信息。

那么,还有一件事你在execute()中有一个错误。将您的:id更改为:offerid,如下所示:

$update->execute(array(
    ':title' => $updateTitle,
    ':description' => $updateDesc,
    ':redeem' => $updateRedeem,
    ':start' => $updateStart,
    ':exp' => $updateExpiry,
    ':code' => $updateCode,
    ':terms' => $updateTerms,
    ':image' => $updateImage,
    ':url' => $updateUrl,
    ':offerid' => $offerID
));

您使用了更新的错误语法

这将是

$updateSql = "UPDATE codes SET title =:title,
description =:description,
redemption =:redeem,
start =:start,
expiry =:exp,
textcode =:code,
terms :=terms,image =:image,
url =:url
WHERE id=:id";// write id instead of offset because you are binding ':id'=>$offerID

检查http://dev.mysql.com/doc/refman/5.7/en/update.html

感谢您的回复。我最初的更新语法实际上正如你所提到的,我在查看其他一些解决方案时对其进行了更改,并没有将其改回,但无论哪种方式,即使使用了正确的语法,我仍然会遇到同样的错误。

通过查看您的回复,我可以看到我有":id"=>$offerID,但在sql代码中使用了:offerID,这显然需要更新,所以感谢您指出这一点!希望这能解决问题。。。