PHP如何使用预准备语句将数据插入到mysql中的多个表中

时间:2015-05-11 06:01:40

标签: php database mysqli prepared-statement

我是PHP的新手,我有两张表如下。

表1 - 信息

字段 - id,name,upload

表2 - 文件存储

字段 - 上传

我想使用预准备语句将数据插入这两个表中。无论用户在表1中上传了该文件,它都应存储在表2中。

我收到此错误:无法准备声明

conn.php

<?php
$host = "localhost";
$user = "root";
$pwd  = "root";
$db   = "test";
$mysqli = new mysqli($host,$user,$pwd,$db);
/* ESTABLISH CONNECTION */
if (mysqli_connect_errno())
{
echo "Failed to connect to mysql : " . mysqli_connect_error();
exit();
}
?>

Add.php

// Connect to your db.
include 'conn.php';
$stmt = $mysqli->prepare("
            BEGIN;
INSERT INTO `info` (`id`, `name`, `upload`) VALUES(?, ?, ?);
INSERT INTO `filestore`(`upload`) VALUES(LAST_INSERT_ID());
COMMIT;
");
if(!$stmt)
{
    echo "Unable to prepare statement";
}
else
{
    $stmt->bind_param("iss", $id, $name, $upload);
        if($stmt->execute())
{
    echo "Successfully saved to db"; ?> <br> <?php
}
else
{
    die("Unable to save to db");
}
}
$stmt->close();

$mysqli->close();
    // Move the uploaded file to the server.
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $DIR)) 
{ ?> <br> <?php
echo "File is uploaded successfully...";
}
else
{ ?> <br> <?php
echo "Error in uploading a file....."; ?> <br> <?php
echo "Here is some debugging info";
print_r($_FILES);
}
?>

0 个答案:

没有答案