簡體   English   中英

jQuery 移動 mySQL - 更新不起作用

[英]jQuery mobile mySQL - UPDATE doesn't work

我的 jquery 移動應用程序有問題。
我正在嘗試更新我的數據庫中的一些內容,但它不起作用。 我沒有從 JS 或 PHP 文件中得到任何錯誤(除非我打開 update.php 而不提交任何內容,但唯一的錯誤是我用 $_POST 綁定的值的未定義索引)。
最奇怪的部分是 INSERT INTO 的相同代碼實際上是有效的。

這是工作代碼:
-JS:

$(document).ready(function(){
$("#saveForm").submit(function(){
    var formData=$(this).serialize();

    $.post('save.php',formData,processData).error(errorResponse);

    function processData(data){
        $("#popupSave").popup();
        $("#popupSave").popup("open");
    };

    function errorResponse(){
        alert("Something went wrong!");
    };

    return false; //Prevent the form from reloading
}); //end of submit function
}); //end of jquery document

-PHP:

<?php
$dsn = "mysql:host=localhost;dbname=titlesdbs";
$username="root";
$password="";

try
{
$conn = new PDO($dsn, $username, $password);
echo 'Connected!';
}
catch(PDOException $error)
{
echo 'Connection no established: ' . $error->getMessage();
}

$sql="INSERT INTO titlestbl (title, pages, date) VALUES(:title, :pages, :date)";
try {
    $st=$conn->prepare($sql);
    $st->bindValue(':title', $_POST['title'], PDO::PARAM_STR);
    $st->bindValue(':pages', $_POST['pages'], PDO::PARAM_STR);
    $st->bindValue(':date', $_POST['date'], PDO::PARAM_STR);
    $st->execute();
}
catch (PDOException $e) {
echo "Server error - Try again".$e->getMessage();
};
$conn=null;
?>

和不起作用的代碼:
-JS:

$(document).ready(function(){
$("#updateForm").submit(function(){
    var formData=$(this).serialize();

    $.post('update.php',formData,processData).error(errorResponse);

    function processData(data){
        $("#popupUpdate").popup();
        $("#popupUpdate").popup("open");
    };

    function errorResponse(){
        alert("Something went wrong!");
    };

    return false; //Prevent the form from reloading
}); //end of submit function
}); //end of jquery document

-PHP:

<?php
$dsn = "mysql:host=localhost;dbname=titlesdbs";
$username="root";
$password="";

try
{
$conn = new PDO($dsn, $username, $password);
//echo 'Connected!';
}
catch(PDOException $error)
{
echo 'Connection no established: ' . $error->getMessage();
}

$sql="UPDATE titlestbl SET check=1, summary=':summary', quotes=':quotes', comments=':comments' WHERE id=:id";
try {
    $st=$conn->prepare($sql);
    $st->bindValue(':id', $_POST['id'], PDO::PARAM_STR);
    $st->bindValue(':summary', $_POST['summary'], PDO::PARAM_STR);
    $st->bindValue(':quotes', $_POST['quotes'], PDO::PARAM_STR);
    $st->bindValue(':comments', $_POST['comments'], PDO::PARAM_STR);
    $st->execute();
}
catch (PDOException $e) {
echo "Server error - Try again".$e->getMessage();
};
$conn=null;
?>

這是我找不到的愚蠢錯誤還是我應該以另一種方式做?

您在綁定變量周圍有引號 ( ' s),這會將它們轉換為字符串文字並中斷您的更新。 刪除它們,你應該沒問題:

$sql = "UPDATE titlestbl SET check=1, summary=:summary, quotes=:quotes, comments=:comments WHERE id=:id";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM