简体   繁体   English

jQuery 移动 mySQL - 更新不起作用

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

I have a problem with my jquery mobile app.我的 jquery 移动应用程序有问题。
I'm trying to update a few things in my database, but it doesn't work.我正在尝试更新我的数据库中的一些内容,但它不起作用。 I don't get any errors from JS or PHP files (unless I open update.php without submiting anything, but then the only errors are undefined indexes for values I bind with $_POST).我没有从 JS 或 PHP 文件中得到任何错误(除非我打开 update.php 而不提交任何内容,但唯一的错误是我用 $_POST 绑定的值的未定义索引)。
The weirdest part is that identical code for INSERT INTO is actually working.最奇怪的部分是 INSERT INTO 的相同代码实际上是有效的。

Here is the working code:这是工作代码:
-JS: -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:

<?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;
?>

And the code that doesn't work:和不起作用的代码:
-JS: -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:

<?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;
?>

Is it some stupid mistake I can't find or should I do it anothe way?这是我找不到的愚蠢错误还是我应该以另一种方式做?

You have quotes ( ' s) around the bound variables, which turns them into string literals and breaks your update.您在绑定变量周围有引号 ( ' s),这会将它们转换为字符串文字并中断您的更新。 Remove them, and you should be OK:删除它们,你应该没问题:

$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