Php 我的数据库赢了';i don’我提交表格时不要插入我的记录
我目前正在做我的fyp。我是新的PHP编码。我不知道为什么在我提交表格时,数据不会插入我的数据库。我在表格中添加的照片可以存储到目标文件中。请帮帮我 这是我的表单restaurant-add.phpPhp 我的数据库赢了';i don’我提交表格时不要插入我的记录,php,html,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"&
<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>
餐厅名称
密码
餐厅标志
联系电话
地址
提交
重置
提交表单addmenu.php时的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");
}
}
“联系人号码”中有空格,这在SQL中不正确,请在数据库中使用准确的列名
这应该会有帮助。行中:
<?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");
}
}
?>
$sql = "INSERT INTO testingrestaurant (Logo, Name, Password, Contact Number, Address) VALUES ('$uploadedfile', '$name', '$password', '$contactnum', '$address')";
将testingrestaurant
替换为'testingrestaurant'
。PHP如果您使用变量,PHP仍然需要破折号。谢谢您的回复,但在我的数据库中,它们之间有一个空格。这不是最佳做法,如果您可以在数据库中更改名称,请这样做。您还忘了包括mysqli_查询($conn,$sql);在你询问之后。这是将查询($sql)发布到数据库中的一行解析错误:语法错误,C:\xampp\htdocs\FYP\php\addmenu.php中的意外“echo”(T_echo),位于第34行,它指的是这行echo“Added”$名称“转到数据库!”;我试着用谷歌搜索,但他们的情况与我不同。你可以发布当前的addmenu.php脚本吗?代码太长,直到我无法在评论框中发布,所以我会发布另一个答案。那是一堆代码。但这看起来并不是一个真正的答案。你改变了什么?为什么要解决这个问题?改变了mysqli的所有实例“改变了mysqli的所有实例”-以什么方式?为什么?这是使用mysqli的另一种方式。此格式与forma之间的区别在于,它以不同的方式调用每个mysqli函数。e、 对于连接,我从新的mysqli改为mysqli\u connect。如果你想坚持使用你的方法,你必须寻找使用你的方法调用mysqli_查询的正确方法。“这是使用mysqli的另一种方法”-为什么它能解决问题?OP调用->query
的方式有什么问题?危险:您正在使用并且需要删除您用户的密码。危险:您很容易受到需要从中删除的密码的攻击。如果($conn->query($sql)==TRUE){
-这是真的吗?我猜不是。有一个错误函数可以告诉你数据库服务器报告了什么错误。使用它!
<?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");
}
}
?>
$sql = "INSERT INTO testingrestaurant (Logo, Name, Password, Contact Number, Address) VALUES ('$uploadedfile', '$name', '$password', '$contactnum', '$address')";