我有一個2頁的表單。第一頁從用戶接收關於財產和商店的輸入retailer_add_property
表。在該頁面之後,用戶被重定向到下一頁admin_add_property_images.php
,其中用戶可以存儲在前一頁中輸入的屬性的多個圖像(表名是propertyimages
)。我所試圖做的是獲取將在第一種形式創建retailer_add_property
表的最後插入的ID,並將其存儲在propertyimages表從一個表中獲取id並使用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」
<?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,但它並沒有制定出我想要的方式。一個人告訴我怎麼可以做
所有這些工作都是在用戶登錄後完成的,所以我使用admin_session創建了登錄系統,我可以存儲該會話中的id值或我必須創建另一個? – Sam 2014-10-10 10:45:08
k,我試過了,但當我試圖以第二種形式回顯id的值時,我得到的值爲'0' – Sam 2014-10-10 11:04:00
我已經做了多個條目並在每個條目後檢查了值,無論是第一條還是第五條id 0的值只有 – Sam 2014-10-10 11:08:30