我有一个2页的表格。第一页从用户获取有关属性的输入,并将其存储在retailer_add_property
表中。在此页面之后,用户被重定向到下一页admin_add_property_images.php
,其中用户可以存储在上一页中输入的属性的多个图像(表名为propertyimages
)。我想要做的是获取最后插入的retailer_add_property
表的id,它将在第一个表单中创建并存储在propertyimages表中
表格一:admin_add_property.php
<form class="form-horizontal" role="form" action="admin_insert_property.php" enctype="multipart/form-data" method="post">
<div class="form-group">
<label class="col-lg-3 control-label">Property name:</label>
<div class="col-lg-8">
<input class="form-control" name="propertyname" value="" type="text" required>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Property Type:</label>
<div class="col-md-8">
<select name="cancellation" id="cancellation" required>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="submit">
<input class="btn btn-primary" value="Save " type="submit" name="submit">
<span></span>
</div>
</div>
</form>
&#34; admin_insert_property.php&#34;
<?php
include('admin_session.php');
$con=mysqli_connect("localhost","qwe","pwd","qwe");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$propertyname = mysqli_real_escape_string($con, $_POST['propertyname']);
$propertytype = mysqli_real_escape_string($con, $_POST['propertytype']);
$sql="INSERT INTO retailer_add_property (propertyname,propertytype) VALUES ('$propertyname','$propertytype')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
mysqli_query($con, $sql);
header("Location: admin_add_property_images.php");
mysqli_close($con);
?>
第二种形式:admin_add_property_images.php
<form class="form-horizontal" role="form" action="admin_insert_property_images.php" enctype="multipart/form-data" method="post">
<div class="form-group">
<label class="col-md-3 control-label">Upload Image:</label>
<div class="col-md-8">
<input class="form-control" name="file" id="file" value="" type="file" required>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="submit">
<input class="btn btn-primary" value="Save " type="submit" name="submit">
</div>
</div>
</form>
admin_insert_property_images.php
<?php
include('admin_session.php');
$con=mysqli_connect("localhost","qwe","pwd","qwe");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Replace $mysqli with your $con then. $con->query($sql);
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
if (file_exists("propertyimages/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
$imagepath = "propertyimages/" . $_FILES["file"]["name"];
move_uploaded_file($_FILES["file"]["tmp_name"], $imagepath);
$sql="INSERT INTO propertyimages(propertyimage) VALUES ('".$imagepath."')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
}
}
}
else
{
echo "Invalid file";
}
?>
我试图将id从一个页面带到下一页,但它并没有按照我想要的方式解决。有人告诉我怎么做呢
答案 0 :(得分:2)
使用mysqli_insert_id($con)
功能将其保存在$_SESSION['']
中,并通过在顶部添加session_start();
在任何页面上使用它。
**注意:**插入查询后使用mysqli_insert_id()函数;
看看下面的问题部分
**文件名:** admin_insert_property.php“
看看那里的查询部分。它必须像这样
$sql="INSERT INTO retailer_add_property (propertyname,propertytype) VALUES ('$propertyname','$propertytype')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
mysqli_query($con, $sql);
$_SESSION['insert_id']=mysqli_insert_id($con);
//save the value in the $_SESSION
header("Location: admin_add_property_images.php");
现在,您可以在任何页面使用$_SESSION['insert_id'];
在顶部使用session_start();
查看此链接以获取更多信息http://php.net/manual/en/mysqli.insert-id.php
答案 1 :(得分:0)
在$mysqli->insert_id
页面中使用admin_insert_property.php
并修改此类代码并在dmin_add_property_images.php
页面上获取该ID并根据您的要求使用它。
mysqli_query($con, $sql);
$lastId = $mysqli->insert_id;
header("Location: admin_add_property_images.php?id=".$lastId);
答案 2 :(得分:0)
您需要使用$ _GET变量。 在第一次插入后,只需在标题中添加插入项的id。
header("Location: admin_add_property_images.php?id=". $inserted_id;
通过这种方式,您可以将其传递给其他网页。