Php 插入多行

Php 插入多行,php,mysql,arrays,loops,Php,Mysql,Arrays,Loops,我试图在我的数据库中插入多行,但该图像未保存到文件夹中, 我得到这个错误 Blockquote ArrayRay您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以了解在第1行的“jummery.jpg”)、(“hydangeas.jpg”)、(“hydangeas.jpg”)、(“jummery.jpg”)、(“hydangeas.j”附近使用的正确语法 这是我的代码: $hopid = $_POST[photo_hop_id]; $title = $_POST['photo_nam

我试图在我的数据库中插入多行,但该图像未保存到文件夹中,
我得到这个错误

Blockquote ArrayRay您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以了解在第1行的“jummery.jpg”)、(“hydangeas.jpg”)、(“hydangeas.jpg”)、(“jummery.jpg”)、(“hydangeas.j”附近使用的正确语法

这是我的代码:

$hopid = $_POST[photo_hop_id];
$title = $_POST['photo_name_id'];

if(!is_array($title)) {
    $title = array();
}

$titleds = "('" . implode("'), ('", $title) . "')";

$tmp_file = $_FILES['ne_photo_image']['tmp_name'];
$file = $_FILES['ne_photo_image']['name'];  

if(!is_array($tmp_file)) {
    $tmp_file = array();
}
if(!is_array($file)) {
    $file = array();
}

$sementara = "('" . implode("'), ('", $tmp_file) . "')";
$filed = "('" . implode("'), ('", $file) . "')";    

$tmp_file1 = $_FILES['fe_photo_image']['tmp_name'];
$file1 = $_FILES['fe_photo_image']['name']; 

if(!is_array($tmp_file1)) {
    $tmp_file = array();
}
if(!is_array($file1)) {
    $file = array();
}

$sementara1 = "('" . implode("'), ('", $tmp_file) . "')";
$filed1 = "('" . implode("'), ('", $file) . "')";   

if(!move_uploaded_file($sementara, 'image/' . $filed)) {
    echo $_FILES["ne_photo_image"]["error"];
}

if(!move_uploaded_file($sementara1, 'image/' . $filed1)) {
    echo $_FILES["fe_photo_image"]["error"];
}

$y = "INSERT INTO photo VALUES (null, '".$filed."', '".$filed1."', '".$hopid."', '".$titleds."')";
$z = mysql_query($y) or die (mysql_error());
if($z) {
    $msg = "Data sudah ditambahkan";
}
else {
    $msg = "Data tidak bisa dimasukkan";
}

echo print_r($y);
这是我的表格:

<form method="post" enctype="multipart/form-data">
    <table border="0"cellpadding="0" cellspacing="0" width= "100%">
        <tr>
            <td>Hop Name :<?echo "$data[hop_name]"?>
                <input type='hidden' name='photo_hop_id' value='<?echo"$data[hop_id]"?>'>
            </td>
        </tr>
        <table border="0"cellpadding="0" cellspacing="0" width= "100%">
            <tr>
                <td cellpadding="0" cellspacing="0" width= "50%"> 
                    Near End Site Name : <?echo "$data[ne_site_name]" ?>
                    </br>
                    Near End Site Id : <?echo "$data[ne_site_code]" ?>
                </td>
                <td cellpadding="0" cellspacing="0" width= "50%"> 
                    Far End Site Name : <?echo "$data[fe_site_name]" ?>
                    </br>
                    Far End Site Id : <?echo "$data[fe_site_code]"?>
                </td>
            </tr>   
            <tr>
                <td cellpadding="0" cellspacing="0" width= "50%"> 
                    <?  $pm1= mysql_query("SELECT photo_name FROM photo_name WHERE photo_name_id = 1");
                    $dpm1 = mysql_fetch_array ($pm1);echo"$dpm1[0]" ?> 
                    <input type='hidden' name='photo_name_id[]' value='<?echo"$dpm1[0]"?>'> :  
                    <input type="file" name="ne_photo_image[]">
                </td>
                <td cellpadding="0" cellspacing="0" width= "50%"> 
                    <?echo "$dpm1[0]"?> : <input type="file" name="fe_photo_image[]">
                </td>
            </tr>   
            <tr>
                <td cellpadding="0" cellspacing="0" width= "50%"> 
                    <? $pm1= mysql_query("SELECT photo_name FROM photo_name WHERE photo_name_id = 2");
                    $dpm1 = mysql_fetch_array ($pm1);echo"$dpm1[0]" ?> 
                    <input type='hidden' name='photo_name_id[]' value='<?echo"$dpm1[0]"?>'> :  
                    <input type="file" name="ne_photo_image[]">
                </td>
                <td cellpadding="0" cellspacing="0" width= "50%"> 
                    <?echo "$dpm1[0]"?> : <input type="file" name="fe_photo_image[]">
                </td>
            </tr>   
        </table>
    </table>

    <input type="submit" value="insert" />
</form>

跃点名称:

近端站点Id: 远端站点名称:
远端站点Id: : :

非常感谢您的帮助

删除
$filed
$filed1
$titleds

$y = "INSERT INTO photo VALUES (null, ".$filed.", ".$filed1.", '".$hopid."', ".$titleds.")";

不要使用mysql函数,因为它已被弃用。

您的错误显然来自以下几行:

$y = "INSERT INTO photo VALUES (null, '".$filed."', '".$filed1."', '".$hopid."', '".$titleds."')";
例如,您的var
$field
是一个内爆数组,看起来像
('foo'),('bar')
。最后,您的请求看起来像
插入到照片值中(null,('foo'),('bar'),[…]);

您必须在
$filed
$filed1
$hopid
$titleds
中转义简单的引号,但我很确定您的请求完全错误

您能给我们提供
photo
表格模式结构吗

编辑:

给出的结构:
(照片id、ne照片图片、fe照片图片、hop id、标题)

要在表中插入多行,必须使用以下语法:

INSERT INTO photo(`photo_id`, `ne_photo_image`, `fe_photo_image`, `hop_id`, `title`)
VALUES
    (null, 'foo', 'bar', 'baz', 'qux'),
    (null, 'foo', 'bar', 'baz', 'qux'),
    (null, 'foo', 'bar', 'baz', 'qux');
又名,您必须逐行插入数据,而不是逐列插入数据(您现在正在尝试的)

您的代码应该是这样的(这肯定不是完整的代码,我不确定是否理解您的所有代码,然后我给您一些跟踪):


在代码中,您正在将提交的数据分配给相应的变量。您正在检查这些变量是否为数组。如果不是,则使用
数组()分配该变量
。因此,您试图将其转换为不含元素的数组变量,因为当您执行该赋值时,您将覆盖任何现有值

因此,最后,如果提交的数据不是数组,那么数组中可能没有用于插入的任何元素

对于测试:

$abc = 'hi';
if(!is_array($abc))
{
    $abc = array();
}
print_r($abc); // $abc will be an empty array
另外,请阅读samsam和Alexander关于单引号的观点

另一件事是,
$\u POST
是一个关联数组。您必须将
键作为字符串传入。在第一行:
$hopid=$\u POST[photo\u hop\u id];
,它应该是这样的:
$hopid=$\u POST['photo\u hop\u id'];


当你想回显一个变量的值时,你可以这样做:
echo$variblename;
。在你的代码中:
,最好这样使用:
(也就是说,没有双引号,因为里面没有其他字符串).

您是否尝试回显
$y
以检查从代码填充的查询?是的,我回显$y,结果是:插入到照片值中(null,(“”,(“”,“,”(“”)1为什么SO上99%的问题代码完全不可读?@Codemonkey你是什么意思?@MeijuNainggolan:我的意思是,你的代码凌乱、缩进很差,而且完全没有任何有用的注释。我不是有意冒犯你,但你真的应该清理你的代码,让问题更容易被发现。我尝试了一下,我得到了这个:ArrayArray列计数与第1行的值计数不匹配不要仅发布错误消息,发布一些有用的调试信息。例如,您的结果查询我在数据库中得到了此结果:18(')('))0Nothing@MeijuNainggolan发布完整的查询。它应该类似于
插入照片值(NULL,'image.jpg','image2.jpg',2,'title')
你指的是表格结构吗?(照片id、ne\u照片图片、fe\u照片图片、hop\u id、标题)谢谢你的反馈人…我真的很抱歉现场:)
$abc = 'hi';
if(!is_array($abc))
{
    $abc = array();
}
print_r($abc); // $abc will be an empty array