doumianfeng5065 2017-03-11 07:01 采纳率: 0%
浏览 30
已采纳

代码是正确的,但我无法将数据输入数据库

I'm a beginner in php. The code seemed right but when I try to insert the data from the form to the database, it doesn't enter to the database. What is wrong with my code?

Here's my code

addemployee.php

echo '<form action="addemployee.php" method="POST">';
echo '<table align ="center">';

    echo '<tr><td>First Name: </td> <td><input type="text" name="first" pattern="[A-Za-z]{1,}" title="A-Z only" required></td> </tr>';
    echo '<tr><td>Middle Initial: </td><td><input type="text" name="middle"></td></tr>';
    echo '<tr><td>Last Name:</td><td> <input type="text" name="last"></td></tr>';
    echo '<tr><td>Contact Number:</td><td> <input type="tel" name="contact" maxlength="11"></td></tr>';
    echo '<tr> <td>Province: </td><td> <input type="text" name="provincee"></td></tr>';
    echo '<tr> <td>City: </td><td> <input type="text" name="cityy"></td></tr>';
    echo '<tr> <td>Username:</td><td> <input type="email" name="usernamee"></td></tr>';
    echo '<tr><td>Password:</td><td> <input type="password" name="pass"></td></tr>';

echo '</table>';
echo '<br> <input type="Submit" name="submitt">';

    if(isset($_POST['submitt'])){
    $firstname=$_POST['first'];
    $middleinitial=$_POST['middle'];
    $lastname=$_POST['last'];
    $contactnumber=$_POST['contact'];
    $province= $_POST['provincee'];
    $city =$_POST['cityy'];
    $username=$_POST['usernamee'];
    $password=$_POST['pass']; 
    $type= 'employee';

 $query=("INSERT INTO usertbl (fname,middeinitial,lname,contactnum,province,city,username,password,type) VALUES ('$firstname','$middleinitial','$lastname','$contactnumber','$province',
  '$city','$username','$password','$type')");
  mysqli_query($db, $query);
}
echo '</form>';
  • 写回答

1条回答 默认 最新

  • doumao6048 2017-03-11 07:24
    关注

    Please check the changes:-

    addemployee.php

    <!-- use html form not php generated form -->
    
    <form action="addemployee.php" method="POST">
        <table align ="center">
            <tr><td>First Name: </td> <td><input type="text" name="first" pattern="[A-Za-z]{1,}" title="A-Z only" required></td> </tr>
            <tr><td>Middle Initial: </td><td><input type="text" name="middle"></td></tr>
            <tr><td>Last Name:</td><td> <input type="text" name="last"></td></tr>
            <tr><td>Contact Number:</td><td> <input type="tel" name="contact" maxlength="11"></td></tr>
            <tr> <td>Province: </td><td> <input type="text" name="provincee"></td></tr>
            <tr> <td>City: </td><td> <input type="text" name="cityy"></td></tr>
            <tr> <td>Username:</td><td> <input type="email" name="usernamee"></td></tr>
            <tr><td>Password:</td><td> <input type="password" name="pass"></td></tr>
        </table>
        <br>
        <input type="Submit" name="submitt">
    </form>
     <?php
        error_reporting(E_ALL); // check all type of error
        ini_set('display_errors',1); // display those errors
        if(isset($_POST['submitt'])){
            $firstname=$_POST['first'];
            $middleinitial=$_POST['middle'];
            $lastname=$_POST['last'];
            $contactnumber=$_POST['contact'];
            $province= $_POST['provincee'];
            $city =$_POST['cityy'];
            $username=$_POST['usernamee'];
            $password=$_POST['pass']; 
            $type= 'employee';
    
            $db = mysqli_connect ('hostname','username','password','dbname') or die(mysqli_connect_error()); // provide your details here
    
            $query=("INSERT INTO usertbl (fname,middeinitial,lname,contactnum,province,city,username,password,type) VALUES ('$firstname','$middleinitial','$lastname','$contactnumber','$province',
      '$city','$username','$password','$type')");
            if(mysqli_query($db, $query)){
               echo "Inserted successfully";
            }else{
               echo "error:-".mysqli_error($db);
            }
        }
    
    ?>
    

    Note:- your code is open to SQLInjection. So read about prepared statements of mysqli_* Or PDO and use them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划