数据库连接模头

时间:2018-07-08 11:20:26

标签: php html database forms database-connection

我的数据库连接有问题。 每当我尝试连接时,它就会消失并显示我写的此消息 “查询失败”。 我正在尝试创建联系表以将数据发送到我的数据库,而我的数据库名称为“ contact”。 而我的表格名称是“ contact_table”。

我不知道该怎么办,请帮助我。 这些是我的代码:

<div class="contact">
  <form action="contact_us.php" method="POST">
    <label class="label-name">Name: </label>
    <input class="input-name" name="name" type="text">
    <label class="label-lname">Last Name: </label>
    <input class="input-lname" name="lastname" type="text">
    <label class="label-email">Email: </label>
    <input class="input-email" name="email" type="email">
    <label class="label-message">Message: </label>
    <textarea class="input-message" name="message" type="text"></textarea>
    <a href="#"><input class="submit-btn" name="submit" type="submit" value="Submit"></a>
  </form>
</div>

if(isset($_POST['submit'])){
    global $connection;
    $name = $_POST['name'];
    $lname = $_POST['lastname'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    $name = mysqli_real_escape_string($connection, $name );
    $lname = mysqli_real_escape_string($connection, $lname );
    $email = mysqli_real_escape_string($connection, $email );
    $message = mysqli_real_escape_string($connection, $message );

    $query = "INSERT INTO contact_table('name', 'lastname', 'email', 'message') ";
    $query .= "VALUES ('name', 'lastname', 'email', 'message')";

    $result = mysqli_query($connection, $query);

    if(!$result){
        die('Query FAILED');
    } else {
        echo "Record Created";
    }
}

这是我的数据库连接

$server = "localhost";
$username = "root";
$password = "";
$dbname = "contact";


$connection = mysqli_connect($server , $username, $password, $dbname);
    if(!$connection){
        die("Database connection failed".  mysqli_error());
}

3 个答案:

答案 0 :(得分:0)

这应该适用于插入语句。我是PDO的粉丝,建议使用它。

    <div class="contact">
      <form action="contact_us.php" method="POST" id="your_form"> //Give the form an ID
        <label class="label-name">Name: </label>
        <input class="input-name" name="name" type="text">
        <label class="label-lname">Last Name: </label>
        <input class="input-lname" name="lastname" type="text">
        <label class="label-email">Email: </label>
        <input class="input-email" name="email" type="text">
        <label class="label-message">Message: </label>
        <textarea class="input-message" name="message" form="your_form"></textarea> 
        //Tell your text he belongs with your form.
        <input class="submit-btn" name="submit" type="submit" 
        value="Submit">//removed <a> tag
      </form>
    </div>



        if(isset($_POST['name'])){ //if this post is set then...
        $name = $_POST['name']; //get posted information
        $lname = $_POST['lastname'];
        $email = $_POST['email'];
        $message = $_POST['message'];

        //Use PDO connection
        $dsn = "mysql:host=localhost;dbname=your_database_here";
        $username = "your_username";
        $password= "your_password";
        $conn = new PDO($dsn, $username, $password); //should test connection



        $stmt = $conn->prepare("INSERT INTO contact_table (name, lastname, email, message) 
        VALUES (:name, :lastname, :email, :message); //Make named placeholders
        $stmt->bindParam(':name', $name);
        $stmt->bindParam(':lastname', $lname);
        $stmt->bindParam(':email', $email); //bind the posted information
        $stmt->bindParam(':message', $message);

        $stmt->execute(); //Execute the insert



        if(!$stmt){
            die('Query FAILED');
        } else {
            echo "Record Created";
        }
    }

答案 1 :(得分:-1)

您的连接没有失败。您的插入查询中有错误。

查询应为

$query = "INSERT INTO contact_table('name', 'lastname', 'email', 'message') ";
$query .= "VALUES ($name, $lname, $email, $message)";

答案 2 :(得分:-1)

我终于发现了问题所在。 我不应该使用引号。 这是正确的代码:

$query = "INSERT INTO contact_table(name, lastname, email, message) ";