简体   繁体   English

执行查询以将数据从一个表复制到另一个表

[英]Execute query for copy data from one table to another

I am developing an android application. 我正在开发一个android应用程序。 The Login and Registration page of this application is connected with a php page and data is storing into mySql database through php page. 此应用程序的“登录和注册”页面与php页面连接,并且数据通过php页面存储到mySql数据库中。 Now, when the user clicks on Registration button, the data should be save in database through php. 现在,当用户单击“注册”按钮时,数据应通过php保存在数据库中。 This is working fine. 一切正常。 When the user clicks on Login then Username and password should be verify. 当用户单击登录时,应验证用户名和密码。 This one too working fine. 这太好了。 But When user clicks on Login, I want to copy data from one table to another table right after Username and Password verification. 但是,当用户单击登录时,我想在验证用户名和密码后立即将数据从一个表复制到另一表。 A flag should be set to '1' and sent to android. 标志应设置为“ 1”并发送到android。 But If I add query for copy data in php code, then none of the query is executing and Flag is not sending to android application. 但是,如果我在php代码中添加了对复制数据的查询,那么该查询就不会执行,并且Flag也不会发送到android应用。 If I comment $select3 Query ,its working fine.Please give me solution. 如果我评论$ select3 Query ,它工作正常。请给我解决方案。 I am completely new in php and mysql. 我是php和mysql的新手。 Look at the below code : 看下面的代码:

    <?php
    // Connection...

    $name = mysqli_real_escape_string($con, $_POST['Uname']); 
    $password =mysqli_real_escape_string($con, $_POST['Password']);

    $flag['code']=0;

    $select2="update Table1 SET TimeIN=NOW() where BINARY Uname=BINARY'$name' AND BINARY Password = BINARY'$password'";

    $select3 = "insert into Table2 (Uname,Password,Email,Mobile,IP_Address,TimeIN,TimeOUT)select   Uname,Password,Email,Mobile,IP_Address,Time,Timeout from Insert1 where BINARY Uname = BINARY '$name' AND BINARY Password = BINARY'$password';"

    $result=mysqli_query($con,"select * from Table1 where BINARY Uname = BINARY'$name' AND BINARY Password = BINARY'$password'"); 

    $s=mysqli_query($con,$select2);
    $t=mysqli_query($con,$select3);
    $num_rows = $result->num_rows;

    if($num_rows > 0) 
    { 
       $flag['code']=1;
    }
    print(json_encode($flag));
    mysqli_close($con);
?>

You can better use a Mysqli-object. 您可以更好地使用Mysqli对象。 for example: 例如:

$database = new mysqli(url, username, password, database);
$query = $database->stmt_init($select);
$query->execute();

But it is safer to use prepared statements: 但是使用准备好的语句更安全:

$query = $database->stmt_init();
$query->prepare("update Table1 SET TimeIN=NOW() where Uname=? AND BINARY Password = ?");
$query->bind_param("ss", $uname, $password); //with ss you say you will bind two strings to the question marks.
$query->execute();

to read the data: 读取数据:

$query = $database->stmt_init();
$query->prepare("select * from Table1 where Uname =? AND Password =?;");
$query->bind_param("ss", $uname, $password); 
$query->execute();
$rows = $query->num_rows;

I hope this will help you. 我希望这能帮到您。

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

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