dongpangfu6322 2017-09-29 08:44
浏览 45

向XAMPP MySQL数据库提交PHP表单

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

  • 写回答

4条回答 默认 最新

  • doukang2003 2017-09-29 08:56
    关注

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

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题