【问题标题】:jQuery $.post() and MySQL problemjQuery $.post() 和 MySQL 问题
【发布时间】:2010-08-08 16:23:33
【问题描述】:

我正在使用 jQuery 和 $.post()。我的聊天 .php 代码 sn-p 如下:

<?php
$msg=$_POST['msg'];
mysql_connect("localhost","root");
mysql_select_db("user");

mysql_query("INSERT INTO space (name,msg,serial) VALUES('Test','$msg','1')");
?>

这是我的 HTML 文件的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Shout!</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<script type="text/javascript">

var status=1;
function action() {



if(status==1) {
$("#Layer1").hide("slow");
$("#Layer3").hide("fast");
$("#Layer4").hide("slow");
$("#close").attr("src","open.jpg");
status=0;

} 

else if(status==0) {
status=1;
$("#Layer1").show("slow");
$("#Layer3").show("fast");
$("#Layer4").show("slow");
$("#close").attr("src","close.jpg");

}




}


function sendline() {


var msg=$("#msg").val();
$.post("chat.php",{msg:msg});
$("#msg").val(" ");
}

function typeyo() {
var text=$("#msg").val();

$("#Layer6").html(text);

}

</script>
<style type="text/css">
<!--
body {
background-color: #000000;
}
#Layer1 {
position:absolute;
width:200px;
height:115px;
z-index:1;
left: 199px;
top: 3px;
}
#Layer2 {
position:absolute;
width:69px;
height:64px;
z-index:2;
left: 570px;
top: 543px;
}
#Layer3 {
position:absolute;
width:131px;
height:91px;
z-index:3;
left: 487px;
top: 327px;
}
.style1 {
color: #FFFFFF;
font-family: "Segoe UI";
font-weight: bold;
}
#Layer4 {
position:absolute;
width:99px;
height:38px;
z-index:4;
left: 744px;
top: 485px;
}
#Layer5 {
position:absolute;
width:274px;
height:70px;
z-index:5;
left: 422px;
top: 62px;
}
#Layer6 {

width:638px;
height:356px;
z-index:5;
left: 352px;
top: 105px;



}
-->
</style></head>

<body>
<div class="style1" id="Layer3">
<textarea name="textarea" cols="30" rows="5" id="msg" ></textarea>
</div>
<div id="Layer1">Hello World!<img src="body.jpg" width="842" height="559" /></div>
<div id="Layer2"><img src="close.jpg" alt="Go Online/Offline" name="close" width="63" height="64" id="close" OnClick="action()"/></div>
<div id="Layer4">
<input type="button" value="Send Line" onclick="sendline()" /></div>
<div id="Layer6" style="color:white;font-family:Segoe UI;font-size:16px;width:500px; height:400px; overflow:auto;"></div>
</body>
</html>

现在,在我提供的 HTML 代码上使用 chat.php for $.post() 发布变量 msg 似乎有些问题。

在此处发送“消息”似乎有问题。 chat.php 文件很好,因为如果我们直接运行它,而不是通过 $.post() 调用它可以完美运行

请帮助!谢谢!

