简体   繁体   中英

Execute query for copy data from one table to another

I am developing an android application. The Login and Registration page of this application is connected with a php page and data is storing into mySql database through php page. Now, when the user clicks on Registration button, the data should be save in database through 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. 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. If I comment $select3 Query ,its working fine.Please give me solution. I am completely new in php and 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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