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

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

问题描述

我的 php.ini 中的数组结果有问题.我的第一个问题是:

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

即使未选中复选框,它也会显示所有内容.我的第二个问题是,即使它已连接到数据库,它也不会将值插入到数据库中(如您在前面的屏幕截图中所见,它显示连接成功").

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天全站免登陆