文件类型输入在循环中不起作用


file type input not working in a loop

我正在尝试上传图片并在名为 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);
        }  

它只会保存文件夹的名称和一个斜杠,并且会产生错误。 请帮我解决这个问题

你的第一段代码有错误,我修复了它,它看起来像这样。此外,我将输入的名称(从 addcolor 更改为 addcolor[])以将它们与数组一起使用到您的第二段代码中(请参阅如何使用多维数组帖子):

<?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 } ?>