douyong2531 2013-04-01 04:38
浏览 77

使用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?

  • 写回答

4条回答

  • duanji5116 2013-04-01 04:41
    关注

    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); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器