我正在尝试使用pdo创建此表单,您可以在同一表单中插入数据和多个图像,目前,这是我的问题: HTML:
<form enctype="multipart/form-data" name="postHome" action="<?php echo htmlentities($_SERVER['PHP_SELF'])?>" method="post">
<table border="0" style="width: auto; line-height: 300%">
<p>
<tr>
<td><u>Advertisement Details:</u></td>
</tr>
<tr>
<td>Title:</td>
<td><input type="text" name="home_adTitle" width="500px" placeholder="Title" class="text-input" required="required"></td>
</tr>
<tr>
<td valign="top">Description:</td>
<td><textarea rows="5" cols="50" input type="text" name="home_description" width="400px"></textarea></td>
</tr>
</p>
<p>
<br>
<tr>
<td><u>Homestay Details:</u></td>
</tr>
<tr>
<td>Homestay Name:</td>
<td><input type="text" name="home_name" width="500px" placeholder="Homestay name" class="text-input"></td>
</tr>
<tr>
<td>Category: </td>
<td><select name="home_category">
<option value>Choose category</option>
<option value="1">Room</option>
<option value="2">Apartment</option>
<option value="3">Terrace</option>
<option value="4">Bungalow</option>
</select>
</td>
</tr>
<tr>
<td>Area: </td>
<td><select name="Area" required="required">
<option value>Choose area</option>
<option value="1">Area 1</option>
<option value="2">Area 2</option>
<option value="3">Area 3</option>
<option value="4">Area 4</option>
<option value="5">Area 5</option>
<option value="6">Area 6</option>
<option value="7">Area 7</option>
<option value="8">Area 8</option>
<option value="9">Area 9</option>
<option value="10">Area 10</option>
<option value="11">Area 11</option>
<option value="12">Area 12</option>
<option value="13">Area 13</option>
<option value="14">Area 14</option>
<option value="15">Area 15</option>
<option value="16">Area 16</option>
<option value="17">Area 17</option>
<option value="18">Area 18</option>
<option value="19">Area 19</option>
<option value="20">Area 20</option>
<option value="21">Area 21</option>
<option value="22">Area 22</option>
<option value="23">Area 23</option>
<option value="24">Area 24</option>
<option value="25">Area 25</option>
</select>
</td>
</tr>
<tr>
<td valign="top">Address:</td>
<td><textarea rows="1" cols="50" input type="text" name="home_add" width="400px" class="add" required="required"></textarea></td>
<tr>
<td valign="top"></td>
<td><textarea rows="1" cols="50" input type="text" name="home_add2" width="400px" class="add" required="required"></textarea></td>
<tr>
<td valign="top"></td>
<td><textarea rows="1" input type="text" name="home_add3" class="add" ></textarea></td>
</tr>
<tr>
<td>No. of room(s):</td>
<td><input type="number" name="no_room" width="60px" class="text2" required="required"> </td>
</tr>
<tr>
<td>Air-condition:</td>
<td><input type="radio" name="ac_avail" value="1">Yes <input type="radio" name="ac_avail" value="0">No</td>
</tr>
</p>
<p>
<tr>
<td>Contact No:</td>
<td><input type="text" name="home_tel" width="300px" placeholder="Contact No." class="phone" required="required"></td>
</tr>
<tr colspan=2>
<td>Rental: </td>
<td><b>RM </b><input type="number" name="price" width="50px" placeholder="00.00"></td>
</tr>
<tr>
<td>Deposit: </td>
<td><b>RM </b><input type="number" name="home_deposit" width="50px" placeholder="00.00" class="text"></td>
</tr>
</p>
<p>
<tr>
<td align="left">Images:</td>
<td><input type="file" id="file" name="file[]" width="200px" height="200px" multiple="multiple"></td>
</tr>
</table>
<br>
<input type="submit" name="postHome" value="POST" class="button large">
</p>
</form>
当我尝试上传它没有图像的PHP,它的成功,但当我插入图像php它成为 - &gt;警告:为foreach()提供的参数无效
PHP:
if(isset($_POST['postHome'])) //postHome name is taken from the name place for the submit button
{
$userid=$_SESSION['oid'];
$h_id=null;
$h_title= $_POST['home_adTitle'];
$h_name= $_POST['home_name'];
$h_category= $_POST['home_category'];
$h_add1= $_POST['home_add'];
$h_add2= $_POST['home_add2'];
$h_add3= $_POST['home_add3'];
$h_area= $_POST['area'];
$h_desc= $_POST['home_description'];
$h_price= $_POST['price'];
$h_deposit= $_POST['home_deposit'];
$h_tel = $_POST['home_tel'];
$h_ac = $_POST['ac_avail'];
$h_room = $_POST['no_room'];
$h_status = 1; //h_status: 1-available 0-unavailable
$query = $conn->prepare("SELECT home_name FROM homestay WHERE home_name=:home_name"); //2: select table from db. if use //3: prepare statement
$query->bindParam(':home_name', $h_name);
$query->execute();
if($query->rowCount() > 0)
{
echo 'existed already';
}
else
{
// Prepaire SQL Statement
$query = $conn->prepare("INSERT INTO homestay (home_id, home_name, ac_avail, no_room, price, home_category, home_tel, home_deposit, home_add, home_add2, home_add3, area, home_description, home_adTitle, status, o_id)
VALUES(null, :home_name, :ac_avail, :no_room, :price, :home_category, :home_tel, :home_deposit, :home_add, :home_add2, :home_add3, :area, :home_description, :home_adTitle, :status, :o_id)");
$query->bindParam(':home_name', $h_name);
$query->bindParam(':ac_avail', $h_ac);
$query->bindParam(':no_room', $h_room);
$query->bindParam(':price', $h_price);
$query->bindParam(':home_category', $h_category);
$query->bindParam(':home_tel', $h_tel);
$query->bindParam(':home_deposit', $h_deposit);
$query->bindParam(':home_add', $h_add1);
$query->bindParam(':home_add2', $h_add2);
$query->bindParam(':home_add3', $h_add3);
$query->bindParam(':area', $h_area);
$query->bindParam(':home_description', $h_desc);
$query->bindParam(':home_adTitle', $h_title);
$query->bindParam(':status', $h_status);
$query->bindParam(':o_id', $userid);
//IMAGE
function microtime_name()
{
list($usec, $sec) = explode(" ", microtime());
return ($usec + $sec)*10000;
}
$j=0;
foreach($_FILES['file']['tmp_name'] as $key=>$name_tmp)
{
$target_path='home_image/';
// Loop to get individual element from the array
$validextensions = array("jpeg", "jpg", "png"); // Extensions which are allowed.
$ext = explode('.', basename($_FILES['file']['name'][$key])); // Explode file name from dot(.)
$file_extension = end($ext); // Store extensions in the variable.
$namePic=microtime_name();
$target_path = $target_path . $namePic . "." . $ext[count($ext) - 1]; // Set the target path with a new name of image.
if (in_array($file_extension, $validextensions))//in_array check if value(in this case: $file_extension) exist in array or not(in this case: $validextensions)
{
if (move_uploaded_file($_FILES['file']['tmp_name'][$key], $target_path))
{
// If file moved to uploads folder.
//echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
$image_name=$target_path;
$stmt=$conn->prepare("INSERT INTO h_image(image_id, image_name, home_id) VALUES (null, :image_name, null)");
$stmt->bindParam(':pic_path', $image_name);
echo $image_name.': <br>';
$stmt->execute();
//if($stmt)
echo 'success!<br>';
}
else
{ // If File Was Not Moved.
echo $j. ').<span id="error">please try again!.</span><br/><br/>';
}
}
else
{ // If File Size And File Type Was Incorrect.
echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
}
}
if($query->execute())
{
$h_id=$conn->lastInsertId('home_id');
echo '<br>home id is:'.$h_id;
$test="select home_id from homestay";
$test=$conn->prepare($test);
$test->execute();
if($test->rowCount() > 0)
{
$r = $test->fetch(PDO::FETCH_LAZY);
}
}
}
}