【问题讨论】:

    标签: php jquery mysql


    【解决方案1】:

    您尝试通过 POST 发送的 msg 变量未初始化, 在 sendline 函数的开头添加以下行:

    var msg = $("#msg").val();
    

    注意:您在未经事先处理的情况下在 MySQL 查询中插入来自 POST 的变量时存在很大/巨大的安全问题。

    【讨论】:

    • 是的,我已经这样做了,很抱歉我忘记在我提供的网络链接上更新它。它仍然不起作用:/
    • 很抱歉,如果您不提供实际代码,则很难提供实际解决方案:(
    • 不是:var msg=("#msg").val();但是:var msg = $("#msg").val();
    • 仍然没有运气:(。没有任何东西被插入到表中:(
    • 我已经测试了您的页面并且数据现在已正确发送,但我想知道您的 chat.php 脚本是否与您的主页处于同一级别,因为techknowlegy.iblogger.org/chat.php 得到一个“403禁止”
    【解决方案2】:

    更新:我建议你使用 javascript 调试器,在 function sendline() 开头设置断点并单步执行代码。使用哪一种取决于您的浏览器。
    Firefox -> 例如Firebug
    IE7 -> 例如IE Developer Toolbar
    IE8+ -> 只需按F12 即可打开 IE 附带的开发者工具。


    除了darma's answer:您的 php 脚本容易发生 sql 注入(有意/恶意以及无意的注入)。正确使用prepared, parametrized statementsescape the data

    工作示例:

    test.html:

    <html>
      <head><title>test.html</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
          function sendline() {
            var msg = $('#msg').val();
            // --- add some tests on msg here, e.g. msg=="" ---
            $.post(
              "chat.php",
              {'msg':msg},
              function(data, textStatus, req) { $('#reply').text('reply: ' + data); }
            );
          }
        </script>
      </head>
      <body>
        <div>
          <textarea id="msg" rows="5" cols="30" name="textarea"></textarea>
          <button onclick="sendline()">send line</button>
        </div>
        <div id="reply">&nbsp;</div>
      </body>
    </html>
    

    聊天.php:

    <?php
    // --- add some tests on $_POST['msg'] here
    // e.g. isset($_POST['msg']) and 0<strlen(trim($_POST['msg'])) ---
    
    // you might want to use a slightly more sophisticated error handling than "or die(mysql_error())" ...but this is only an example.
    $mysql = mysql_connect("localhost","localonly", "localonly") or die(mysql_error());
    mysql_select_db("test", $mysql)  or die(mysql_error());
    $msg=mysql_real_escape_string($_POST['msg'], $mysql);
    $sql = "INSERT INTO space (name,msg,serial) VALUES('Test','$msg','1')";
    
    // mysql_query($sql, $mysql) or die(mysql_error());
    echo htmlspecialchars($sql);
    

    update2: 您的 php 脚本中仍然没有任何错误处理。任何 mysql_* 函数都可能由于各种原因而失败;测试结果。您需要“看到”这些错误,例如通过将它们写入日志文件或其他东西...
    试试

    <?php
    define('LOGERRORS', 1);
    function dbgLog($text) {
      if (LOGERRORS) {
        error_log(date('Y-m-d H:i:s : ').$text."\n", 3, 'error.log');
      }
    }
    
    if ( !isset($_POST['msg']) ) {
      dbgLog('script called without post parameter "msg"');
      die();
    }
    
    $mysql = mysql_connect("localhost","root");
    if ( !$mysql ) {
      dbgLog('database connection failed: '.mysql_error());
      die();
    }
    
    $result = mysql_select_db("user", $mysql);
    if ( !$result ) {
      dbgLog('database selection failed: '.mysql_error($mysql));
      die();
    }
    
    $msg=mysql_real_escape_string($_POST['msg'], $mysql);
    $sql = "INSERT INTO space (name,msg,serial) VALUES('Test','$msg','1')";
    dbgLog('sending query: '.$sql);
    
    $result = mysql_query($sql, $mysql);
    if ( !$result ) {
      dbgLog('query failed: '.mysql_error($mysql));
      die();
    }
    

    【讨论】:

    • 感谢您的回复!我想了解更多关于 function(data, textStatus, req) { $('#reply').text('reply: ' + data); } 。我不太明白这对 jQUery 很陌生)。能否请您详细说明一下?谢谢!
    • 另外,我想知道我的代码在技术上是否存在任何问题?我已经按照上面给出的文件中使用的方式使用了 $.post() 并且它工作得很好,但在这种情况下不是。知道为什么吗?
    • 您的代码中仍有var msg=("#msg").val();,即缺少$function(data, textStatus, req) { } 在 jquery 成功完成请求时被调用。
    • 很抱歉,服务器似乎有问题。页面没有更新。但它仍然没有解决 MySQL 的问题......知道它为什么会发生吗?我的代码在逻辑上和语法上是否正确(当然在做出你指出的更正之后)
    • 请用您当前的代码版本更新原始问题。如果由于某种原因 html 代码没有在您的服务器上更新,则还包括该代码的相关部分。 猜测您当前版本的真实外观对我们来说没有多大意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    相关资源
    最近更新 更多