使用AJAX使用jQuery更新数据库

I'm having trouble getting this AJAX code to update my database. The code is a image that onClick will run the command to update the database

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 code:

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 code:

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

main.php

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

Does anyone know why it's not updating the database and why the "option" isn't getting data for the variable?

duanchifo2866
duanchifo2866 我正在使用的服务器有PHP版本5.3.19,所以我不相信这是问题,我试图在PHPMyAdmin运行查询,并完美地工作
7 年多之前 回复
dstbtam8732
dstbtam8732 你也在使用obsolescemysql_*api它已弃用,并且会在php>=5.5中导致E_DEPRECATED错误检查stackoverflow.com/questions/12859942/...
7 年多之前 回复
dthl8036
dthl8036 尝试“UPDATE项目SETfavorite='”。$fav_update。“'WHEREid='”。$fav_id。“'”;
7 年多之前 回复
dotaer1993
dotaer1993 检查该查询是否成功运行,如果您收到任何错误,请告诉我们
7 年多之前 回复

4个回答

Try this

<?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); 
?>
douzhiba6873
douzhiba6873 尝试过,但没有奏效
7 年多之前 回复



如果数据类型为字符串 VARCHAR,TEXT <,则必须在 $ fav_update </ code>周围放置单引号 / code> </ p>

  $ query =“UPDATE项目SET favorite ='$ fav_update'WHERE id = $ fav_id”; 
^ ^
</ code> </ pre >

从此处删除 $ main </ code>并尝试</ p>

  mysql_query($ query);

</ code> </ pre>
</ div>

展开原文

原文

you have to put single quote around $fav_update if its datatype is string VARCHAR,TEXT

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

Remove $main from here and try

mysql_query($query); 

doukui4836
doukui4836 是的,因为在此之前我一直在为多个脚本使用相同的连接文件,这就是为什么我难倒
7 年多之前 回复
dongmao9217
dongmao9217 我们确定你已经与数据库正确连接了吗?
7 年多之前 回复
dsfdgdsfd23212
dsfdgdsfd23212 在警报(输出)上它回显查询,查询是正确的,适用于PHPMyAdmin
7 年多之前 回复
doushun1904
doushun1904 是的,我100%肯定
7 年多之前 回复
dongquming3255
dongquming3255 你是否正在调用正确的php文件?
7 年多之前 回复
dongqu9917
dongqu9917 仍然没有奏效
7 年多之前 回复
doushui20090526
doushui20090526 见编辑的答案。
7 年多之前 回复
douya7282
douya7282 尝试过,但没有奏效
7 年多之前 回复

Please try to debug your PHP (server side code first) :-

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

Use $_REQUEST instead of $_POST, and call this api directly from the browser, by creating its url like http://localhost/filename.php?favorite=somevalue1&id=somevalue2

And check whether you got the insert in DB or not, and check the query by printing it too.

And after checking the API, please change the $_REQUEST, back to $_POST

dongwei1855
dongwei1855 是更新就好了
7 年多之前 回复
douzuanze0486
douzuanze0486 你复制了查询,并在数据库上手动运行它?
7 年多之前 回复
doufen9815
doufen9815 我已经检查了它,但有趣的是,它的工作原因是我实际上经常使用该连接文件通过网站
7 年多之前 回复
dourong8495
dourong8495 请检查您的main.php一次,或在此处发布该文件
7 年多之前 回复
douni9620
douni9620 我正在获得正确的查询,但数据库仍未更新?
7 年多之前 回复

Changed php script to:

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


?>

hope that helps if anyone runs into the same issue

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问