PHP MySQL 插入数据库 [英] PHP MySQL Insert into database

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

问题描述

早上好!

我正在尝试制作一个登录页面,我们的员工可以在其中将产品添加到我们的发票中.我已经为您创建了一个测试页面,以便您可以轻松地遵循并了解此代码的想法.在页面上,我们的员工现在只能将产品添加到一张发票​​(因为它是一个测试页面),即 invoice_nr 2014002.我希望能够以一种形式添加多个产品.我使用 javascript 添加更多输入字段,但是每次我运行查询时,它只会插入最后一行.

I'm trying to make a login page where our employees can add products to our invoices. I've created a test page for you so you can follow easily and maybe see the idea of this code. On the page our employees can add products to only one invoice right now (because it's a test page) which is invoice_nr 2014002. I want to be able to add multiple products in one form.. I use javascript to add more input fields, but every time I run the query, then it's only inserting the last row.

这是我的 JavaScript 代码:

This is my JavaScript code:

<script type="text/javascript">
function myFunction()
{
var table = document.getElementById("products");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
cell1.innerHTML = '<td>Description <input type="text" name="description" value=""></td>';
cell2.innerHTML = '<td>Qty <input type="text" name="qty" value=""></td>';
cell3.innerHTML = '<td>Price <input type="text" name="price" value=""></td>';
cell4.innerHTML = '<td><input type="button" onclick="removeRow(this)" value="Remove"></td>';
}

removeRow = function(el) {
    $(el).parents("tr").remove()       
}
</script>

这是带有表单的 HTML:

This is the HTML with the form:

<h1>Add more products to invoice</h1>

<form action="" method="post">
    <table id="products">
        <tr>
            <td>Description <input type="text" name="description" value=""></td>
            <td>Qty <input type="text" name="qty" value=""></td>
            <td>Price <input type="text" name="price" value=""></td>
        </tr>
    </table>

    <br />
    <input type="button" onclick="myFunction()" name="addproduct" value="+ Add product"><br /><br />
    <input type="submit" name="addproduct" value="Submit new products">
</form>

最后,这是 PHP 代码..:

And last, this is the PHP code..:

require_once("conn.php");

if(isset($_POST['addproduct'])){
$description    = addslashes($_POST['description']);
$qty            = (int)$_POST['qty'];
$price          = addslashes($_POST['price']);

$db->query("INSERT INTO products SET
invoice_nr      = 2014002,
description     = '".$description."',
qty             = '".$qty."',
price           = '".$price."'
");
}

当然,我知道网站上的注入,但这只是我创建的一个基本测试页面,因为我不想发布太多代码让您以其他方式阅读.

And of course, I am aware of the injection on the site, but this is just a basic test page that I've created, because I didn't want to post too much code for you to read otherwise.

我希望它很容易理解 - 祝你有美好的一天 :)

I hope it's easy to understand - have a nice day peeps :)

推荐答案

可以生成不同名称的表单,例如:

You can generate the form with different names, for example:

<script type="text/javascript">
var i = 1;

function myFunction()
{
var table = document.getElementById("products");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
cell1.innerHTML = '<td>Description <input type="text" name="description'+i+'" value=""></td>';
cell2.innerHTML = '<td>Qty <input type="text" name="qty'+i+'" value=""></td>';
cell3.innerHTML = '<td>Price <input type="text" name="price'+i+'" value=""></td>';
cell4.innerHTML = '<td><input type="button" onclick="removeRow(this)" value="Remove"></td>';

i++;
}

removeRow = function(el) {
    $(el).parents("tr").remove()       
}
</script>

然后在 php 中,您可以遍历所有名称:

And then in php you can just iterate through all names:

$i=1; $values = '';
while(isset($_POST['description'.$i])) {
 $i++;
$values .= "($_POST[description$i],$_POST[qty$i],$_POST[price$i])"; // add escaping
    }

mysql_query("insert into table_name (column1,column2,column3,...)
VALUES $values");

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

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