0

我试图编写一个简单的程序,服务器可以从客户端获取数据。我在我的脚本中写了一个简单的代码

var str = "testString"; 

$.post("http://anonymous.comze.com/test1.php", { string: str }); 

在服务器中,

$var = $_POST['string']; // this fetches your post action

$sql2 = "INSERT INTO afb_comments VALUES ('3',$var)";

$result2= mysql_query($sql2,$conn);

问题是 var 始终为空。例如,如果我将 $var 更改为“1111”,则可以执行 sql2,但如果我输入 $var,它就不起作用。谁能给点建议?

4

2 回答 2

1

你正在将字符串传递给查询,所以它应该是

$var = $_POST['string']; // this fetches your post action

$sql2 = "INSERT INTO afb_comments VALUES ('3','".$var."')";

$result2= mysql_query($sql2,$conn);

另请检查该列的数据类型。

于 2013-10-23T04:13:17.000 回答
0

使用此示例并从此代码中学习如何获取数据

或者

使用也可以使用这个链接:

http://api.jquery.com/jQuery.get/

$user 和 $pass 应该设置为你的 MySql 用户的用户名和密码。

我会使用这样的东西:

JS

success: function(data){
             if(data.status === 1){
                 sr = data.rows;
             }else{
                 // db query failed, use data.message to get error message
             }
        }
PHP:

<?php

    $host = "localhost";
    $user = "username";
    $pass = "password";
    $databaseName = "movedb";
    $tableName = "part parameters";

    $con = mysql_pconnect($host, $user, $pass);
    $dbs = mysql_select_db($databaseName, $con);
    //get the parameter from URL
    $pid = $_GET["pid"];
    if(empty($pid)){
        echo json_encode(array('status' => 0, 'message' => 'PID invalid.'));
    } else{
        if (!$dbs){
            echo json_encode(array('status' => 0, 'message' => 'Couldn\'t connect to the db'));       
        }
        else{
            //connection successful
            $sql = "SELECT `Processing Rate (ppm)` FROM `part parameters` WHERE `Part Number` LIKE `" . mysqli_real_escape_string($pid) . "`"; //sql string command
            $result = mysql_query($sql) or die(mysql_error());//execute SQL string command
            if(mysql_num_rows($result) > 0){
                $rows = mysql_fetch_row($result);
                echo json_encode(array('status' => 1, 'rows' => $rows["Processing Rate (ppm)"]);
            }else{
                echo json_encode(array('status' => 0, 'message' => 'Couldn\'t find processing rate for the give PID.'));   
            }
        }

    }

?>
As another user said, you should try renaming your database fields without spaces so part parameters => part_parameters, Part Number => part_number.
于 2013-10-23T04:15:48.090 回答