Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么我的SQL语法中有一个错误作为未定义的索引_Php_Html_Sql - Fatal编程技术网

Php 为什么我的SQL语法中有一个错误作为未定义的索引

Php 为什么我的SQL语法中有一个错误作为未定义的索引,php,html,sql,Php,Html,Sql,我的php文件有问题。提交表单时,出现一个名为“我的PHP文件中的未定义索引”的错误,并且 错误:插入到付款中 (PID、PInvoice\u编号、p\u说明、单价、数量、总计)值(“”、“”、“”、“”、“”) 您的SQL语法有错误;检查手册 对应于要使用的正确语法的MariaDB服务器版本 第1行的“”附近、“”、“”) 尽管我试了好几次,还是找不到错误。我试图解决这个问题,但解决不了,所以请帮我解决这个问题 这是我的html表单 <body> <div c

我的php文件有问题。提交表单时,出现一个名为“我的PHP文件中的未定义索引”的错误,并且

错误:插入到
付款中
(PID、PInvoice\u编号、p\u说明、单价、数量、总计)值(“”、“”、“”、“”、“”) 您的SQL语法有错误;检查手册 对应于要使用的正确语法的MariaDB服务器版本 第1行的“”附近、“”、“”)

尽管我试了好几次,还是找不到错误。我试图解决这个问题,但解决不了,所以请帮我解决这个问题

这是我的html表单

<body>
        <div class-"logo">
        <img src="images/logo.png" width="150" height="130" align="left" alt="logo"/>
        
        <a href="admin.php">
        <img src="images/homebutton.png" width="130" height="130" align="right" alt="Home"/></a>
        </div>
        <br /><br /><br /><br /><br />
        
        <div class="form2">
        <pre>   <b><font size="+5">Payment</font></b></pre>
    
        
        <table>
        <form name="payment" align="center" action="payment_file.php" method="GET">
        
<tr><td>Invoice_no  &nbsp; &nbsp; &nbsp; </td><td> <input type="text" name="PInvoice_no" size="11" id="PInvoice_no" required/></td></tr>
            <tr><td>Payment description </td><td><input type="text" name="pay_description" size="50" id="p_description" required/></td></tr>
            <tr><td>Unit price  &nbsp; &nbsp; &nbsp; </td><td><input type="text" name="UP" size="5" id="unit_price"/> </td></tr>
            <tr><td>Quantity  &nbsp; &nbsp; &nbsp; </td><td><input type="number" name="quantity" size="20" id="quantity"/> </td></tr>
            <tr><td>Total </td><td><input type="text" name="total" size="10" id="total" required/></td></tr>
            <tr><td><br /></td><td> </td><td> &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp; &nbsp;   &nbsp; &nbsp; &nbsp;   </td></tr>
            <tr><td colspan="2"><input type="submit" name="Add" size="100" value="Add"/></td>
        </form>
        </table>
        
        </div>
</body>








付款 发票号 付款说明 单价 量 全部的
这是我的php文件

<div class-'logo'>
    <img src='images/logo.png' width='150' height='130' align='left' alt='logo'/>
    <a href='admin.php'>
        <img src='images/homebutton.png' width='130' height='130' align='right' alt='Home'/>
    </a>
</div>

<div class='form2'>

    <pre>
        <b><font size='+5'>Payment</font></b>
    </pre>

    <form name='payment' align='center' method='POST'><!-- action='payment_file.php'  -->
        <table>
            <tr>
                <td>PID</td>
                <td><input type='text' name='PID' size='11' value=23 required/></td>
            </tr>
            <tr>
                <td>Invoice_no</td>
                <td><input type='text' name='PInvoice_no' size='11' value=123456789 required/></td>
            </tr>
            <tr>
                <td>Payment description</td>
                <td><input type='text' name='pay_description' size='50' value='suspicious payment from a shady, underworld goblin' required/></td>
            </tr>
            <tr>
                <td>Unit price</td>
                <td><input type='text' name='unit_price' size='5' value=1000 /></td>
            </tr>
            <tr>
                <td>Quantity</td>
                <td><input type='number' name='quantity' size='20' value=23 /></td>
            </tr>
            <tr>
                <td>Total</td>
                <td><input type='text' name='total' size='10' required value=23000 /></td>
            </tr>
            <tr>
                <td colspan='2'><input type='submit' name='Add' size='100' value='Add'/>
            </td>
        </table>
    </form>
</div>

如果修改HTML,使其首先是有效的标记,然后将表单的方法设置为POST-like so:

