0

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!

Vasilisa
  • 4,604
  • 3
  • 20
  • 25
  • 1
    Please, easy on the ALL CAPS. – tadman Dec 04 '19 at 01:01
  • 2
    Tip: A lot of problems can be detected and resolved by [enabling exceptions in `mysqli`](https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) so errors resulting from simple mistakes made aren’t easily ignored. Without exceptions you must pay close attention to return values, many of these indicate problems you must resolve or report to the user. Exceptions allow for more sophisticated flow control as they can “bubble up” to other parts of your code where it’s more convenient to handle them. – tadman Dec 04 '19 at 01:02
  • are you binding 5 parameters when you need only 4 – Arun Kamalanathan Dec 04 '19 at 02:10
  • try removing `if(isset($_GET['date'])){ $date = $_GET['date']; }` and you can get value fof date in the second if statement. – Utkarsh Dec 04 '19 at 05:12

5 Answers5

0
if (isset($_GET['date'])){
    $date = $_GET['date'];
    // Every code of php should be between here.
}
henriquehbr
  • 1,063
  • 4
  • 20
  • 41
0

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 (?,?,?,?)");
tadman
  • 208,517
  • 23
  • 234
  • 262
  • "Not working" is not a useful diagnostic. Do you get any errors in your error log? Are you watching the error log? Have you enabled error reporting? – tadman Dec 04 '19 at 01:38
  • My error reporting is enabled and and still no errors found, but the problem is still the same. – Chedrick F. Rowy Dec 04 '19 at 02:12
  • You're going to have to find out a reason. Is the `$_POST` data properly populated? Stub in some `echo var_dump` calls. – tadman Dec 04 '19 at 02:16
0

try this simple solution

<button type="submit" name="submit">
          SUBMIT
</button>
0

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';
    }
} 
Utkarsh
  • 635
  • 7
  • 14
0

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>
Hitesh Tripathi
  • 856
  • 1
  • 11
  • 23