Submission of PHP Form to XAMPP MySQL database

2019-09-19 10:16发布

问题:

I created a php form for a catering service that I'm planning to insert into a MySQL database: The php form is called index.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/font-awesome.min.css">
    <link rel="stylesheet" href="css/style.css">
    <title>BOOTSTRAP</title>
</head>
<div class="container-fluid">
<div class="row">
<div class="inquiry col-md-6">
        <h1>Inquire Now</h1>
            <form method="post" action="inquire.php" name="inquireform" id="inquireForm">
                <div class="form-group">
                    <label for="InputName">Name*</label>
                    <input class="form-control" type="text" id="inputName" placeholder="Name" name="Cust`Name">
                </div>
                <div class="form-group">    
                    <label for="InputLocation">Location*</label>
                    <input class="form-control" type="text" id="inputLocation" placeholder="Location" name="Location">
                </div>
            </form>
            <form class="form-inline">
                <div class="form-group">
                    <label for="SelectDate">Date of Event*</label>
                    <select class="form-control" id="SelectMonth" name="Month">
                        <option>Jan</option>
                        <option>Feb</option>
                        <option>Mar</option>
                        <option>Apr</option>
                        <option>May</option>
                        <option>Jun</option>
                        <option>Jul</option>
                        <option>Aug</option>
                        <option>Sept</option>
                        <option>Oct</option>
                        <option>Nov</option>
                        <option>Dec</option>
                    </select>
                    <select class="form-control" id="SelectDay" name="Day">
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                        <option>5</option>
                        <option>6</option>
                        <option>7</option>
                        <option>8</option>
                        <option>9</option>
                        <option>10</option>
                        <option>11</option>
                        <option>12</option>
                        <option>13</option>
                        <option>14</option>
                        <option>15</option>
                        <option>16</option>
                        <option>17</option>
                        <option>18</option>
                        <option>19</option>
                        <option>20</option>
                        <option>21</option>
                        <option>22</option>
                        <option>23</option>
                        <option>24</option>
                        <option>25</option>
                        <option>26</option>
                        <option>27</option>
                        <option>28</option>
                        <option>29</option>
                        <option>30</option>
                        <option>31</option>
                    </select>
                    <select class="form-control" id="selectYear" placeholder="Year" name="Year">
                        <option>2017</option>
                        <option>2018</option>
                        <option>2019</option>
                        <option>2020</option>
                        <option>2021</option>
                        <option>2022</option>
                    </select>
                </div>
                </form>
                <form>
                <div class="form-group">    
                    <label for="InputNumber">Number of Guests*</label>
                    <input class="form-control" type="Number" id="inputNumber" placeholder="Number" name="Guests">
                </div>
                <div class="form-group">    
                    <label for="InputContact">Contact Number*</label>
                    <input class="form-control" type="text" id="inputContact" placeholder="Contact Number" name="ContNum">
                </div>
                <input class="btn btn-default" type="submit" value="submit">
            </form>
        </div>
    </div>
    </div>
</body>
</html>

And the inquire.php is:

<?php
$mysql_host     = "localhost";
$mysql_username = "root";
$mysql_password = "password";
$mysql_database = "catering";

$mysqli  = new Mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database);
$prepare = $mysqli->prepare("INSERT INTO `inquiry`(`CustName`,`Location`,`Month`,`Day`,`Year`,`Guests`,`ContNum`) VALUES (?,?,?,?,?,?,?)");
$prepare->bind_param("sssssss", print_r($POST)['CustName'], print_r($POST)['Location'], print_r($POST)['Month'], print_r($POST)['Day'], print_r($POST)['Year'], print_r($POST)['Guests'], print_r($POST)['ContNum']);
$prepare->execute();
$mysqli->close();
?>

Which I got from this other question: How to write information from html form to MySQL Database

However, mine doesn't work. The php url just changes, no data is inserted into my database. I'm thinking maybe something's wrong with my setup or installation maybe its one of the following?

  1. I have no PHP installed in my PC, just XAMPP.
  2. I put the index.php and inquire.php in a folder in xampp >htdocs
  3. I run XAMPP in Admin mode.
  4. the print_r($_POST) might be incorrect.

Guys i really need help. I made sure the database name, table name, and column names matched. What could I be doing wrong here?

DATABASE PIC: enter image description here

Update ;

No data still inserted, the url only changes from file://index.php to file://index.php?Guests=2&ContNum=123

回答1:

Your binding is most likely to be incorrect, you could remove the print_r and it should be $_POST not $POST. I'm assuming that the POST names are correct, and are supplied. To be safe, change Mysqli to mysqli

<?php
$mysql_host     = "localhost";
$mysql_username = "root";
$mysql_password = "password";
$mysql_database = "catering";

$mysqli  = new mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database);
$prepare = $mysqli->prepare("INSERT INTO `inquiry`(`CustName`,`Location`,`Month`,`Day`,`Year`,`Guests`,`ContNum`) VALUES (?,?,?,?,?,?,?)");
$prepare->bind_param("sssssss", $_POST['CustName'], $_POST['Location'], $_POST['Month'], $_POST['Day'], $_POST['Year'], $_POST['Guests'], $_POST['ContNum']);
$prepare->execute();
$mysqli->close();
?>

This would be better if you first put the data in a variable

<?php
$mysql_host     = "localhost";
$mysql_username = "root";
$mysql_password = "password";
$mysql_database = "catering";

