-1

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

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
  • UPDATE: Change the columns to the right data type on database. Still no data inserted. – Gorilya Warfare Sep 29 '17 at 08:48
  • Do u know what does print_r do? – Masivuye Cokile Sep 29 '17 at 08:53
  • Yes, why `print_r($_POST)['...']` and not `$_POST['...']` ? And try to check for errors after each step, that way you know which part of your code is working and which not. Link: http://php.net/manual/ro/mysqli.quickstart.prepared-statements.php – Badea Mihai Florin Sep 29 '17 at 08:57
  • @BadeaMihaiFlorin I used $_POST['...'] before but it did not add info to database, In the link, I saw a user said please use print_r($_POST) then the guy who asked the question said it worked. Ima try i again. – Gorilya Warfare Sep 29 '17 at 09:02
  • Try to `print_r($_POST)` before adding to db, to see if you receive all parameters. And, now that i take a closer look, why do you have nested forms? You should have just one. – Badea Mihai Florin Sep 29 '17 at 09:06
  • The problem is that you not running php from a server but from a browser directly – Masivuye Cokile Sep 29 '17 at 09:11
  • Possible duplicate of [How to run php files on my computer](https://stackoverflow.com/questions/8580273/how-to-run-php-files-on-my-computer) – Masivuye Cokile Sep 29 '17 at 09:11
  • @MasivuyeCokile I have xampp running and Apache and MySQL installed and started – Gorilya Warfare Sep 29 '17 at 09:15
  • you need to type on your browser this : localhost/FolderThatUssavdthefileon – Masivuye Cokile Sep 29 '17 at 09:16
  • @BadeaMihaiFlorin for the nested form I'm using bootstrap. for the date form, it looks like this: https://imgur.com/a/tXYiu – Gorilya Warfare Sep 29 '17 at 09:17
  • @MasivuyeCokile I tried it, localhost/form/index.php is the url, pictures got removed? When i clicked submit, the url still change to http://localhost/form/index.php?Guests=2&ContNum=201 still it didnot insert the data i submitted to the database – Gorilya Warfare Sep 29 '17 at 09:25

4 Answers4

1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
pr1nc3
  • 8,108
  • 3
  • 23
  • 36
  • Yes i already changed it to localhost/form/index.html, it removed my inage slider, logo, menu images and still didnt submit the data to the database. – Gorilya Warfare Sep 29 '17 at 09:41
0

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();
?>
Carl Binalla
  • 5,393
  • 5
  • 27
  • 46
0

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

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
0

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>