简体   繁体   中英

My HTML form is not posting to my database, is there something wrong with my php?

I am trying to get my HTML form to post to my data base but I get nothing. I am not bothered about the security side of things at the moment I would just like it functional. I have looked all over the web for a solution and at most of the similar problems on here. I am probably just missing something stupid.

HTML

  <form action="booking.php"  id="booking" name="booking" method="post">
       Let us know what you would like and when and we will get back to you 
       to confirm<br>
         <label for="name">Name:</label>
         <input type="text" id="name" name="name"><br>
         <label for="email">Email:</label>
         <input type="email" name="email" id="email" required>`

PHP

<?php

define('db_database', 'database');
define('db_user_name', 'username');
define('db_password', 'password');
define('db_server_name', 'server');

$dbconnect = mysqli_connect(db_server_name, db_user_name, db_password, 
db_database);

if (!$dbconnect) {
die('Could not connect: ' . mysqli_error());
}

echo 'Connected!';

$db_selected = mysqli_select_db($dbconnect, db_database);


     $name = $_POST['name'];

     $mysqli = "INSERT INTO booking (name) VALUES ($name)"; 

?>

MySQL returned an empty result set (ie zero rows). (Query took 0.0004 seconds.)

  1. Make sure that the name attribute in the input tag from your name is defined as name like: <input type="text" name="name"> Without this you can't access. Check if you get the value of the submitted form like: echo $_POST["name"]

I would recommend to get the $name variable in the SQL-Query like this: INSERT INTO booking (name) VALUES ('$name')

If it's not working after all, try to execute the query like this:

$result = mysqli_query($dbconnect, $mysqli);

(instead of your $db_selected part)

I hope I can help.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM