代码之家  ›  专栏  ›  技术社区  ›  thanos_zach

使用php将表单数据发送到mysqli DTB失败(xampp环境)

  •  0
  • thanos_zach  · 技术社区  · 7 年前

    这是我的html表单代码:问题是我不知道如何使用xampp成功地将表单提交到mysql DTB。(数据不会发送到DTB)。

        <!DOCTYPE html>
        <html>
        <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>My Form</title>
        <meta name="description" content="An interactive form">
        </head>
    
        <body>
    
        <form action="test.php" method="post" id="Personalinfo">
    
        <label for="fname">Όνομα:</label>
        <input type="text" id="fname" name="firstname" placeholder="Όνομα 
        Πελάτη..">
    
        <input type="submit" value="Submit">
    
        </body>
        </html>
    

        <?php
        $servername = "localhost";
        $username = "username";
        $password = "";
        $dbname = "mydb";
    
        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
       // Check connection
       if ($conn->connect_error) {
       die("Connection failed: " . $conn->connect_error);
       } 
    
       $sql = "INSERT INTO Guests (firstname)
       VALUES ('?')";
    
       if ($conn->query($sql) === TRUE) {
       echo "New record created successfully";
       } else {
       echo "Error: " . $sql . "<br>" . $conn->error;
       }
    
       $conn->close();
       ?>
    

    谢谢你,塔诺斯

    2 回复  |  直到 7 年前
        1
  •  1
  •   Nana Partykar    7 年前
    $sql = "INSERT INTO Guests (firstname) VALUES ('?')";
    

    '?' 替换为整数、字符串、double或blob值。

    您放置了“?”,但是忘了用 bind_param $firstname 将值转换为 $stmt->bind_param("s", $firstname);

    $firstname = $_POST['firstname'];
    $sql = $conn->prepare("INSERT INTO Guests (firstname) VALUES (?)");
    $sql->bind_param("s", $firstname);
    
    if ($sql->execute() === TRUE) {
    

    1. Prepared Statements in MySQLi

    2. how to insert into mysql using Prepared Statement with php

        2
  •  0
  •   Lekens    7 年前
           <!DOCTYPE html>
                <html>
                <head>
                <meta charset="utf-8">
                <meta http-equiv="X-UA-Compatible" content="IE=edge">
                <title>My Form</title>
                <meta name="description" content="An interactive form">
                </head>
    
                <body>
    
                <form action="test.php" method="post" id="Personalinfo">
    
                <label for="fname">Όνομα:</label>
                <input type="text" id="fname" name="firstname" placeholder="Όνομα 
                Πελάτη..">
    
                <input type="submit" name="submitForm" value="Submit">
    
                </body>
                </html> 
        **test.php file**
        <?php
            $servername = "localhost";
            $username = "root";
            $password = "";
            $dbname = "mydb";
    
            // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);
           // Check connection
           if ($conn->connect_error) {
           die("Connection failed: " . $conn->connect_error);
           } 
    
        if(isset($_POST['submitForm'])){
        $firstname = $_POST['firstname'];
           $sql = "INSERT INTO Guests (firstname)
           VALUES ('{$firstname}')";
    
           if ($conn->query($sql) === TRUE) {
           echo "New record created successfully";
           } else {
           echo "Error: " . $sql . "<br>" . $conn->error;
           }
    
        }else{
    echo "Are you sure you enter a firstname and the name of your html submit is submitForm";
    }
    
           $conn->close();
           ?>