【问题标题】:jQuery mobile mySQL - UPDATE doesn't workjQuery mobile mySQL - 更新不起作用
【发布时间】:2015-10-03 23:30:00
【问题描述】:

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

这是我找不到的愚蠢错误还是我应该以另一种方式来做?

【问题讨论】:

    标签: javascript php jquery mysql jquery-mobile


    【解决方案1】:

    您在绑定变量周围有引号 ('s),这会将它们转换为字符串文字并中断您的更新。删除它们,您应该就可以了:

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

    【讨论】:

    • 仅:“注意:未定义索引:第 19 行 C:\xampp\htdocs\app\update.php 中的 id 注意:未定义索引:C:\xampp\htdocs\app\update 中的摘要第 20 行的 .php 注意:未定义的索引:第 21 行的 C:\xampp\htdocs\app\update.php 中的引号 注意:未定义的索引:第 22 行的 C:\xampp\htdocs\app\update.php 中的 cmets"但那是当我打开文件而不提交表单时,PHP 没有其他错误,JS 也没有错误。
    猜你喜欢
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多