0

我无法获取此 AJAX 代码来更新我的数据库。该代码是onClick将运行命令以更新数据库的图像

HTML:

<a>
    <img class       = "heart" 
         src         = "images/heart.png" 
         onClick     = "favUpdate(0,1)" 
         onMouseover = "this.src='images/heart_mo.png'"
         onMouseout  = "this.src='images/heart.png'"/>
</a>

Javascript代码:

function favUpdate(fav_up, id_up) {
        $.ajax({
            type: 'post',
            url: 'includes/fav_update.php',
            data: {favorite: fav_up, id: id_up},
            success: function(output) {
              alert('success, server says '
                            + output
                            + 'Variables passed are '+fav_up+' '+id_up);
                }, 
                    error: function() {
              alert('something went wrong, Favorite update failed');
            }
            });
}

PHP代码:

<?php
    require_once('../Connections/main.php');
    $fav_update = mysql_real_escape_string($_POST['favorite']);
    $fav_id     = mysql_real_escape_string($_POST['id']);
    $query      = "UPDATE projects SET favorite = $fav_update WHERE id = $fav_id";
    mysql_query($query, $main); 
?>

主文件

<?php
$hostname_main = "localhost";
$database_main = "test";
$username_main = "root";
$password_main = "";
$main = mysql_pconnect($hostname_main, $username_main, $password_main) or trigger_error(mysql_error(),E_USER_ERROR); 
?>

有谁知道它为什么不更新数据库以及为什么“选项”没有获取变量的数据?

4

4 回答 4

1

尝试这个

<?php
    require_once('../Connections/main.php');
    $fav_update = mysql_real_escape_string($_POST['favorite']);
    $fav_id = mysql_real_escape_string($_POST['id']);
    $query = "UPDATE projects SET favorite = '".$fav_update."' WHERE id = '".$fav_id."'";
    mysql_query($query, $main); 
?>
于 2013-04-01T04:46:01.397 回答
0

$fav_update如果它的数据类型是字符串,你必须加上单引号VARCHAR,TEXT

$query = "UPDATE projects SET favorite = '$fav_update' WHERE id = $fav_id";
                                         ^           ^

从这里删除$main并尝试

mysql_query($query); 
于 2013-04-01T04:41:42.560 回答
0

请尝试调试您的 PHP(首先是服务器端代码):-

<?php
    require_once('../Connections/main.php');
    $fav_update = mysql_real_escape_string($_REQUEST['favorite']);
    $fav_id = mysql_real_escape_string($_REQUEST['id']);
    $query = "UPDATE projects SET favorite = $fav_update WHERE id = $fav_id";
    echo $query;
     mysql_query($query, $main); 
?>

使用 $_REQUEST 而不是 $_POST,并直接从浏览器调用此 api,方法是创建其 url,如http://localhost/filename.php?favorite=somevalue1&id=somevalue2

并检查您是否在数据库中插入,并通过打印来检查查询。

检查API后,请将$_REQUEST改回$_POST

于 2013-04-01T04:57:47.120 回答
0

将php脚本更改为:

<?php
    require_once('../Connections/main.php');
    $fav_update = mysql_real_escape_string($_POST['favorite']);
    $fav_id = mysql_real_escape_string($_POST['id']);
    $updateSQL = sprintf("UPDATE projects 
                          SET favorite=%s  
                          WHERE id=%s",
                          $fav_update,
                          $fav_id);
    mysql_select_db($database_main, $main);
    $Result1 = mysql_query($updateSQL, $main) or die(mysql_error());


?>

希望如果有人遇到同样的问题会有所帮助

于 2013-04-01T16:39:01.753 回答