dqhdz04240 2013-04-20 19:16 采纳率: 0%
浏览 56
已采纳

通过ajax调用php mySQL更新

I think this is simple question. I'm new to jQuery. I'm trying to make script, so that when you click on image, ajax will call php file which will update mySQL database.

My script:

<script>
function update(){      
        var request = $.ajax({
        url: "insert.php",
        type: "GET",            
        dataType: "html"
    });

    request.done(function(msg) {
        $("div.recen").html(msg);          
    });

    request.fail(function(jqXHR, textStatus) {
        alert( "Request failed: " + textStatus );
    });
}; 
</script>


HTML:

<div class="recen">
            <img src="./smile.png" class="pic" onclik="update()" /> 
</div>


PHP file:

<?php
$var='123';

$url='localhost';
$user='root';
$password='';

$sql=mysql_connect($url,$user,$password);
mysql_select_db('database');

$query="UPDATE table SET var='".$var."';";
if(mysql_query($query)) echo '<b>Done.</b>';
else echo mysql_error();
?>

For some reason, there is absolutely no response. Database remains the same and html page doesn't change a bit. Is there something wrong with the code, or I just can't update database through ajax?

  • 写回答

2条回答 默认 最新

  • dongzhiman2162 2013-04-20 19:18
    关注

    i think somthing wrong in your query

    try this

       $query="UPDATE table SET var='".$var."' ";
                                              ^^----remove this `;`
    

    change this

     if(mysql_query($query)) echo '<b>Done.</b>';
     else echo mysql_error();
    

    to

     if(mysql_query($query)){ echo '<b>Done.</b>';}
     else {echo mysql_error();}
    

    there is no onclik in javascript instead use onClick

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable