-2

我有一个像下面这样的小 php 页面,我想从此插入返回“auto-incremented_id”。

唯一的要求是我可以从 android 应用程序中读取数字。我确定我可以查一下,但是有没有SQL我可以检查成功的代码代码会返回它?

这是php:

<?php

//Make connection
$con = mysqli_connect('xxxxx', 'xxxxxxx', 'xxxxx');


//check connection
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

//change db to andriodnfp db
mysqli_select_db($con, 'andriodnfp');

$table = 'USER';

$fbid   = htmlspecialchars($_GET["fbid"]);
$social = htmlspecialchars($_GET["social"]);


$name = htmlspecialchars($_GET["name"]);
$name = !empty($name) ? "'$name'" : "NULL";

$fname = htmlspecialchars($_GET["fname"]);
$fname = !empty($fname) ? "'$fname'" : "NULL";

$username = htmlspecialchars($_GET["username"]);
$username = !empty($username) ? "'$username'" : "NULL";

$email = htmlspecialchars($_GET["email"]);
$email = !empty($email) ? "'$email'" : "NULL";

$picture = htmlspecialchars($_GET["picture"]);
$picture = !empty($picture) ? "'$picture'" : "NULL";

$other = htmlspecialchars($_GET["other"]);
$other = !empty($other) ? "'$other'" : "NULL";

if (!$fbid == '') {

    if (!mysqli_query($con, 'INSERT INTO ' . $table . ' ( facebookID, social_outlet, Name, first_name, username, email, picture, significant_other) VALUES ("' . $fbid . '","' . $social . '","' . $name . '","' . $fname . '","' . $username . '","' . $email . '","' . $picture . '","' . $other . '")')) {
        printf("Errormessage: %s\n", mysqli_error($con));
    };
}

mysqli_close($con);

//$posts = array($json);
$posts = array(
    1
);
header('Content-type: application/json');
echo json_encode(array(
    'posts' => $posts
));

?>
4

2 回答 2

3

尝试这个:mysqli_insert_id

if (mysqli_query($con, 'INSERT INTO '.$table.' ( facebookID, social_outlet, Name, first_name, username, email, picture, significant_other) VALUES ("'.$fbid.'","'.$social.'","'.$name.'","'.$fname.'","'.$username.'","'.$email.'","'.$picture.'","'.$other.'")') === false) {
    printf("Errormessage: %s\n", mysqli_error($con));
    die();
}
$id = mysqli_insert_id($con);

你也应该die在错误之后。

此外,您还应该将查询与数据类型进行比较===,这样可以确保它确实失败了,而不是返回一个评估为假的值。- 但它不太可能为插入返回一些东西,对吗?

假设您在数据库中有一个如下所示的表:

id, username, user_level
1, dave, 0
2, jcaruso, 1

假设user_level是一个整数,(0 = 用户,1 = 管理员)。做一个选择,比如SELECT user_level FROM table WHERE id=1会返回0,如果你将它与 比较==,那将是真的。0==false.

于 2013-06-20T05:00:19.880 回答
1
<?php 
//Make connection
$con = mysqli_connect('xxxxx','xxxxxxx','xxxxx') ;


//check connection
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

//change db to andriodnfp db
mysqli_select_db($con, 'andriodnfp');

$table= 'USER';

$fbid = htmlspecialchars($_GET["fbid"]);
$social = htmlspecialchars($_GET["social"]);


$name = htmlspecialchars($_GET["name"]);
$name = !empty($name) ? "'$name'" : "NULL";

$fname = htmlspecialchars($_GET["fname"]);
$fname = !empty($fname) ? "'$fname'" : "NULL";

$username = htmlspecialchars($_GET["username"]);
$username = !empty($username) ? "'$username'" : "NULL";

$email = htmlspecialchars($_GET["email"]);
$email = !empty($email) ? "'$email'" : "NULL";

$picture = htmlspecialchars($_GET["picture"]);
$picture = !empty($picture) ? "'$picture'" : "NULL";

$other = htmlspecialchars($_GET["other"]);
$other = !empty($other) ? "'$other'" : "NULL";

//$posts = array($json);
$posts = array(1);

if (!$fbid == '') {

    if (!mysqli_query($con, 'INSERT INTO '.$table.' ( facebookID, social_outlet, Name, first_name, username, email, picture, significant_other) VALUES ("'.$fbid.'","'.$social.'","'.$name.'","'.$fname.'","'.$username.'","'.$email.'","'.$picture.'","'.$other.'")')) {
        printf("Errormessage: %s\n", mysqli_error($con));
    };

    $auto_id = 'autoincramented_id';

    //passyour primary key here
    $cql  = "SELECT MAX($auto_id) AS primary_id FROM {$table}";

    $result = mysqli_query($con, $cql);

    $id_result = mysql_fetch_assoc($result);

    //$posts = array($json);
    $posts = array('auto_increment_id'=>$id_result['primary_id']);


}

mysqli_close($con);


header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));

?>
于 2013-06-20T04:57:01.607 回答