$custName = $_POST['CustName'];
$location= $_POST['Location'];
$month= $_POST['Month'];
$day= $_POST['Day'];
$year= $_POST['Year'];
$guests= $_POST['Guests'];
$contNum= $_POST['ContNum'];

$mysqli  = new mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database);
$prepare = $mysqli->prepare("INSERT INTO `inquiry`(`CustName`,`Location`,`Month`,`Day`,`Year`,`Guests`,`ContNum`) VALUES (?,?,?,?,?,?,?)");
$prepare->bind_param("sssssss",
                    $custName,
                    $location,
                    $month,
                    $day,
                    $year,
                    $guests,
                    $contNum);
$prepare->execute();
$mysqli->close();
?>


回答2:

Change this :

$prepare->bind_param("sssssss", print_r($POST)['CustName'], print_r($POST)['Location'], print_r($POST)['Month'], print_r($POST)['Day'], print_r($POST)['Year'], print_r($POST)['Guests'], print_r($POST)['ContNum']);

To this :

$prepare->bind_param("sssssss", $_POST['CustName'], $_POST['Location'], $_POST['Month'], $_POST['Day'], $_POST['Year'], $_POST['Guests'], $_POST['ContNum']);

NB : Is also important to validate user input before storing



回答3:

You have more forms in the html, you need to have just one. I commented out the extra ones so you can understand better.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/font-awesome.min.css">
    <link rel="stylesheet" href="css/style.css">
    <title>BOOTSTRAP</title>
</head>
<div class="container-fluid">
<div class="row">
<div class="inquiry col-md-6">
        <h1>Inquire Now</h1>
            <form method="post" action="inquire.php" name="inquireform" id="inquireForm">
                <div class="form-group">
                    <label for="InputName">Name*</label>
                    <input class="form-control" type="text" id="inputName" placeholder="Name" name="Cust`Name">
                </div>
                <div class="form-group">    
                    <label for="InputLocation">Location*</label>
                    <input class="form-control" type="text" id="inputLocation" placeholder="Location" name="Location">
                </div>
            <!-- </form> -->
            <!-- <form class="form-inline"> -->
                <div class="form-group">
                    <label for="SelectDate">Date of Event*</label>
                    <select class="form-control" id="SelectMonth" name="Month">
                        <option>Jan</option>
                        <option>Feb</option>
                        <option>Mar</option>
                        <option>Apr</option>
                        <option>May</option>
                        <option>Jun</option>
                        <option>Jul</option>
                        <option>Aug</option>
                        <option>Sept</option>
                        <option>Oct</option>
                        <option>Nov</option>
                        <option>Dec</option>
                    </select>
                    <select class="form-control" id="SelectDay" name="Day">
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                        <option>5</option>
                        <option>6</option>
                        <option>7</option>
                        <option>8</option>
                        <option>9</option>
                        <option>10</option>
                        <option>11</option>
                        <option>12</option>
                        <option>13</option>
                        <option>14</option>
                        <option>15</option>
                        <option>16</option>
                        <option>17</option>
                        <option>18</option>
                        <option>19</option>
                        <option>20</option>
                        <option>21</option>
                        <option>22</option>
                        <option>23</option>
                        <option>24</option>
                        <option>25</option>
                        <option>26</option>
                        <option>27</option>
                        <option>28</option>
                        <option>29</option>
                        <option>30</option>
                        <option>31</option>
                    </select>
                    <select class="form-control" id="selectYear" placeholder="Year" name="Year">
                        <option>2017</option>
                        <option>2018</option>
                        <option>2019</option>
                        <option>2020</option>
                        <option>2021</option>
                        <option>2022</option>
                    </select>
                </div>
                <!-- </form> -->
                <!-- <form> -->
                <div class="form-group">    
                    <label for="InputNumber">Number of Guests*</label>
                    <input class="form-control" type="Number" id="inputNumber" placeholder="Number" name="Guests">
                </div>
                <div class="form-group">    
                    <label for="InputContact">Contact Number*</label>
                    <input class="form-control" type="text" id="inputContact" placeholder="Contact Number" name="ContNum">
                </div>
                <input class="btn btn-default" type="submit" value="submit">
            </form>
        </div>
    </div>
    </div>
</body>
</html>


回答4:

Use localhost/your_project_name.html to access your project in the URL bar. Basically you don't use "xampp localhost" you just double-click on the html file from what i get.

Ok there are some MORE problems with the code that I spotted.

First of all inside the print_r($POST['something']) in your query change it to $_POST['something']. Moving forward you have wrong values inside post for example $_POST['Cust Name'] in your form is set name="Cust`Name". Then you have too many forms you must narrow it down. For 1 html file you can't have that many form. Php can not "read elements" it's server side so if you keep that structure maybe you should consider ajax call to php.

<form method="post" action="inquire.php" name="inquireform" id="inquireForm">
    <div class="form-group">
        <label for="InputName">Name*</label>
        <input class="form-control" type="text" id="inputName" placeholder="Name" name="Cust`Name">
    </div>
    <div class="form-group">    
        <label for="InputLocation">Location*</label>
        <input class="form-control" type="text" id="inputLocation" placeholder="Location" name="Location">
    </div>
</form>

This part here will make the request to php file but that's it the rest will not be send through request because they are included in another form , without action and php file is not included there.