向XAMPP MySQL数据库提交PHP表单

时间:2017-09-29 08:44:29

标签: php mysql database forms

我为我正计划插入MySQL数据库的餐饮服务创建了一个php表单: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>

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();
?>

我从另一个问题中得到了:How to write information from html form to MySQL Database

然而,我的不起作用。 php url只是更改,没有数据插入到我的数据库中。我想也许我的设置或安装有问题可能是以下其中一种?

  1. 我的电脑上没有安装PHP,只是XAMPP。
  2. 我把index.php和inquire.php放在xampp的一个文件夹中&gt; htdocs
  3. 我在管理员模式下运行XAMPP。
  4. print_r($ _ POST)可能不正确。
  5. 伙计我真的需要帮助。我确保匹配数据库名称,表名称和列名称。我在这里做错了什么?

    数据库PIC: enter image description here

    更新;

      

    尚未插入任何数据,该网址仅从file://index.php更改为   ?文件://index.php客人= 2及ContNum = 123

4 个答案:

答案 0 :(得分:0)

您的绑定最有可能不正确,您可以删除print_r,而$_POST不是$POST。我假设POST名称是正确的,并且是提供的。为安全起见,请将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();
?>

如果您首先将数据放入变量

,这会更好
<?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();
?>

答案 1 :(得分:0)

改变这个:

 Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet

对此:

$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']);

注意:在存储

之前验证用户输入也很重要

答案 2 :(得分:0)

你在html中有更多的表单,你只需要一个。我评论了额外的那些,以便你能更好地理解。

<!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>

答案 3 :(得分:0)

使用localhost / your_project_name.html在URL栏中访问您的项目。基本上你不会使用&#34; xampp localhost&#34;你只需双击我得到的html文件。

好的,我发现的代码存在一些问题。

首先,在您的查询中的print_r($POST['something'])内,将其更改为$_POST['something']。继续前进,您在帖子中有错误的值,例如$_POST['Cust Name']在您的表单中设置名称=&#34; Cust`Name&#34;。那么你有太多的形式你必须缩小它。对于1个html文件,您不能拥有那么多形式。 Php不能读取元素&#34;它的服务器端,所以如果你保留这个结构,也许你应该考虑对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>

此部分将向php文件发出请求,但其余部分将不会通过请求发送,因为它们包含在另一个表单中,没有操作,并且不包含php文件。