【问题标题】:PHP - 1 form, 2 tables - Can't insert data into table 1PHP - 1 个表格,2 个表格 - 无法将数据插入表格 1
【发布时间】:2015-10-30 05:03:36
【问题描述】:

我正在尝试制作一个发票系统,该系统从教程中添加行并且数据已正确插入到 tbl_orderdetail 中,但我无法将数据插入到 tbl_order 中。

这是 2 个 MYSQL 表:

CREATE TABLE IF NOT EXISTS `tbl_order` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`re_name` varchar(255) DEFAULT NULL,
`location` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `tbl_orderdetail` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) DEFAULT NULL,
`product_name` varchar(255) DEFAULT NULL,
`quantity` varchar(255) DEFAULT NULL,
`price` varchar(255) DEFAULT NULL,
`discount` int(11) DEFAULT NULL,
`amount` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=73 ;

这是我的连接代码:

<?php
$cn = mysql_connect('localhost','root','');
if($cn)
{
mysql_select_db('mydb',$cn);
}

if(isset($_POST['submit']))
$re_name = $_POST['re_name'];
$location = $_POST['location'];
{
mysql_query ("INSERT INTO tbl_order(re_name,location)        VALUES('{$_POST['re_name']}','{$_POST['location']}')");
$id = mysql_insert_id();
for($i = 0 ;$i < count($_POST['productname']);$i++)
{
    mysql_query("INSERT INTO tbl_orderdetail 
                SET order_id        = '{$id}', 
                    product_name    = '{$_POST['productname'][$i]}', 
                    quantity        = '{$_POST['quantity'][$i]}', 
                    price           = '{$_POST['price'][$i]}', 
                    discount        = '{$_POST['discount'][$i]}', 
                    amount          = '{$_POST['amount'][$i]}'
                ");
}
}
?>

我的代码有什么问题?

这是提交值的表单:

<form  action="" method="post"> 
<div class="box-body">
            <div class="form-group">
                ReceptName
                <input type="text" name="re_name" id="re_name" class="form-control">
            </div>
            <div class="form-group">
                Location
                <input type="text" name="location" id="location" class="form-control">
            </div>
            <input type="submit" class="btn btn-primary" name="submit" id="submit" value="Save Record">
    </div>

        <table class="table table-bordered table-hover">
            <thead>
                <th>No</th>
                <th>ProductName</th>
                <th>Quantity</th>
                <th>Price</th>
                <th>Discount</th>
                <th>Amount</th>
                <th><input type="button" value="+" id="add" class="btn btn-primary"></th>
            </thead>
            <tbody class="detail">
                <tr>
                    <td class="no">1</td>
                    <td><input type="text" class="form-control productname" name="productname[]"></td>
                    <td><input type="text" class="form-control quantity" name="quantity[]"></td>
                    <td><input type="text" class="form-control price" name="price[]"></td>
                    <td><input type="text" class="form-control discount" name="discount[]"></td>
                    <td><input type="text" class="form-control amount" name="amount[]"></td>
                    <td><a href="#" class="remove">Delete</td>
                </tr>
            </tbody>

            <tfoot>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th style="text-align:center;" class="total">0</th>
            </tfoot>
        </table>
    </div>      
          </div><!-- /.box -->
          </form> 
        </div><!--/.col (left) -->

【问题讨论】:

  • 发生了什么错误?你还需要格式化你的代码。
  • 没有写入错误,但唯一获得我输入的表是 tbl_orderdetail。表 tbl_order 始终为空。
  • 是否启用错误报告?
  • 是的,他们是,@Script47
  • 尝试使用 Chrome Logger 进行调试。您可以在 chrome 控制台上打印出值

标签: php mysql forms html-table


【解决方案1】:

谢谢大家帮助我。 我删除了 tbl_order 并创建了一个名为“orcamen”的新订单。

这是有效的 php 代码:

if(isset($_POST['submit']))
{
$razao     = $_POST["razao"];
$local      = $_POST["local"];
$condicao     = $_POST["condicao"];
$estado         = $_POST["estado"];
$material       = $_POST["material"];
$obs        = $_POST["obs"];
mysql_query
("INSERT INTO orcamen ( id , razao , local , condicao , estado , material , obs ) 
        VALUES ( NULL , '$razao', '$local', '$condicao', '$estado', $material', '$obs')");
$id = mysql_insert_id();
for($i = 0 ;$i < count($_POST['productname']);$i++)
{
    mysql_query("INSERT INTO tbl_orderdetail 
                SET order_id        = '{$id}', 
                    product_name    = '{$_POST['productname'][$i]}', 
                    quantity        = '{$_POST['quantity'][$i]}', 
                    price           = '{$_POST['price'][$i]}', 
                    discount        = '{$_POST['discount'][$i]}', 
                    amount          = '{$_POST['amount'][$i]}'
                ");
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-13
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多