我正在尝试上传图片并在名为images的文件夹中创建它的副本,但它在循环期间不起作用,你能帮我解决问题吗?这是我的代码;
$sql="SELECT * FROM product";
$q=$conn->query($sql);
while($r=$q->fetch(PDO::FETCH_ASSOC))
{
$code= $r['prod_code'];
<table class="table" width=200% >
<tr>
<th colspan=6 style="background-color: lightgray"> </th>
</tr>
<tr>
<th>Add variant </th>
</tr>
<tr>
<th>Image</th>
<th>color</th>
<th>Size</th>
<th>Size Type</th>
<th>Quantity</th>
<th>Action</th>
</tr>
<tr>
<td><input type="file" name="image" id="image" style="width: 80px" required /></td>
<form method="POST">
<td><input type="text" name="addcolor" class="form-control" style="width: 150px; border-radius: 0px" required/></td>
<td><input type="text" name="addsize" class="form-control" style="width: 150px; border-radius: 0px " required/></td>
<td><select name="addtype" class="form-control" style="width: 150px; border-radius: 0px" required ><option value="International">International </option>
<option value="US">US </option>
<option value="UK">UK</option>
</select></td>
<td><input type="text" name="addquantity" class="form-control" style="width: 150px; border-radius: 0px" required/></td>
<td><button type="submit" class="btn btn-success" name="addprod" value="<?php echo $code; ?>" ><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></button></td>
</form>
</tr>
</table>
}
继承人php:
if(isset($_POST['addprod'])){
$prodcode= $_POST['addprod'];
$color= $_POST['addcolor'];
$size= $_POST['addsize'];
$type= $_POST['addtype'];
$quantity= $_POST['addquantity'];
$image=addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name=addslashes($_FILES['image']['name']);
$image_size=getimagesize($_FILES['image']['tmp_name']);
move_uploaded_file($_FILES['image']['tmp_name'],"images/".$_FILES['image']['name']);
$location="images/".$_FILES['image']['name'];
$sql7 = "INSERT into color_variation (prod_code,color_pic,color,size,size_type,quantity) values('$prodcode','$location','$color','$size','$type','$quantity')";
$q7 = $conn->query($sql7);
}
它只会保存folde的名称斜杠,并产生错误。请帮帮我
答案 0 :(得分:1)
你的第一段代码中有错误,我修复了它,看起来像这样。另外,我更改了输入的名称(从addcolor
到addcolor[]
)以使用它们与数组一起使用到第二段代码中(请参阅如何work with multi-dimensional array post here):
<?php
$sql = "SELECT * FROM product";
$q = $conn->query($sql);
while ($r = $q->fetch(PDO::FETCH_ASSOC)) {
$code = $r['prod_code']; ?>
<table class="table" width=200%>
<tr>
<th colspan=6 style="background-color: lightgray"></th>
</tr>
<tr>
<th>Add variant</th>
</tr>
<tr>
<th>Image</th>
<th>color</th>
<th>Size</th>
<th>Size Type</th>
<th>Quantity</th>
<th>Action</th>
</tr>
<tr>
<td><input type="file" name="image[]" id="image" style="width: 80px" required/></td>
<form method="POST">
<td><input type="text" name="addcolor[]" class="form-control" style="width: 150px; border-radius: 0px"
required/></td>
<td><input type="text" name="addsize[]" class="form-control" style="width: 150px; border-radius: 0px "
required/></td>
<td><select name="addtype[]" class="form-control" style="width: 150px; border-radius: 0px" required>
<option value="International">International</option>
<option value="US">US</option>
<option value="UK">UK</option>
</select></td>
<td><input type="text" name="addquantity[]" class="form-control" style="width: 150px; border-radius: 0px"
required/></td>
<td>
<button type="submit" class="btn btn-success" name="addprod[]" value="<?php echo $code; ?>"><span
class="glyphicon glyphicon-ok" aria-hidden="true"></span></button>
</td>
</form>
</tr>
</table>
<?php } ?>