0

我想通过php将数据发送到mysql服务器。我的应用程序运行良好,但数据未显示在数据库中。这是我的代码:

            HttpClient httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost("http://127.0.0.1:4001/file.php");


            List<NameValuePair> pair=new ArrayList<NameValuePair>(2);
            pair.add(new BasicNameValuePair("fn",fname));
            pair.add(new BasicNameValuePair("ln",lname));


            httppost.setEntity(new UrlEncodedFormEntity(pair));
            HttpResponse response = httpclient.execute(httppost);

和我的 php:

            <?php

            $con=mysql_connect("host","username");
            if(!$con)
            {
            die("Could Not Connect".mysql_error());
            }

            $db="CREATE DATABASE login";
            mysql_query($db,$con);

            mysql_select_db("login",$con);

            $tab="CREATE TABLE info(FirstName varchar(20),LastName varchar(20))";
            mysql_query($tab,$con);

            $user_fname=$_POST['fn'];
            $user_lname=$_POST['ln'];

            $row="INSERT INTO info (FirstName,LastName) 
            VALUES ($user_fname,$user_lname)";
            mysql_query($row,$con);

            mysql_close($con);

            ?>

首选方式,不使用 JSON。您的帮助将不胜感激!!!

4

1 回答 1

0

我想问题出在

$row="INSERT INTO info (FirstName,LastName) 
        VALUES ($user_fname,$user_lname)";

将其更改为

$row= mysql_query("INSERT INTO info (FirstName,LastName)  VALUES('$user_fname', '$user_lname')");
if ($row) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "Product successfully created.";
    echo $row;
}
于 2013-05-23T17:45:48.633 回答