[英]fetch id from one table and store it into another using php
I have a 2 page form. 我有一个2页的表格。 First page takes input from user regarding property ans stores it in
retailer_add_property
table. 第一页从用户那里获取有关属性的输入,并将其存储在
retailer_add_property
表中。 after this page the user is redirected to next page admin_add_property_images.php
where user can store multiple images (table name is propertyimages
)of the property that was entered in the previous page. 在此页面之后,用户将重定向到下一页
admin_add_property_images.php
,用户可以在其中存储上一页中输入的属性的多个图像(表名称为propertyimages
)。 what I am trying to do is to fetch the last inserted id of retailer_add_property
table which will be created in the first form and store it in the propertyimages table 我想要做的是获取将以第一种形式创建的
retailer_add_property
表的最后插入的ID,并将其存储在propertyimages表中
Form one: admin_add_property.php 表格一: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>
"admin_insert_property.php" “admin_insert_property.php”
<?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);
?>
Second Form: admin_add_property_images.php 第二种形式: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 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";
}
?>
i tried to carry id from one page to next, but it didn't worked out the way i wanted. 我试图将ID从一页移到下一页,但是并没有按照我想要的方式解决。 an someone tell me how can it be done
有人告诉我怎么做
use mysqli_insert_id($con)
fucntion save it in $_SESSION['']
and use it on any page with by adding session_start();
使用
mysqli_insert_id($con)
功能将其保存在$_SESSION['']
并通过添加session_start();
在任何页面上使用它session_start();
at the top. 在顶部。
**Note:**use mysqli_insert_id() function after your insert query; **注意:**在插入查询后使用mysqli_insert_id()函数;
look at your quesiton part below 看看下面的问题部分
**File Name:**admin_insert_property.php" **文件名:** admin_insert_property.php”
Just look at the query secion there. 只需查看那里的查询部分。 it has to be like this
它必须像这样
$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");
Now you can use $_SESSION['insert_id'];
现在您可以使用
$_SESSION['insert_id'];
at any page, if you use session_start();
在任何页面上,如果您使用
session_start();
at the top 在顶部
check this link for more info http://php.net/manual/en/mysqli.insert-id.php 检查此链接以获取更多信息http://php.net/manual/en/mysqli.insert-id.php
use $mysqli->insert_id
in admin_insert_property.php
page and modify code like this and get that id on a dmin_add_property_images.php
page and use it as per your requirement. 在
admin_insert_property.php
页面中使用$mysqli->insert_id
并修改这样的代码,并在dmin_add_property_images.php
页面上获取该ID,并根据需要使用它。
mysqli_query($con, $sql);
$lastId = $mysqli->insert_id;
header("Location: admin_add_property_images.php?id=".$lastId);
You need to make use of the $_GET variable. 您需要使用$ _GET变量。 After the first insert just add in the header the id of the inserted item.
第一次插入后,只需在标题中添加插入项的ID。
header("Location: admin_add_property_images.php?id=". $inserted_id;
This way you can pass it to other pages. 这样,您可以将其传递到其他页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.