0

Im trying to write a simple prgram that the server can get data from client. I write a simple code in my script

var str = "testString"; 

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

in the server,

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

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

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

The question is var is always null. The sql2 can be executed if I change $var into "1111" for example, but if I put $var, it doesn't work. Can anyone give some advice?

3
  • var_dump() your POST values and see what do you get. Commented Oct 23, 2013 at 4:14
  • 1
    Put quotes around $var in the query. Commented Oct 23, 2013 at 4:14
  • var_dump() is used to display info, however, i send the data from my client. How can my server display the info when the server receive the info? Commented Oct 24, 2013 at 17:34

2 Answers 2

1

your are passing string to the query so it should be

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

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

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

please also check datatype of the that column.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried your method, now I can run the code. However, in my database, i do insert a record but the value for $var$ is blank.
0

Use this example and learn from this code how to get data

Or

use can also use this link:

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

$user and $pass should be set to your MySql User's username and password.

I'd use something like this:

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.

1 Comment

That's a huge overkill.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.