sql update没有执行

时间:2016-04-08 13:24:53

标签: php mysql pdo

道歉,如果有另一个有相同问题的Feed,我尝试了不同的建议解决方案,但我仍然遇到错误,我不明白为什么!

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

  

致命错误:带有消息的未捕获异常'PDOException'   'SQLSTATE [HY093]:参数号无效:参数未定义'   在   /Applications/XAMPP/xamppfiles/htdocs/love-deals/admin/update_offer.php:46   堆栈跟踪:#0   /Applications/XAMPP/xamppfiles/htdocs/love-deals/admin/update_offer.php(46):   PDOStatement->执行(数组)#1 {main}抛出   /Applications/XAMPP/xamppfiles/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等,所以请保持温和,非常感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

首先,你有更新语句的语法错误,正如其他人提到的那样,改变:

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语法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
));

答案 1 :(得分:1)

您使用的是错误的Update

语法

这将是

range()

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

答案 2 :(得分:0)

感谢您的回复。我原来的更新语法实际上就像你提到的那样,我在查看其他一些解决方案时已经改变了它,并没有改回来,但无论哪种方式,即使语法正确,我仍然会遇到同样的错误。

通过回复,我可以看到我有&#39;:id&#39; =&gt; $ offerID但是在sql代码中使用了:offerid,显然需要更新,所以感谢指出!希望这能解决问题......