i have two table employee table(parent table) and employee_image table(child table).
|=============================|
|employee_id employee_name |
| 1 ram |
| 2 sham |
| 3 dany |
| 4 james |
|=============================|
|=====================================================|
|image_id name image employee_id |
| 1 img_one [BLOB-22.3KiB] 2 |
| 2 img_two [BLOB-24.5KiB] 3 |
| 3 img_three [BLOB-32.2KiB] 3 |
| 4 img_four [BLOB-18.6KiB] 3 |
| 5 img_five [BLOB-17.7KiB] 4 |
|=====================================================|
On first insert.php form i am using this script
<html>
<head>
<title>
</title>
</head>
<body>
<form action="commit.php" method="post" enctype="multipart/form-data">
<table>
<tr><td>employee name : </td><td><input type ="text" name ="employee_name"/></td></tr>
<tr><td>upload image 1 : </td><td><input type="file" name="image"></td></tr>
<tr><td>upload image 2 : </td><td><input type="file" name="image"></td></tr>
<tr><td>upload image 3 : </td><td><input type="file" name="image"></td></tr>
<tr><td><input type ="submit" name="submit" value ="submit"/></td></tr>
</table>
</body>
</html>
and in second page "commit.php" i am using this script
<?php
$db=mysql_connect('localhost', 'root', '') or
die ('Unable to connect. Check your connection paramerters.');
mysql_select_db('my_db', $db) or die(mysql_error($db));
$query ='INSERT INTO employee_tbl
(employee_name)
VALUES
("' . $_POST['employee_name'] . '")';
mysql_query($query, $db) or die(mysql_error($db));
echo 'employee name inserted succecfully!';
$imageName=mysql_real_escape_string($_FILES["image"]["name"]);
$imageData=mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
$imageType=mysql_real_escape_string($_FILES["image"]["type"]);
if(substr($imageType,0,5) == "image")
{
mysql_query("insert into test_image values ('', '$imageName', '$imageData')");
echo "image uploaded";
}
else
{
echo "only images are allowed";
}
}
?>
Replace this
mysql_query("insert into test_image values ('', '$imageName', '$imageData')");
With:
$id = mysql_insert_id();
mysql_query("insert into test_image values ('', '$imageName', '$imageData','$id')");
You can get the last inserted auto increment PK value by two ways -
SELECT LAST_INSERT_ID()
Reference: mysqli_insert_id()
function Reference: For the method 1. , you can use SELECT last_insert_id()
in MySQL and the query will return the last inserted ID
For the method 2. you can use mysqli_insert_id()
function
Here is the example of method 2. * mysqli_*
functions are used in the following example *
$link = mysqli_connect("localhost", "root", "", "my_db")
or die(mysqli_error());
$query ='INSERT INTO employee_tbl
(employee_name)
VALUES
("' . $_POST['employee_name'] . '")';
mysqli_query($link, $query) or die(mysqli_error());
echo 'employee name inserted succecfully!';
$imageName=mysqli_real_escape_string($link, $_FILES["image"]["name"]);
$imageData=mysqli_real_escape_string($link, file_get_contents($_FILES["image"]["tmp_name"]));
$imageType=mysqli_real_escape_string($link, $_FILES["image"]["type"]);
if(substr($imageType,0,5) == "image")
{
$id = mysqli_insert_id($link);
mysqli_query($link, "insert into test_image values ('', '$imageName', '$imageData', '$id')");
echo "image uploaded";
}
else
{
echo "only images are allowed";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.