我提交表单时,我的数据库不会插入我的记录

时间:2017-12-10 09:16:58

标签: php html

我正在做我的fyp。我是PHP编码的新手。我不知道为什么在我提交表单时数据不会插入到我的数据库中。我在表单中添加的照片可以存储到目标文件中。请帮帮我。

这是我的表格restaurant-add.php

<form action="php/addmenu.php" method="POST" enctype="multipart/form-data">
    <div class="box-body">
        <div class="form-group">
            <label>Restaurant Name</label>
            <input class="form-control" type="text" name="restaurant_name">
        </div>
        <div class="form-group">
            <label>Password</label>
            <input class="form-control" type="text" name="restaurant_password">
        </div>
        <div class="form-group">
            <label for="exampleInputFile">Restaurant Logo</label>
            <input id="exampleInputFile" type="file" name="restaurant_logo">
        </div>
        <div class="form-group">
            <label>Contact Number</label>
            <input class="form-control" type="text" name="restaurant_contactnum">
        </div>
        <div class="form-group">
            <label>Address</label>
            <input class="form-control" type="text" name="restaurant_address" >
        </div>

        <div class="box-footer">
            <button class="btn btn-primary" type="submit" name="submit">Submit</button>
            <button class="btn btn-primary" type="reset">Reset</button>
        </div>
</form>

我提交表单时的php文件addmenu.php

<?php
include 'db.php';
//define other variables with submitted values from $_POST
$name = $_POST['restaurant_name'];
$contactnum = $_POST['restaurant_contactnum'];
$address = $_POST['restaurant_address'];

//md5 hash password for security
$password = md5($_POST['restaurant_password']);

$uploadedfile = $_FILES["restaurant_logo"]["tmp_name"];
$allowedExts = array("png","jpg","jpeg"); /* ACCEPTED FILE FORMAT */
$filename = $_FILES["restaurant_logo"]["name"]; /* NAME OF THE FILE */
$extension = pathinfo($filename, PATHINFO_EXTENSION); /* GET THE FILE EXTENSION */
$extension = strtolower($extension); /* LOWER THE STRINGS OF THE EXTENSION */

if (in_array($extension,$allowedExts)) { /* IF FILE IS INDEED AN IMAGE */

    $path = "restaurantlogo/".$filename; /* DIRECTORY WHERE YOU WANT TO STORE THE IMAGE ALONG WITH THE FILE NAME */
    move_uploaded_file($uploadedfile,$path);

    //set session variables to display on welcome page
    $_SESSION['username'] = $name;
    $_SESSION['restaurant_logo'] = $uploadedfile;

    //insert user data into database
    $sql = "INSERT INTO testingrestaurant (Logo, Name, Password, Contact Number, Address) VALUES ('$uploadedfile', '$name', '$password', '$contactnum', '$address')";

    //check if mysql query is successful
    if (($conn->query($sql) === TRUE) {
        $_SESSION['message'] = "Registration successful!"
        echo "Added ".$name." to the database!";
        echo $message;
        //redirect the user to welcome.php
        header("location: restaurant-add.php");
    }
}

连接数据库db.php的php文件

<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName="food delivery";

// Create connection
$conn = new mysqli($dbServername, $dbUsername, $dbPassword,$dbName);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";

3 个答案:

答案 0 :(得分:2)

在行中:

$sql = "INSERT INTO testingrestaurant (Logo, Name, Password, Contact Number, Address) VALUES ('$uploadedfile', '$name', '$password', '$contactnum', '$address')";  

testingrestaurant替换为'testingrestaurant'。 PHP,如果你改变变量,仍然PHP需要短划线..

答案 1 :(得分:1)

$sql = "INSERT INTO testingrestaurant (Logo, Name, Password, Contact Number, Address) VALUES ('$uploadedfile', '$name', '$password', '$contactnum', '$address')";

你有一个空格&#34;联系号码&#34;,这在SQL中是不对的,使用数据库中的确切列名。

这应该有所帮助。

答案 2 :(得分:1)

<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName="food delivery";

// Create connection
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword,$dbName);

// Check connection
if (mysqli_connect_errno()) {
    $result  = 'error';
    $message = 'Failed to connect to database: ' . mysqli_connect_error();

}

echo "Connected successfully";

//define other variables with submitted values from $_POST
$name = $_POST['restaurant_name'];
$contactnum = $_POST['restaurant_contactnum'];
$address = $_POST['restaurant_address'];

//md5 hash password for security
$password = md5($_POST['restaurant_password']);

$uploadedfile = $_FILES["restaurant_logo"]["tmp_name"];
$allowedExts = array("png","jpg","jpeg"); /* ACCEPTED FILE FORMAT */
$filename = $_FILES["restaurant_logo"]["name"]; /* NAME OF THE FILE */
$extension = pathinfo($filename, PATHINFO_EXTENSION); /* GET THE FILE EXTENSION */
$extension = strtolower($extension); /* LOWER THE STRINGS OF THE EXTENSION */

if (in_array($extension,$allowedExts)) { /* IF FILE IS INDEED AN IMAGE */

    $path = "restaurantlogo/".$filename; /* DIRECTORY WHERE YOU WANT TO STORE THE IMAGE ALONG WITH THE FILE NAME */
    move_uploaded_file($uploadedfile,$path);

    //set session variables to display on welcome page
    $_SESSION['username'] = $name;
    $_SESSION['restaurant_logo'] = $uploadedfile;

    //insert user data into database
    $sql = "INSERT INTO testingrestaurant (Logo, Name, Password, ContactNumber, Address) VALUES ('$uploadedfile', '$name', '$password', '$contactnum', '$address')";

    $insert = mysqli_query($conn, $sql) or die(mysqli_error($conn));

    //check if mysql query is successful
    if ($sql) {
        $_SESSION['message'] = "Registration successful!";
        echo "Added " . $name . " to the database!";
        echo $message;
        //redirect the user to welcome.php
        header("location: restaurant-add.php");
    }

}
?>