简体   繁体   English

提交 PHP 表单到 XAMPP MySQL 数据库

[英]Submission of PHP Form to XAMPP MySQL database

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:我为餐饮服务创建了一个 php 表单,我打算将其插入到 MySQL 数据库中:该 php 表单名为 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:而inquire.php是:

<?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我从另一个问题中得到: 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. php url 只是更改,没有数据插入到我的数据库中。 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.我的电脑上没有安装 PHP,只有 XAMPP。
  2. I put the index.php and inquire.php in a folder in xampp >htdocs我把index.php和inquire.php放在xampp>htdocs的文件夹中
  3. I run XAMPP in Admin mode.我在管理员模式下运行 XAMPP。
  4. the print_r($_POST) might be incorrect. print_r($_POST) 可能不正确。

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仍然没有插入数据,url 只从 file://index.php 更改为 file://index.php?Guests=2&ContNum=123

Use localhost/your_project_name.html to access your project in the URL bar.使用 localhost/your_project_name.html 在 URL 栏中访问您的项目。 Basically you don't use "xampp localhost" you just double-click on the html file from what i get.基本上你不使用“xampp localhost”,你只需双击我得到的html文件。

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'] .首先,在查询中的print_r($POST['something'])中将其更改为$_POST['something'] Moving forward you have wrong values inside post for example $_POST['Cust Name'] in your form is set name="Cust`Name".继续前进,您在 post 中有错误的值,例如$_POST['Cust Name']在您的表单中设置为 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.对于 1 个 html 文件,您不能有那么多形式。 Php can not "read elements" it's server side so if you keep that structure maybe you should consider ajax call to php. Php 无法“读取元素”,它是服务器端,因此如果您保留该结构,也许您应该考虑对 php 进行 ajax 调用。

<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.这里的这部分将向 php 文件发出请求,但剩下的将不会通过请求发送,因为它们包含在另一种形式中,没有操作并且 php 文件不包含在那里。

Your binding is most likely to be incorrect, you could remove the print_r and it should be $_POST not $POST .您的绑定很可能不正确,您可以删除print_r并且它应该是$_POST而不是$POST I'm assuming that the POST names are correct, and are supplied.我假设 POST 名称是正确的,并且已提供。 To be safe, change Mysqli to mysqli为了安全起见,将Mysqli更改为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();
?>

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注意:在存储之前验证用户输入也很重要

You have more forms in the html, you need to have just one.您在 html 中有更多表单,您只需要一个。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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