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

时间:2014-05-28 07:41:25

标签: php mysql sql

我正在开发一个Android应用程序。此应用程序的登录和注册页面与php页面连接,数据通过php页面存储到mySql数据库中。现在,当用户点击Registration按钮时,数据应该通过php保存在数据库中。这工作正常。当用户单击Login时,应验证用户名和密码。这个太好了。但是当用户点击Login时,我想在用户名和密码验证后立即将数据从一个表复制到另一个表。标志应设置为' 1'并发送到android。但是如果我在php代码中添加复制数据的查询,那么查询都没有执行,并且Flag没有发送到Android应用程序。如果我评论 $ select3查询,它的工作正常。请给我解决方案。我是php和mysql的新手。请看下面的代码:

    <?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);
?>

1 个答案:

答案 0 :(得分:0)

您可以更好地使用Mysqli对象。例如:

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

但使用准备好的陈述更安全:

$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();

阅读数据:

$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;

我希望这会对你有所帮助。