I am currently doing my fyp. I am new to PHP coding. I have no idea on why the data would not being insert into my database when I submit my form.The photo I added in the form can be stored to the target file. Please help me.
This is my form 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>
My php file when I submit my form 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");
}
}
The php file to connect to database db.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";