当前位置: 首页 > 知识库问答 >
问题:

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

空英达
2023-03-14

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

错误:在付款(PID,PInvoice_no,p_description,unit_price,quantity,total)值('','','','','')中插入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文件

<?php
    session_start();
    include('dbconnection.php');

        $PID = $_POST['PID'];
        $PInvoice_no = $_POST['PInvoice_no'];
        $p_description = $_POST['p_description'];
        $unit_price = $_POST['unit_price'];
        $quantity = $_POST['quantity'];
        $total = $_POST['total'];
         
          
        $sql="INSERT into `payment` (PID,PInvoice_no,p_description,unit_price,quantity,total)VALUES('$PID','$PInvoice_no','$p_description',$unit_price','$quantity','$total')";
        
        
        if (mysqli_query($con, $sql)) {
          echo "New record created successfully";
        } else {
          echo "Error: " . $sql . "<br>" . mysqli_error($con);
        }
        
    mysqli_close($con);

?>

共有1个答案

朱天逸
2023-03-14

如果您修改HTML使其首先是有效的标记,然后将表单的方法设置为POST类,则如下所示:

<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>
    
    <div class='form2'>
    
        <pre>
            <b><font size='+5'>Payment</font></b>
        </pre>
    
        <form name='payment' align='center' action='payment_file.php' method='POST'>
            <table>
                <tr>
                    <td>Invoice_no</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</td>
                    <td><input type='text' name='UP' size='5' id='unit_price'/></td>
                </tr>
                <tr>
                    <td>Quantity</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 colspan='2'><input type='submit' name='Add' size='100' value='Add'/>
                </td>
            </table>
        </form>
    </div>
</body>

然后,您应该将PHP更改为使用prepared statement来解决缺少引号的问题,并帮助减轻SQL注入攻击。

<?php

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

    if( $_SERVER['REQUEST_METHOD']=='POST' && isset(
        $_POST['PID'],
        $_POST['PInvoice_no'],
        $_POST['p_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['p_description'],$_POST['unit_price'],$_POST['quantity'],$_POST['total'] );
        $res=$stmt->execute();
        $stmt->close();
        exit( $res ? 'New record created successfully' : 'bogus' );
    }
?>

比起使用多个 &或
标记,您可能会发现css选项是一种更好的方法~当然会留下更干净的HTML代码。

 类似资料: