0

我想使用我选择并回显的行再次插入到表中。所以我想要的是 row 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'];}
4

1 回答 1

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'];}
于 2013-08-17T20:43:32.047 回答