php - sql更新不执行

标签 php mysql pdo

如果有另一个 feed 出现同样的问题,我深表歉意,我尝试了不同的建议解决方案,但我仍然遇到错误,我不明白为什么!

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

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined' in /Applications/XAMPP/xamppfiles/htdocs/love-deals/admin/update_offer.php:46 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/love-deals/admin/update_offer.php(46): PDOStatement->execute(Array) #1 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/love-deals/admin/update_offer.php on line 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 更新语法的更多信息 here .

还有一件事,你在 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
));

关于php - sql更新不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36500784/

相关文章:

php - 错误更新限制mysql

php - 从变量中删除表情符号

php - 如何在 PHP 中解码具有多个对象的 JSON 字符串?

php - 在 PHP 中运行 MySQL *.sql 文件

php - LARAVEL ELOQUENT - 我想将用户表中的列添加到关系表中

MySQL/存储 MySQL 凭据

mysql - 想要在变量中获取数据库值

使用 PDO 的 PHP sql

PHP PDO 连接失败但没有错误

javascript - 将 dataURI 图像从 JavaScript 解析为 PHP