2

I'm new to PHP, I have two tables as follows.

Table 1 - info

Fields - id, name ,upload

Table 2 - filestore

Fields - upload

I want to insert data into these two tables using prepared statement. Whatever the user uploaded the file in table 1 it should also be stored in table 2.

I'm getting this Error : Unable to prepare statement

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);
}
?>
MAK
  • 41
  • 6
  • possible duplicate of [MySQLi: How to insert into multiple tables with prepared statements](http://stackoverflow.com/questions/25080636/mysqli-how-to-insert-into-multiple-tables-with-prepared-statements) – Leena May 11 '15 at 06:04

0 Answers0