简体   繁体   English

PHP MySQL INSERT查询和PHP似乎运行了两次

[英]PHP MySQL INSERT Query & PHP seems to be run twice

I want to make simple login server for android. 我想为Android创建简单的登录服务器。

Login feature works well, but in registration has some problem. 登录功能效果很好,但是在注册时存在一些问题。

My intent is : 我的意图是:

  1. Android Client Send User Info(id, username, password) to XAMPP Server by using HTTPPOST. Android客户端使用HTTPPOST将用户信息(ID,用户名,密码)发送到XAMPP服务器。

  2. Server get User Info, and find repetition id or username from database(using select query). 服务器获取用户信息,并从数据库中查找重复ID或用户名(使用选择查询)。 if repetition exists, response to client using echo. 如果存在重复,则使用echo对客户端进行响应。

  3. if repetition not exists, make new user info to database using insert query. 如果不存在重复,请使用插入查询将新的用户信息添加到数据库。 And response to client using echo. 并使用echo响应客户端。

If repetition exists, it works well. 如果存在重复,则效果很好。 server response correctly. 服务器正确响应。

but in case of no repetition, query's output is 1 then server response 'there is repetition'... 但是在没有重复的情况下,查询的输出为1,则服务器响应为“有重复” ...

login.php is same code with register.php, but there is no insert query in login.php, and it works perfectly well. login.php与register.php是相同的代码,但是login.php中没有插入查询,它运行得很好。

so I think this problem caused by insert query. 所以我认为这个问题是由插入查询引起的。

here is my code: 这是我的代码:

<?php
$hostname_localhost ="localhost";
$database_localhost ="mydatabase";
$username_localhost ="root";
$password_localhost ="";
$id_localhost ="";

$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost, $id_localhost) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_localhost, $localhost);

$username = $_POST['username'];
$password = $_POST['password'];
$id = $_POST['id'];

$query_search = "select * from tbl_user where username = '".$username."' OR id = '".$id."'";
$query_exec = mysql_query($query_search) or die(mysql_error());
$rows = mysql_num_rows($query_exec);

if($rows == 0) { 
    $query_register = "INSERT INTO tbl_user (id, username, password) VALUES ('$id', '$username', '$password')";
    $result = mysql_query($query_register) or die(mysql_error());
    if($result == TRUE){
        echo "Register Success";
    } else {
        echo "Register Fail";
    }
    exit;
    die;
} else {
    echo "Registered Device or Username";
    exit;
}
?>

login.php is almost same. login.php几乎相同。 only search query(select id and username and password same) and insert query is different(no insert in login.php). 仅搜索查询(选择ID和用户名和密码相同),而插入查询则不同(在login.php中不插入)。

server response is wrong but insert action is doing right. 服务器响应错误,但插入操作正确。 I can see new record in my database. 我可以在数据库中看到新记录。 so... it seems to be run twice. 所以...似乎要运行两次。

client side has no problem because it also almost same with login code(And login works well with no insert query:)). 客户端没有问题,因为它也几乎与登录代码相同(并且无需插入查询即可登录:)。

Update 更新资料

mysql_num_rows return an int, you are checking if $rows is false instead of 0. Use === . mysql_num_rows返回一个int,正在检查$ rows是否为false而不是0。使用===


What id are you referring to in your tbl_user and why would the user supply their ID? 什么id是你指的是在tbl_user和为什么用户提供他们的ID? Are you sure that you are checking for the right id ? 您确定要检查正确的id吗? It feels like you should be doing a separate query for the device id OR check for a device_id column in the table tbl_user . 感觉您应该对设备ID进行单独查询,或者检查表tbl_userdevice_id列。

Other things to look into 其他需要研究的东西

exit; and die; die; are the same so calling it twice makes no sense. 是相同的,所以两次调用都没有意义。

if ($rows == 0) is checking for (bool) false instead of the integer 0 . if ($rows == 0)正在检查(bool) false而不是整数0 Use === for checking for supplied type. 使用===检查提供的类型。

When using "" double-quotation marks you can supply the variables directly; 当使用“”双引号时,可以直接提供变量。 Eg "...WHERE username = '$username'" instead of "... WHERE username = '".$username."'" . 例如"...WHERE username = '$username'"而不是"... WHERE username = '".$username."'"

Look into using mysqli instead of mysql for more features and improved security; 考虑使用mysqli代替mysql以获得更多功能和更高的安全性; http://php.net/manual/en/book.mysqli.php http://php.net/manual/zh/book.mysqli.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM