PHP将多个复选框和文本框数组插入MySQL数据库 [英] PHP inserting multiple checkbox AND textbox arrays into MySQL Database

查看:80
本文介绍了PHP将多个复选框和文本框数组插入MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在php中遇到数组结果问题.我的第一个问题是:

I'm having trouble with the array results in my php. My first problem is this :

即使未选中该复选框,它也会显示所有内容.我的第二个问题是,即使它已连接到数据库,它也不会将值插入数据库(如您在上一个屏幕快照中看到的那样,它说"connected success").

It shows everything even if the checkbox isn't checked. My second problem is that it won't insert the values to the database even though it's connected to the database (as you can see in the previous screenshot, it says "connected successfully").

这是我的html表单:

This is my html form:

<form method="POST">
    <input type="hidden" name="item[]" value="cupcake">
    <input type="text" name="items" value="cupcake" readonly><br>
    <b>Price :</b> <span name="price" value="3.00">$17.00</span><br>
    Quantity: <input tabindex="1" name="quantity[]" min="0" max="5" type="number" class="quantity" value="1" /><br>
    <input tabindex="1" name="checkbox[]" type="checkbox" value="17" /><span>Add to Cart</span></label></div></div></td><br>

    <input type="hidden" name="item[]" value="cake">
    <input type="text" name="items" value="cake" readonly><br>
    <b>Price :</b> <span name="price" value="20.00">$20.00</span><br>
    Quantity: <input tabindex="1" name="quantity[]" min="0" max="5" type="number" class="quantity" value="1" /><br>
    <input tabindex="1" name="checkbox[]" type="checkbox" value="20" /><span>Add to Cart</span></label></div></div></td><br>

    <input type="submit" name="insertBT"><br>
</form>

PHP:

if(isset($_POST['insertBT']))
{
    class db_conn
    {
        public function create_conn($servername, $username, $password, $db)
        {
            global $conn;
            $conn = new mysqli ($servername, $username, $password, $db);
        }

        public function check_conn()
        {
            global $conn;
            if($conn->connect_error)
            {
                die ("Connection Failed : " . $conn->connect_error);
            }
            else
            {
                echo ("Connected Successfully <br>");
            }
        }

        public function insert()
        {

            if(isset($_POST['checkbox'])) {
                foreach($_POST['checkbox'] as $check) {

                    $check = implode(',', $_POST['checkbox']);
                    $name = implode(',', $_POST['item']);
                    $quantity = implode(',', $_POST['quantity']);
                }
                echo $check . "<br>";
                echo $name . "<br>";
                echo $quantity . "<br>";

                mysql_query("INSERT INTO purchases(Product, Quantity, Price) VALUES('$name', '$quantity','$check')");

            }
        }
    }
    $obj1 = new db_conn;
    $obj1->create_conn("localhost","root","", "dbtest");
    $obj1->check_conn();
    $obj1->insert();
}

推荐答案

您不应该使用implode.这会将表单中所有内容的逗号分隔列表放入您要插入的每一行中,并对选中的每个框重复此操作.您应该通过索引数组在每行中插入一个项目.

You shouldn't be using implode. That puts a comma-separated list of everything in the form into each row that you insert, and repeats this for every box that's checked. You should just insert one item in each row, by indexing the arrays.

但是,当您在表单中有一个复选框时,它仅提交已选中的复选框.结果是$_POST['checkbox']数组的索引将与相应的$_POST['item']$_POST['quantity']元素不匹配.您需要在checkbox名称中放入显式索引,以便可以将它们关联起来.

However, when you have a checkbox in a form, it only submits the ones that are checked. The result of this is that the indexes of the $_POST['checkbox'] array won't match up with the corresponding $_POST['item'] and $_POST['quantity'] elements. You need to put explicit indexes into the checkbox names so you can relate them.

<form method = "POST">

<input type = "hidden" name = "item[]" value = "cupcake">
<input type = "text" name = "items" value = "cupcake" readonly><br>
<b>Price :</b> <span name = "price" value = "3.00">$17.00</span><br>
Quantity: <input tabindex="1" name="quantity[]" min="0" max="5" type="number" class="quantity" value="1" /><br>
<input tabindex="1" name="checkbox[0]" type="checkbox" value="17" /><span>Add to Cart</span></label></div></div></td><br>

<input type = "hidden" name = "item[]" value = "cake">
<input type = "text" name = "items" value = "cake" readonly><br>
<b>Price :</b> <span name = "price" value = "20.00">$20.00</span><br>
Quantity: <input tabindex="1" name="quantity[]" min="0" max="5" type="number" class="quantity" value="1" /><br>
<input tabindex="1" name="checkbox[1]" type="checkbox" value="20" /><span>Add to Cart</span></label></div></div></td><br>

<input type = "submit" name = "insertBT"><br>
</form>

然后您的PHP代码可以像这样:

Then your PHP code can be like this:

$stmt = $conn->prepare("INSERT INTO purchases (Product, Quantity, Price) VALUES (?, ?, ?)");
$stmt->bind_param("sis", $name, $quantity, $price);
foreach ($_POST['checkbox'] as $i => $price) {
    $name = $_POST['name'][$i];
    $quantity = $_POST['quantity'][$i];
    $stmt->execute();
}

顺便说一句,将价格放入HTML中似乎是个坏主意.没有什么可以阻止用户在提交表单之前使用Web检查器修改HTML,因此可以降低价格.处理表单时,您应该从数据库中获取价格.

BTW, putting the prices in your HTML seems like a bad idea. Nothing stops the user from modifying HTML using the web inspector before they submit the form, so they could lower the price. You should get the prices from the database when processing the form.

此外,请注意,在原始代码中,您使用MySQLi打开了数据库连接,但是随后您尝试使用mysql_query而不是$conn->query()进行插入.您不能混合这样的API. myql_query仅在通过mysql_connect打开连接时可以使用.

Also, notice that in your original code you opened the database connection using MySQLi, but then you tried to do the insert using mysql_query instead of $conn->query(). You can't mix APIs like that; myql_query can only be used when you open the connection with mysql_connect.

这篇关于PHP将多个复选框和文本框数组插入MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