插入选定的行mysql

时间:2013-08-17 20:34:49

标签: php mysqli

我想使用我选择并回显的行再次插入表格。 所以我想要的是行to_user。我该如何定义$to_user来获取to_user行?

$to_user = $row['to_user']; //this gives me undefined variable error

$stmt = $mydb->prepare("insert into `messages`(`to_user`) values(?)");
echo $mydb->error;
$stmt->bind_param('s', $to_user);
$stmt->execute();

$stmt = $mydb->prepare("SELECT * FROM messages where id = ? ");
 $stmt->bind_param('s', $id);
 $stmt->execute();
     $result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {
echo $row['to_user'];}

1 个答案:

答案 0 :(得分:0)

$row['to_user'];

$stmt = $mydb->prepare("insert into `messages`(`to_user`) values(?)");
echo $mydb->error;
$stmt->bind_param('s', $row['tu_user']);
$stmt->execute();
//get the last inserted id and store it in a var
$lastInserted = $mydb->lastInsertId();

        $stmt = $mydb->prepare("SELECT * FROM messages where id = ? ");
         $stmt->bind_param('s', $lastInserted);
         $stmt->execute();
             $result = $stmt->get_result();
//for testing
        //print_r($result);
       // echo $result;
        while ($row = $result->fetch_assoc()) {
        echo $row['to_user'];}