【问题标题】:insert multiple row插入多行
【发布时间】:2012-08-01 10:10:49
【问题描述】:

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

Blockquote ArrayArray 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 'Chrysanthemum.jpg')、('Hydrangeas.jpg')'、'('Chrysanthemum.jpg')、('Hydrangeas.j' 行附近使用正确的语法1

这是我的代码:

$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>

非常感谢您的帮助

【问题讨论】:

  • 您是否尝试过回显$y 来检查从代码中填充的查询?
  • 是的,我回应 $y,我得到了这个:INSERT INTO photo VALUES (null, '('')', '('')', '', '('')')1
  • 为什么 99% 的 SO 上有问题的代码完全不可读?
  • @Codemonkey 你是什么意思?
  • @MeijuNainggolan:我的意思是你的代码很乱,缩进很差,而且绝对没有任何有用的 cmets。我没有任何冒犯的意思,但您确实应该清理您的代码,以便更容易发现问题。

标签: php mysql arrays loops


【解决方案1】:

您的错误显然来自这一行:

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

例如,您的 var $filed 是一个内爆数组,看起来像 ('foo'), ('bar')。最后,您的请求将类似于INSERT INTO photo VALUE (null, '('foo'), ('bar')', [...]);

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

你能给我们photoschema结构吗?

编辑:

给定结构:(photo_id,ne_photo_image,fe_photo_image,hop_id,title)

要在表中插入多行,您必须使用这种语法:

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');

也就是说,您必须逐行插入数据,而不是逐列插入数据(您现在正在尝试的操作)。

你的代码应该是这样的(这绝对不是一段完整的代码,我不确定是否看懂了你所有的代码,然后我给你一些跟踪):

<?php 
   $hopid = $_POST['photo_hop_id'];

   $titles = $_POST['photo_name_id'];
   $ne_photo_images = $_FILES['ne_photo_image']['tmp_name'];
   $fe_photo_images = $_FILES['fe_photo_image']['tmp_name'];

   /*
   I assume $titles, $ne_photo_images and $fe_photo_images 
   have the same number of elements, in the right order.
   */

   $sql = "INSERT INTO photo(`photo_id`, `ne_photo_image`, `fe_photo_image`, `hop_id`,    `title`) VALUES";
   for($i = 0, $l = sizeof($titles) ; $i < $l ; $i++)
   {
      //adding row datas
      $sql .= " (null, 
                 '".$ne_photo_images [$i]."', 
                 '".$fe_photo_images [$i]."', 
                 '".$hopid."', 
                 '".$titles[$i]."')";
      if($i < $l - 1)
         $sql .= ",";
   }

   if(mysql_query($sql))
      //get happy
   else
      //take a coffee
?>

同样,这段代码远非完美,我只是想向您解释如何解决这个问题。

【讨论】:

  • 你的意思是表结构吗? (photo_id,ne_photo_image,fe_photo_image,hop_id,title)
【解决方案2】:

删除$filed$filed1$titleds 周围的撇号

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

自从 mysql_* 函数被弃用后,永远不要使用它。

【讨论】:

  • 我试了一下,我得到了这个:ArrayArrayColumn 计数与第 1 行的值计数不匹配
  • 不要只发布错误信息,发布一些有用的调试信息。例如,您生成的查询
  • 我在我的数据库中得到了这个结果:18 (') (') 0 Nothing
  • @MeijuNainggolan 发布您的完整查询。应该是INSERT INTO photo VALUES (NULL, 'image.jpg', 'image2.jpg', 2, 'title')
【解决方案3】:

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

所以最后,如果您提交的数据不是数组,则数组中可能没有任何元素用于插入!

用于测试:

$abc = 'hi';
if(!is_array($abc))
{
    $abc = array();
}
print_r($abc); // $abc will be an empty array

另外,请阅读 samsam 和 Alexander 指出的关于单引号的内容。

另外,$_POST 是一个关联数组。而且您必须将key 作为字符串传递。在你的第一行:$hopid = $_POST[photo_hop_id];,应该是这样的:$hopid = $_POST['photo_hop_id'];

当你想回显一个变量的值时,你可以简单地这样做:echo $variblename;。在您的代码中:&lt;?echo "$dpm1[0]"?&gt;,最好像这样使用它:&lt;?echo $dpm1[0]?&gt;(即,没有双引号,因为其中没有其他字符串)。

【讨论】:

  • 感谢反馈的人......我真的很抱歉现场:)
猜你喜欢
  • 1970-01-01
  • 2013-10-31
  • 2012-08-14
  • 2012-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多