用PHP在多行MySQL表上划分数组内容

时间:2014-07-26 08:03:55

标签: php mysql arrays

我几乎设法拆分不同的数组并在MySQL数据库的表中准备它们,我将解释这种情况:

在主页面上,用户可以在表格中添加和删除行。每行的表格带有这些输入:

input1.name = "product[]";

input2.name = "seller[]";

input3.name = "description[]";

input4.name = "quantity[]";

input5.name = "priece[]";

因此,如果用户在每个数组中插入两行,则会包含两个产品的描述,例如:

产品:" PS3"," PS4&#34 ;;

卖家:" AMAZON"," SONY&#34 ;;

描述:" 100Gb"," 200Gb&#34 ;;

数量:" 1"," 2&#34 ;;

价格:" 100"," 200&#34 ;;

这是布局表:

http://www.mediafire.com/view/ux0su8ssdixfmgc/Cattura2.JPG

问题出现了。我捕获通过帖子输入的数据,但我不能在几行上分发这些数据。我希望你把PS3都放到MySQL表的第一行,而PS4放在表的第二行。到目前为止,数组仅在第一行进行实例化,但是,只有一种产品。因此,需要在阵列的适当行中准备每个盒子。我不知道我是否清楚,但我想实现这样的目标:

http://www.mediafire.com/view/d6f6ahy834jv0p2/Cattura.JPG

显然,表I中的数据是手动输入的,而不是通过代码输入的。你理解是对的吗? 这是我目前用于在不同行上发送多个数组的代码,但它不起作用。

if(isset($_POST['sending']))
    {
        if($_POST['sending'] == "save")
        {
            $row_data = array();
            foreach($_POST['sending'] as $key => $value) 
            { 
                $product=mysqli_real_escape_string($con,($_POST['product'][$row]));
                $seller=mysqli_real_escape_string($con,($_POST['seller'][$row]));
                $description=mysqli_real_escape_string($con,($_POST['description'][$row]));
                $quantity=mysqli_real_escape_string($con,($_POST['quantity'][$row]));
                $priece=mysqli_real_escape_string($con,($_POST['priece'][$row]));
                $user=mysqli_real_escape_string($con,($_POST['user'][$row]));
                $row_data[] = "('$product', '$seller', '$description','$quantity', '$priece', '$user')";
            }
                if (!empty($row_data))
                {
                    $sql = 'INSERT INTO test(product,seller,description,quantity,priece,user) VALUES '.implode(',', $row_data);
                    $result = mysqli_query($con, $sql );

                    if ($result)
                    echo 'ADD COMPLETE!: ' . mysqli_affected_rows($con);
                    else
                    echo 'ERROR' ;
                }           
        } // if ($_POST['sending'] == "save")
    } // if (isset($_POST['sending'])) 
}//close method

1 个答案:

答案 0 :(得分:0)

如果我理解得很好,这就是它应该如何运作

if(isset($_POST['sending']))
{
    if($_POST['sending'] == "save")
    {
        $row_data = array();
        foreach($_POST['sending'] as $key => $value) 
        { 
            $product=mysqli_real_escape_string($con,($_POST['product'][$row]));
            $seller=mysqli_real_escape_string($con,($_POST['seller'][$row]));
            $description=mysqli_real_escape_string($con,($_POST['description'][$row]));
            $quantity=mysqli_real_escape_string($con,($_POST['quantity'][$row]));
            $priece=mysqli_real_escape_string($con,($_POST['priece'][$row]));
            $user=mysqli_real_escape_string($con,($_POST['user'][$row]));
            array_push($row_data, "('$product', '$seller', '$description','$quantity', '$priece', '$user')");
        }

foreach($row_data as $value){
            if (!empty($value))
            {
                $sql = 'INSERT INTO test(product,seller,description,quantity,priece,user) VALUES '.$value;
                $result = mysqli_query($con, $sql );

                if ($result)
                echo 'ADD COMPLETE!: ' . mysqli_affected_rows($con);
                else
                echo 'ERROR' ;
            }           
    } // if ($_POST['sending'] == "save")
  } // if (isset($_POST['sending'])) 
}//close method