我有两个表employee表(父表)和employee_image表(子表)。
|=============================|
|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 |
|=====================================================|
在第一个insert.php表单中,我正在使用此脚本
<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>
并在第二页“commit.php”我正在使用此脚本
<?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";
}
}
?>
答案 0 :(得分:2)
替换此
mysql_query("insert into test_image values ('', '$imageName', '$imageData')");
使用:
$id = mysql_insert_id();
mysql_query("insert into test_image values ('', '$imageName', '$imageData','$id')");
答案 1 :(得分:0)
您可以通过两种方式获取最后插入的自动增量PK值 -
SELECT LAST_INSERT_ID()
Reference: mysqli_insert_id()
函数参考:对于方法 1。,您可以在MySQL中使用SELECT last_insert_id()
,查询将返回最后插入的ID
对于方法 2。,您可以使用mysqli_insert_id()
功能
以下是方法 2的示例。 * mysqli_*
函数用于以下示例*
$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";
}