PHP不会将值插入MySQL数据库

时间:2019-12-15 21:53:09

标签: php mysql mysqli

我的PHP代码未在我的MySQL数据库中输入值。我已经看过类似的问题,但似乎无济于事。如果有人可以告诉我问题出在哪里,我将非常感激。

<?php
include_once 'db.php';
if(isset( $_POST["submit"])){
    //connect_db();
    $table = "contact";
    $id = $_POST["id"];
    $fname = $_POST["firstname"];
    $lname = $_POST["lastname"];
    $city = $_POST["city"];
    $text = $_POST["text"];
    $query = "insert into $table (id, firstname, lastname, city, text) VALUES ('".$id."', '".$fname."', '".$lname."', '".$city."', '.$text.')";
    $result = mysqli_query($query);

// Check the mysql query result. If there is none, there is an error. So tell us what the error is.
if (!$result) {
  die('Invalid query: ' . mysqli_error());
}

mysqli_close($conn);
    }
?>

这是与我的数据库的连接:

<?php
//global variable for using in multiple functions
$conn;

function connect_db(){
    //username and password for accessing mysql db
    $servername = "localhost";
    $username="root";
    $password="";
    $database="fast-fast";
    global $conn;    
    $conn = new mysqli($servername, $username, $password, $database);
    if($link == false){
    echo "Try to reconnect";
    }
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
} 

function db_error(){
    global $conn;
    return $conn->error;
}

function db_query($query){
    global $conn;
    $result=$conn->query($query);
    return $result;
}

function db_close(){
    global $conn;
    $conn->close();
?>

0 个答案:

没有答案
相关问题