我该如何解决这个问题,即数据无法传输到数据库表中

时间:2018-11-25 05:45:35

标签: php mysql forms

我已经创建了这个注册表单,我已经使用了databse作为表单数据,但是当我单击“提交”按钮时,数据没有写到数据库中,有人可以帮我吗?没有收到任何错误,只是表格中的值没有被传送到数据库表中。

php代码:

$servername = "localhost";
$username = "root";
$password = "";   
$dbname = "society123";

$conn = new mysqli($servername,$username,$password,$dbname); 

if($conn->connect_error){ die ("connection failed:" . $conn->connect_error);}

if(isset($_POST["submit"])){
$FNname = $_POST["FName"];
$MNname = $_POST["MName"];
$LNname = $_POST["LName"];
$Gender = $_POST["Gender"];
$add = $_POST["add"];
$occu = $_POST["occu"];
$Resi = $_POST["Resi"];
$eid = $_POST["eid"];
$pass = $_POST["pass"];

$sql = "INSERT INTO 
 member(firstname,middlename,lastname,gender,address,
occupation,residence,email,password) 
VALUES($FNname,$MName,$LName,$Gender,$add,$occu,$Resi,$eid,$pass)";

if($conn->query($sql) === TRUE){

echo "record succesfully entered";
}    
 else{

 echo "error" . $sql . "<br>" . $conn->error;
 }    
  $conn->close();    

}    html代码:

    <form method="post" action="register.php">  
    <center><h1 class="text-uppercase mb-0">Registration</h1></center>
    <hr class="star-dark mb-5">
    <table class="registration1">    
        <tr>
            <td style="padding-right: 60px;">Firstname:<input type="text" 
        name="FName" placeholder=" Firstname" value="<?php echo $FName;?>"><span><?php echo $FNameerr;?></span></td> 
            <td style="padding-right: 60px;">Middlename:<input type="text" name="MName" placeholder=" Middlename" value="<?php echo $MName;?>"><span><?php echo $MNameerr;?></span></td>
            <td style="padding-right: 60px;">Lastname:<input type="text" name="LName" placeholder=" Lastname" value="<?php echo $LName;?>"><span><?php echo $LNameerr;?></span></td>
        </tr>
        <tr style="padding-top: 30px;">
            <td style="padding-top: 30px;">Gender: Male<input type="radio" name="Gender" <?php if(isset($Gender) && $Gender == "male") ?> value="male"></td>
            <td style="padding-top: 30px;">Female<input type="radio" name="Gender"<?php if(isset($Gender) && $Gender == "female") ;?> value="female"></td>
            <td style="padding-top: 30px;">other<input type="radio" name="Gender"<?php if(isset($Gender) && $Gender == "other") ;?> value="other"><span><?php echo $Gendererr;?></span></td>            
        </tr>
        <tr>
            <td style="padding-top: 30px;">Address: <input type="text" name="add" placeholder="Type your address here" size="30" value="<?php echo $add;?>"><span><?php echo $adderr;?></span></td>
            <td style="padding-top: 30px;">Occupation<input type="text" name="occu" placeholder=" occupation" value="<?php echo $occu;?>"><span><?php echo $occuerr;?></span></td>                
        </tr>
        <tr>
            <td style="padding-top: 30px;">Residence type: self owned<input type="radio" name="Resi"<?php if(isset($Resi) && $Resi == "selfowned") ?> value="selfowned"></td>
            <td style="padding-top: 30px;"> Rented<input type="radio" name="Resi"<?php if(isset($resi) && $Resi == "rented") ?> value="rented"><span><?php echo $Resierr;?></span></td>    
        </tr>
        <tr>
            <td style="padding-top: 30px;">E-mail id:<input type="text" name="eid" placeholder=" e-mail id" value="<?php echo $eid;?>"><span><?php echo $eiderr;?></span></td>
            <td style="padding-top: 30px;">password:<input type="password" name="pass" placeholder=" password" value="<?php echo $pass;?>"><span><?php echo $passerr;?></span></td>
        </tr>
        <tr>
            <td><input type="submit" name="submit" value="submit" ></td>
        </tr>
    </table>
</form>

1 个答案:

答案 0 :(得分:-4)

尝试一下... 而不是使用    $ FNname = $ _POST [“ FName”]; 尝试:     $ FNname = mysqli_real_escape_string($ con,$ _POST ['FName']);

使$ con与我的连接变量相同。这应该可以帮助您发布和传输数据。您可能还需要用查询结果变量替换isset('submit')。如果这样做,请在后面的else语句中包含die和mysqli_errno。确保引用您的字符串。

我希望这会有所帮助。

编辑: 同样在此之上: if($ conn-> query($ sql)=== TRUE)

您需要事先调用查询。

添加: $ results = mysqli_query($ conn,$ sql);

然后在if语句中采用条件并将其更改为: if($ results)

这可以确保结果实际上是在查询之后添加的。

祝你好运!

*很抱歉有任何错别字...我正在用手机接听。