I had a problem with submitting the data in MySql database, my code has no error, but when I check the database it seems like there is no submitting of data in the table
this is my php code
if(isset($_GET['date'])){
$date = $_GET['date'];
}
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$timeslot = $_POST['timeslot'];
$mysqli = new mysqli('localhost', 'root', '*******', 'odpas');
$stmt = $mysqli->prepare("INSERT INTO 'bookings' ('name', 'email', 'date', 'timeslot') VALUES (?,?,?,?)");
$stmt->bind_param('ssss', $name, $email, $date, $timeslot);
$stmt->execute();
$msg = "<div>Booking Successfully</div>";
$stmt->close();
$mysqli->close();
}
here is the form
<form action="" method="post">
<h4>Booking: <p id="slot"></p>
</h4>
<label> Name </label>
<input required type="text" name="name">
<label> Email </label>
<input required type="email" name="email">
<label> TIMESLOT </label>
<input required type="text" readonly name="timeslot" id="timeslot">
<button type="submit">
SUBMIT
</button>
</form>
the codes aren't separated, when I click the button "submit" it looks like it's working but when I check the database there's no submitting of data.
Thank you very much for your help!
if (isset($_GET['date'])){
$date = $_GET['date'];
// Every code of php should be between here.
}
Remember single quotes are for SQL strings and back-ticks are for database entities like columns or tables. You're using the wrong quotes here.
Here's the corrected version:
$stmt = $mysqli->prepare("INSERT INTO `bookings` (`name`, `email`, `date`, `timeslot`) VALUES (?,?,?,?)");
Note that unless you have a conflict with a MySQL reserved keyword you don't need the backticks. In this case only date
is a conflict:
$stmt = $mysqli->prepare("INSERT INTO bookings (name, email, `date`, timeslot) VALUES (?,?,?,?)");
try this simple solution
<button type="submit" name="submit">
SUBMIT
</button>
try this:
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$timeslot = $_POST['timeslot'];
$date=$_GET['date'];
$mysqli = new mysqli('localhost', 'root', '*******', 'odpas');
$stmt = "insert into bookings(name, email, data,timeslot) values('$name','$email','$date','$timeslot');";
if($mysqli->query($stmt))
{
echo 'data saved successfully';
}
}
You forgot to give name submit button and checking for isset($_POST['submit']) that's why code inside if condition is not running.
if(isset($_GET['date'])){
$date = $_GET['date'];
}
if(isset($_POST['submit'])){
//print_r($_POST);
$name = $_POST['name'];
$email = $_POST['email'];
$timeslot = $_POST['timeslot'];
$mysqli = new mysqli('localhost', 'root', '*******', 'odpas');
$stmt = $mysqli->prepare("INSERT INTO 'bookings' ('name', 'email', 'date', 'timeslot') VALUES (?,?,?,?)");
$stmt->bind_param('ssss', $name, $email, $date, $timeslot);
$stmt->execute();
$msg = "<div>Booking Successfully</div>";
$stmt->close();
$mysqli->close();
}
?>
<form action="" method="post">
<h4>Booking: <p id="slot"></p>
</h4>
<label> Name </label>
<input required type="text" name="name">
<label> Email </label>
<input required type="email" name="email">
<label> TIMESLOT </label>
<input required type="text" readonly name="timeslot" id="timeslot">
<button type="submit" name="submit">
SUBMIT
</button>
</form>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.