dongmeng1868 2017-04-10 08:44
浏览 12
已采纳

插入数据库不起作用

Hi Im working on a school project and i cant insert this into the database

What am I doring wrong? I dont have any error`s so I im stuck. Please help me. I do have a connection to the database. But the things I insert into the form do not show in to the databse.

$servername = "localhost";
$username = "root";
$password = "root123";
$dbname = "ToetsPro";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
    $query = "INSERT INTO `opleiding` (`id`,
                                   `opleiding`,
                                   `locatie`) 
              VALUES              (NULL,
                                   '".$_POST["opleiding"]."',
                                   '".$_POST["locatie"]."');";
    //echo $query; exit();                     
    $result = mysqli_query($conn, $query);

    $id = mysqli_insert_id($conn);

?>
<!DOCTYPE HTML> 

<form id="register" action="" method="post">
    <table>
        <tr>
            <td>Opleiding: </td>
            <td><input type="text" name="opleiding"></td>
        </tr>
        <tr>
            <td>Locatie: </td>
            <td><input type="text" name="locatie"></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" name="submit"></td>
        </tr>
    </table>
</form>

<a href="index.php">Terug naar de homepage</a>
  • 写回答

2条回答 默认 最新

  • doucou19961205 2017-04-10 09:29
    关注

    What am I doring wrong? I dont have any error`s so I im stuck.

    well there are few things you doing wrong.

    one of the very first things I have noticed is that you are mixing up Object oriented style and Procedural style doing so might confuse you in the long run.

    here your db connection

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

    You using mysqli object oriented style

    Then here : $result = mysqli_query($conn, $query); You using procedural style. I suggest that you only stick with one style, in that way you can easily read,organize and maintain your code.

    two : You might be making an error with your insert statement, id then inserting a null on the ID that might be the problem, if your id is an auto_increment better not even include it within your query.

    three You are writing a dangerous code, that will harm your application in the long run. you are directly inject $_POST values in your query, that might dangerous and it leave your application wide open to sql injections

    You should learn to use prepared statements, with mysqli or PDO prepared statements.

    This is how your code should look :

    <?php
    $servername = "localhost";
    $username   = "root";
    $password   = "root123";
    $dbname     = "ToetsPro";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    
    if (isset($_POST['submit'])) {
        //check and validate your inputs
    
        $opleiding = $_POST['opleiding'];
        $locatie   = $_POST['locatie'];
    
    
        $query = $conn->prepare("INSERT INTO `opleiding` (opleiding,locatie)VALUES(?,?)");
        $query->bind_param("ss", $opleiding, $locatie);
    
        if ($query->execute()) {
    
            echo "success";
            $id->insert_id;
        } else {
    
            echo "Error :" . $conn->error;
        }
    }
    
    ?>
    <!DOCTYPE HTML> 
    
    <form id="register" action="" method="post">
        <table>
            <tr>
                <td>Opleiding: </td>
                <td><input type="text" name="opleiding"></td>
            </tr>
            <tr>
                <td>Locatie: </td>
                <td><input type="text" name="locatie"></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" name="submit"></td>
            </tr>
        </table>
    </form>
    
    <a href="index.php">Terug naar de homepage</a>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?