表单数据不发布到数据库

时间:2016-07-28 12:33:20

标签: php mysqli

任何人都可以看到以下代码的错误。 当我使用HTML表单提交数据时,我按预期获得确认注册的确认。但是,当我检查数据库时,它不存在!

我已检查过每个输入的名称是否正确,并且可以确认数据库是否已成功连接,以及数据库列的名称等等...

非常感谢任何帮助。

<?php 

    if (isset($_POST['carrierinsert'])) {

    $acc = $_POST['acc'];  


   $sql = "SELECT cariers.acc FROM cariers WHERE cariers.acc ='$acc'";
   $result = $db->query($sql);

   if($result->num_rows > 0) {

        echo "<font color='red'>Carrier Already exits</font>"; 


  $sql = "INSERT INTO cariers ( `reg`, `acc`, `accstatus`, `carramid`, `carrfloor`, `pay`, `comms`) 
    VALUES ( '".$db->real_escape_string($_POST['reg'])."', 
             '".$db->real_escape_string($_POST['acc'])."', 
             '".$db->real_escape_string($_POST['accstatus'])."', 
             '".$db->real_escape_string($_POST['carramid'])."',       
             '".$db->real_escape_string($_POST['carrfloor'])."',  
             '".$db->real_escape_string($_POST['pay'])."',        
             '".$db->real_escape_string($_POST['comms'])."' )";


        $insert = $db->query($sql);

                    echo "<font color='red'>Carrier Inserted</font>";

        }
        }
         ?> 


                <h1>Add New</h1>
                <hr>




            <form method="post" action=""> Carrier<br />




            <input name="reg" type="text" required="required" value=""> 
            <br /><br />




            Number<br />
            <input name="acc" type="text" required="required" value="">
            <br /><br />



            Status<br />
            <input name="accstatus" type="text" required="required" value="">





            Floor<br />
            <select name="carrfloor" id="carrfloor">
            <option name="carrfloor" Value="1">1</option>
            <option name="carrfloor" Value="2">2</option>
            </select>
            <br /><br />




            Pay<br />
            <select name="pay" id="pay">
            <option selected="selected" name="pay" Value="0">N/A</option>
            <option name="pay" Value="1">1%</option>
            <option name="pay" Value="1.25">1.25%</option>
            <option name="pay" Value="1.5">1.5%</option>
            <option name="pay" Value="1.75">1.75%</option>
            <option name="pay" Value="2">2%</option>
            <option name="pay" Value="2.25">2.25%</option>
            <option name="pay" Value="2.50">2.50%</option>
            <option name="pay" Value="3.00">3%</option>
            </select>
            <br /><br />



            Additional Charge<br />
            <select name="comms" id="comms">
            <option name="comms" selected ="selected" Value="Fees">Yes</option>
            <option name="comms" Value="No Fees">No</option>
            </select>
            <br /><br />



            Manager<br />
            <select name="carramid" id="carramid">
            <option name="carramid" Value="Yes">Yes</option>
            <option name="carramid" Value="No">No</option>
            </select>
            <br /><br />


            <br /><br />
            <input type="submit" class="btn" name="carrierinsert" value="Save" />


            </form>

2 个答案:

答案 0 :(得分:0)

$sql = "INSERT INTO cariers ( reg, acc, accstatus, carramid, carrfloor, pay, comms) 
        VALUES ( '".{$db->real_escape_string($_POST['reg'])}."', 
                 '".{$db->real_escape_string($_POST['acc'])}."', 
                 '".{$db->real_escape_string($_POST['accstatus'])}."', 
                 '".{$db->real_escape_string($_POST['carramid'])}."',       
                 '".{$db->real_escape_string($_POST['carrfloor'])}."',  
                 '".{$db->real_escape_string($_POST['pay'])}."',        
                 '".{$db->real_escape_string($_POST['comms'])}."' )"; 

更新

$a = 15;

    echo 'this will not write 15: $a'
        .PHP_EOL.
        "this will write 15: $a"
        .PHP_EOL.
        "but i can escape a variable anyways. i dont need to change quotes".$a.' dot (.) is the add method for strings'
        ;

答案 1 :(得分:0)

试试这个:

$sql = "INSERT INTO cariers ( `reg`, `acc`, `accstatus`, `carramid`, `carrfloor`, `pay`, `comms`) 
        VALUES ( '".$db->real_escape_string($_POST['reg'])."', 
                 '".$db->real_escape_string($_POST['acc'])."', 
                 '".$db->real_escape_string($_POST['accstatus'])."', 
                 '".$db->real_escape_string($_POST['carramid'])."',       
                 '".$db->real_escape_string($_POST['carrfloor'])."',  
                 '".$db->real_escape_string($_POST['pay'])."',        
                 '".$db->real_escape_string($_POST['comms'])."' )";

<强> EDITTED: 试试这段代码

<?php 
    if (isset($_POST['carrierinsert'])) {
        $acc = $_POST['acc'];  
        $sql = "SELECT cariers.acc FROM cariers WHERE cariers.acc ='$acc'";
        $result = $db->query($sql);

        if($result->num_rows > 0) {
            echo "<font color='red'>Carrier Already exits</font>"; 
        }else{

            $sql = "INSERT INTO cariers ( `reg`, `acc`, `accstatus`, `carramid`, `carrfloor`, `pay`, `comms`)  VALUES ( '".$db->real_escape_string($_POST['reg'])."', '".$db->real_escape_string($_POST['acc'])."','".$db->real_escape_string($_POST['accstatus'])."','".$db->real_escape_string($_POST['carramid'])."', '".$db->real_escape_string($_POST['carrfloor'])."','".$db->real_escape_string($_POST['pay'])."','".$db->real_escape_string($_POST['comms'])."' )";
            $insert = $db->query($sql);
            echo "<font color='red'>Carrier Inserted</font>";
            }
        }
?> 


<h1>Add New</h1>
<hr>
<form method="post" action=""> Carrier<br />
        <input name="reg" type="text" required="required" value=""> 
        <br /><br />
    Number<br />
        <input name="acc" type="text" required="required" value="">
        <br /><br />
    Status<br />
        <input name="accstatus" type="text" required="required" value="">
    Floor<br />
        <select name="carrfloor" id="carrfloor">
            <option value="1">1</option>
            <option value="2">2</option>
        </select>
        <br /><br />
    Pay<br />
        <select name="pay" id="pay">
            <option selected="selected" value="0">N/A</option>
            <option value="1">1%</option>
            <option value="1.25">1.25%</option>
            <option value="1.5">1.5%</option>
            <option value="1.75">1.75%</option>
            <option value="2">2%</option>
            <option value="2.25">2.25%</option>
            <option value="2.50">2.50%</option>
            <option value="3.00">3%</option>
        </select>
    <br /><br />
    Additional Charge<br />
        <select name="comms" id="comms">
            <option selected ="selected" value="Fees">Yes</option>
            <option value="No Fees">No</option>
        </select>
        <br /><br />
    Manager<br />
        <select name="carramid" id="carramid">
            <option value="Yes">Yes</option>
            <option value="No">No</option>
        </select>
        <br /><br />
        <br /><br />
    <input type="submit" class="btn" name="carrierinsert" value="Save" />
</form>