dongsu3664 2016-12-23 22:03
浏览 44
已采纳

html表单,PHP将数据插入数据库不工作?

I have HTML registration form when I submit the form the PHP code appears and data not insert to database i made my database using phpMyAdmin, what should I do?

Here my PHP code:

     <?php
        $con=mysqli_connect('localhost','root','');
        $db=mysqli_select_db($con,'research_sys');
    if ($con) {
        echo "good";
        }else {
        die('error');
        }
        if(isset($_POST['submit'])){
$Fname = mysqli_real_escape_string($con,$_POST["Fname"]);
$Lname = mysqli_real_escape_string($con,$_POST["Lname"]);
$email = mysqli_real_escape_string($con,$_POST['email']);
$password = mysqli_real_escape_string($con,$_POST['password']);
    $sql =  mysqli_query($con,"INSERT INTO `research_sys`.`researcher` (Re_fname,Re_lname,Re_mobile,Re_password) values ('$Fname','$Lname','$email','$password ')");

            if (mysqli_query($sql)){
            echo "insert";
            } else {
                echo "error" .$sql ."<br>". mysqli_error($con);
                }           
}
?>

here my registration HTML code

            <form method="post" action="connect.php">
                <legend class="center">Register </legend>
                <br>
                <div>

                    <input type="text" name="Fname" placeholder="First Name"/>
                </div>
                <div>

                    <input type="text" name="Lname" placeholder="Last Name"/>
                </div>

                <div>
                    <input type="text" name="email" placeholder="Email"/>
                </div>

                <div>
                    <input type="password" name="password" placeholder="Password"/>
                </div>

                <div>
                    <input type="password" name="con_password" placeholder="Password confirm"/>
                </div>
                <input type="submit" name="submit" value="submit"/>
            </form>
  • 写回答

1条回答 默认 最新

  • douyanzan9145 2016-12-23 22:05
    关注

    Look at the following:

    $sql =  mysqli_query($con,"INSERT INTO `research_sys`.`researcher` 
            ^^^^^^^^^^^^ function
    
            (Re_fname,Re_lname,Re_mobile,Re_password) 
            values ('$Fname','$Lname','$email','$password ')");
                                                         ^ space
    
        if (mysqli_query($sql)){
            ^^^^^^^^^^^^ function
    

    You're using that mysqli_query() function twice, remove one and just do:

    if ($sql){...}
    

    and mysqli_error($con) should have thrown you an error about it.

    If it didn't throw an error, then that may suggest you're using this as file:/// as opposed to http://localhost.

    Edit:

    "i have html registration form whin i submit the form the php code apears"

    That's because of what I wrote above before quoting you. You need to run this off a webserver with php/mysql installed and running properly and as http://localhost.

    Also, remove the space in this '$password '. That space counts as a character.

    • Double-check your column names also. There seems to be something that doesn't match (Re_fname,Re_lname,Re_mobile,Re_password) the Re_mobile and you're referencing an email '$email' in VALUES.

    You also seem to store plain text passwords; don't, it's not safe if you intend on going live with this. Use password_hash() and a prepared statement.


    Footnotes:

        $con=mysqli_connect('localhost','root','');
        $db=mysqli_select_db($con,'research_sys');
    

    You can shorten that to using all 4 arguments in mysqli_connect():

        $con=mysqli_connect('localhost','root', '', 'research_sys');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址