php - 多个图像插入在每次插入后额外占用一行

标签 php mysql

我在这里使用此 php 代码在数据库表的一个字段中插入多个图像,但在插入一行后每次插入后都会多一行.......帮我解决这个问题.. 谢谢提前。

<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
/*if(isset($_REQUEST['submit']))
{


         $pname=$_FILES['image']['name'];
         $tmp_name=$_FILES['image']['tmp_name'];
         move_uploaded_file($tmp_name."photo/".$pname);

        $fileext = pathinfo($pname, "photo/");
        $fileext = strtolower($fileext);

}*/

$uploads_dir = 'photo/';
//$images_name ="";
    foreach ($_FILES["image"]["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
            $tmp_name = $_FILES["image"]["tmp_name"][$key];
            $name = $_FILES["image"]["name"][$key];
            move_uploaded_file($tmp_name, "$uploads_dir/$name");
            $images_name =$images_name.$name.",";
        }
    }

    $sql=mysql_query("INSERT INTO multiimg(image) values('".$images_name."')");


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function addmore(num)
{
    if(num==1)
    {
        document.getElementById('field2').style.display='block';
        document.getElementById('ni1').style.display='block';
        return false;
    }
    else if(num==2)
    {
        document.getElementById('field3').style.display='block';
        return false;
    }

}
</script>
</head>

<body>
<form enctype="multipart/form-data" name="" action="" method="post">
    <div id="field1">Enter One Image :<input type="file" name="image[]" id="img1"/><a href="#" onclick="addmore(1)" id="ni1">addmore...</a></div>
    <div id="field2"  style="display:none;">Enter Two Image :<input type="file" name="image[]" id="img2"/><a href="#" onclick="addmore(2);">add more...</a></div>
    <div id="field3"  style="display:none;">Enter Three Image :<input type="file" name="image[]" id="img3"/><a href="#" onclick="addmore(3)" id="ni3">addmore...</a></div>
    <div id="field4" style="display:none">Enter Forth Image :<input type="file" name="image[]" id="img4"/><a href="#" onclick="addmore(4)" id="ni4">addmore...</a></div>

    <input type="submit" name="submit"/>

</form>

</body>
</html>

最佳答案

请检查表单是否已发布然后执行插入,否则在加载页面时将在表中插入一行数据为空

<?php
    mysql_connect("localhost","root","");
    mysql_select_db("test");

    if(isset($_POST['submit'])){

    $uploads_dir = 'photo/';
    //$images_name ="";
        foreach ($_FILES["image"]["error"] as $key => $error) {
            if ($error == UPLOAD_ERR_OK) {
                $tmp_name = $_FILES["image"]["tmp_name"][$key];
                $name = $_FILES["image"]["name"][$key];
                move_uploaded_file($tmp_name, "$uploads_dir/$name");
                $images_name =$images_name.$name.",";
            }
        }

        $sql=mysql_query("INSERT INTO multiimg(image) values('".$images_name."')");

    }
    ?>

关于php - 多个图像插入在每次插入后额外占用一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27540238/

相关文章:

php - PHP中不使用父类时是否称为多态性?

mysql - 在 Magento 中安全截断的表列表?

python - 将模型迁移到现有 MySQL 数据库时出现 Django 错误

php - 对于与我的设置不对应的字符集,排序规则无效

php - 以零开头的字符串/数字的正确格式?

php - laravel 中特定字段的自定义验证消息

mysql - Union MYSQL 中的 GROUP BY

mysql - node-mysql 一个查询中的多个语句

MySQL Workbench 6.0 和 Windows 8 - 无法连接到本地主机上的 MySql?

PHP 获取执行特定行的确切时间