<?php

    #session_start();
    #include('dbconnection.php');

    if( $_SERVER['REQUEST_METHOD']=='POST' && isset(
        $_POST['PID'],
        $_POST['PInvoice_no'],
        $_POST['pay_description'],
        $_POST['unit_price'],
        $_POST['quantity'],
        $_POST['total']
    ) ){
        
        $sql='INSERT into `payment` ( `PID`, `PInvoice_no`, `p_description`, `unit_price`, `quantity`, `total` ) VALUES ( ?, ?, ?, ?, ?, ? )';
        $stmt=$con->prepare( $sql );
        
        $stmt->bind_param('ssssss', $_POST['PID'], $_POST['PInvoice_no'], $_POST['pay_description'], $_POST['unit_price'], $_POST['quantity'], $_POST['total'] );
        $res=$stmt->execute();
        $stmt->close();
        
        exit( $res ? 'New record created successfully' : 'bogus' );
    }
?>
与使用多个
和/或

标记相比,您可能会发现css选项是一种更好的方法~当然会留下更干净的HTML代码


更新

根据您关于测试上述内容的评论,我意识到我遗漏了表单和php中的几个不一致之处

  • 表单中根本没有字段
    PID
  • 在PHP中,通过HTML ID而不是名称引用元素
由于通常不需要ID属性,因此我在这里从HTML中删除了它们,并修改了表单输入元素的名称,同时还为
PID
添加了一个新的属性。这已经用一个非常基本的表模式和上面的duffault数据进行了测试


在SQL语句中提供的变量中,您缺少一个以前的报价,该报价表示您的代码易受SQL注入的影响~use
prepared statements
而不是
method=“GET”
需要是
method=“POST”
。这就是为什么没有填充值的原因。请注意PHP代码是如何试图从$_POST中读取它们的?@ADyson-tbh我甚至从未查看过表单,但是HTML是无效的-您不能像这里那样将表单插入到表元素中。表单必须完全包含该表,或者表单本身必须完全包含在一个表中。cellI复制了这段代码并进行了尝试。它在提交后显示一个空白页,甚至没有显示任何错误。而且表单数据也不会保存到数据库中。我添加的
isset()
部分基于您在代码中分配的变量。查看表单,其中一些名称没有出现在表单中-是
NAME
属性,而不是出现在POST数组中的
ID
属性也没有表单元素,甚至几乎没有被称为
PID
它起作用了。我更正了NAME属性,并在表单中添加了PID,sql语法中也出现了一个错误,缺少一个逗号,然后它就可以工作了。
<?php

    #session_start();
    #include('dbconnection.php');

    if( $_SERVER['REQUEST_METHOD']=='POST' && isset(
        $_POST['PID'],
        $_POST['PInvoice_no'],
        $_POST['pay_description'],
        $_POST['unit_price'],
        $_POST['quantity'],
        $_POST['total']
    ) ){
        
        $sql='INSERT into `payment` ( `PID`, `PInvoice_no`, `p_description`, `unit_price`, `quantity`, `total` ) VALUES ( ?, ?, ?, ?, ?, ? )';
        $stmt=$con->prepare( $sql );
        
        $stmt->bind_param('ssssss', $_POST['PID'], $_POST['PInvoice_no'], $_POST['pay_description'], $_POST['unit_price'], $_POST['quantity'], $_POST['total'] );
        $res=$stmt->execute();
        $stmt->close();
        
        exit( $res ? 'New record created successfully' : 'bogus' );
    }
?>
mysql> describe payment;
+---------------+------------------+------+-----+---------+----------------+
| Field         | Type             | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| PID           | int(10) unsigned | NO   |     | 0       |                |
| PInvoice_no   | varchar(50)      | NO   |     | 0       |                |
| p_description | varchar(50)      | NO   |     | 0       |                |
| unit_price    | decimal(10,0)    | NO   |     | 0       |                |
| quantity      | int(10) unsigned | NO   |     | 0       |                |
| total         | decimal(10,0)    | NO   |     | 0       |                |
+---------------+------------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)


mysql> select * from payment;
+----+-----+-------------+----------------------------------------------------+------------+----------+-------+
| id | PID | PInvoice_no | p_description                                      | unit_price | quantity | total |
+----+-----+-------------+----------------------------------------------------+------------+----------+-------+
|  1 |  23 | 123456789   | suspicious payment from a shady, underworld goblin |      1000  |       23 | 23000 |
+----+-----+-------------+----------------------------------------------------+------------+----------+-------+
1 row in set (0.01 sec)