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

标签 php html

我目前正在做我的五年计划。我是 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";

最佳答案

行中:

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

testingrestaurant 替换为 'testingrestaurant'。 PHP 如果你用变量代替,PHP 仍然需要破折号..

关于php - 当我提交表单时,我的数据库不会插入我的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47737426/

相关文章:

javascript - 根据类获取<li>值

javascript - jQuery mouseenter 技巧?

php - 通过社交登录方法获取电子邮件地址

html - Bootstrap 下拉菜单隐藏导航链接

html - jQuery 第一级的每个 div

php - 使用php将sql postgres查询导出到excel

html - 供应商前缀内联样式

php - 检测编码并使一切变为 UTF-8

javascript - 使用URL.createObjectURL()处理来自数据库的Blob图像

php - 提交 html/PHP 表单时出现内部服务器错误