通过表单更新MySQL,但没有更新

时间:2013-12-30 21:40:25

标签: php mysql pdo

在过去的10个小时里,我一直在墙壁上试图通过id来获取一个简单的表格来更新sql表数据。我确信它的一些简单的东西是我的脸。

如果有人可以用愚蠢的棍子拍我的ID,感激不尽!哈哈

目标 - 能够填写下面的表格并通过sql中的id更新一行。

<?php
    $dsn = 'mysql:dbname=xxxx;host=xxxxx';
    $user = 'xxxxx';
    $pass = 'xxxxx';
    if($_SERVER['REQUEST_METHOD'] != 'POST')
    {
        echo '<form method="post" action="">
        ID: <input type="text" name="a" /><br>
        Program: <input type="text" name="b" /><br>
        Description: <textarea row="6" cols="50" name="c"></textarea><br>
        Cost: <input type="text" name="d"><br>
        Source: <input type="text" name="e"><br>
        URL: <input type="text" name="f"><br>
        <input type="submit" value="Add Link" />
     </form>';
    }


    try {
         $dbh = new PDO($dsn, $user, $pass);
        $stmt = $dbh->prepare('UPDATE links SET Program = :program, Descr = :descr, Cost = :cost, Source = :source, url = :url, WHERE Id = :id');
    //Im sure $stmt->bindParam can be done via array but im just tring to get the stupid thing to update at the moment
    $stmt->bindParam(":id", $_POST["a"]);
    $stmt->bindParam(":program", $_POST["b"]);
    $stmt->bindParam(":descr", $_POST["c"]);
    $stmt->bindParam(":cost", $_POST["d"]);
    $stmt->bindParam(":source", $_POST["e"]);
    $stmt->bindParam(":url", $_POST["f"]);
    $stmt->execute();
    if (!$stmt) {
    echo "\nPDO::errorInfo():\n";
    print_r($dbh->errorInfo());}
    $dbh = null;


    }catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }

    ?>

感谢您的帮助! JR

1 个答案:

答案 0 :(得分:2)

您准备好的语句没有成功,但PDO尚未配置为抛出异常。它的默认行为是静默忽略错误。

查看PDO documentation on error handling

 try {
     // The constructor *will* throw an exception on error
     $dbh = new PDO($dsn, $user, $pass);

     // But subsequent stuff won't unless configured to do so...
     // Tell PDO to throw exceptions
     $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     // etc...

您的错误来源是WHERE子句之前的错误逗号。 (无论如何,我可以在视觉上发现唯一的错误。)

$stmt = $dbh->prepare('UPDATE links SET Program = :program, Descr = :descr, Cost = :cost, Source = :source, url = :url, WHERE Id = :id');
// -----------------------------------------------------------------------------------------------------------------^^^^^