【问题标题】:Can't save data inside phpmyAdmin无法在 phpmyAdmin 中保存数据
【发布时间】:2016-07-05 08:19:24
【问题描述】:

问题: 您好,我的一个提交表单有问题。我已经创建了一个数据库,我也创建了一个表。当我已经填写了我的表格,然后单击提交按钮,然后检查 phpmyAdmin。它没有显示我已经提交的详细信息。

这是我的 php 代码,包括 sql connect (reserve.php)

<?php 
    if($_POST['formSubmit'] === "Submit")
    {
        $errorMessage = "";

        if(empty($_POST['formName']))
        {
            $errorMessage .= "<li> You forgot to enter your name! </li>";
        }


        //Doesnt Have List
        $varName = $_POST['formName'];
        $varEmail = $_POST['formEmail'];
        $varPhone = $_POST['formPhone'];
        $varStreet = $_POST['formStreet'];
        $varNumber = $_POST['formNumber'];
        $varCity = $_POST['formCity'];
        $varPostCode = $_POST['formPostCode'];
        $varCountry = $_POST['formCountry'];
        //Have List
        $varPeople = $_POST['formPeople'];
        $varTables = $_POST['formTables'];
        $varTime = $_POST['formTime'];
        $varComment = $_POST['formComment'];

        if(empty($errorMessage))
        {
            //Sql Connect
            $db = mysql_connect ('sql999.byethost9.com','b9_9999999','password');
            if(!$db) die("Error connecting to MySQL database.");
            mysql_select_db('b9_9999999_database',$db);

            $sql = "INSERT INTO form2 (Name, Email, Phone, Street, Number, City, PostCode, Country, People, Tables, Time, Comment) VALUES (".
            //Doesnt Have List Dropdown
            PrepSQL($varName) . ", ".
            PrepSQL($varEmail) . ", ".
            PrepSQL($varPhone) . ", ".
            PrepSQL($varStreet) . ", ".
            PrepSQL($varNumber) . ", ".
            PrepSQL($varCity) . ", ".
            PrepSQL($varPostCode) . ", ".
            PrepSQL($varCountry) . ", ".
            //Have List Dropdown
            PrepSQL($varPeople) . ", ".
            PrepSQL($varTables) . ", ".
            PrepSQL($varTime) . ", ".
            PrepSQL($varComment) . ") ";
            mysql_query($sql);

            header('Location:thankyou.html');
            exit();
              }
        }

        //function:PrepSQL()
        //use stripslashes and mysql_real_escape_string PHP functions
        //tosanitize a string for use in an SQL query
        //
        //also puts single quotes around the string
        //
        function PrepSQL($value)
        {
            //Stripslashes
            if(get_magic_quotes_gpc())
            {
                $value = stripslashes($value);
            }

            //Quote
            $value = "'" . mysql_real_escape_string($value) . "'";

            return($value);
        }
    ?>

这是我的表单代码:

<html>
<?php include("reserve.php"); ?>
<body>
.......

<form action="<?php
 echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
  <!-- PHP--> 
  <!--  General -->
  <div class="form-group">
    <h2 class="heading">Booking & contact</h2>
    <div class="controls">
      <input type="text" id="name" class="floatLabel" name="name" value="<?= $varName;
    ?>" />
      <label for="name">Name</label>
    </div>
    <div class="controls">
      <input type="text" id="email" class="floatLabel" name="email" value="<?= $varEmail;
    ?>" />
      <label for="email">Email</label>
    </div>
    <div class="controls">
      <input type="tel" id="phone" class="floatLabel" name="phone" value="<?= $varPhone;
    ?>" />
      <label for="phone">Phone</label>
    </div>
    <div class="grid">
      <div class="col-2-3">
        <div class="controls">
          <input type="text" id="street" class="floatLabel" name="street" value="<?= $varStreet;
    ?>" />
          <label for="street">Street</label>
        </div>
      </div>
      <div class="col-1-3">
        <div class="controls">
          <input type="number" id="street-number" class="floatLabel" name="street-number" value="<?= $varNumber;
    ?>" />
          <label for="street-number">Number</label>
        </div>
      </div>
    </div>
    <div class="grid">
      <div class="col-2-3">
        <div class="controls">
          <input type="text" id="city" class="floatLabel" name="city" value="<?= $varCity;
    ?>" />
          <label for="city">City</label>
        </div>
      </div>
      <div class="col-1-3">
        <div class="controls">
          <input type="text" id="post-code" class="floatLabel" name="post-code" value="<?= $varPostCode;
    ?>" />
          <label for="post-code">Post Code</label>
        </div>
      </div>
    </div>
    <div class="controls">
      <input type="text" id="country" class="floatLabel" name="country" value="<?= $varCountry;
    ?>" />
      <label for="country">Country</label>
    </div>
  </div>
  <!--  Details -->
  <div class="form-group">
    <h2 class="heading">Details</h2>
    <div class="grid">
      <div class="col-1-4 col-1-4-sm">
        <div class="controls">
          <input type="date" id="arrive" class="floatLabel" name="arrive" value="<?php
 echo date('Y-m-d'); ?>">
          <label for="arrive" class="label-date"><i class="fa fa-calendar"></i>&nbsp;&nbsp;Arrive</label>
        </div>
      </div>
    </div>
    <div class="grid">
      <div class="col-1-3 col-1-3-sm">
        <div class="controls"> <i class="fa fa-sort"></i>
          <select class="floatLabel">
            <option value="blank"></option>
            <option value="1" <?php if ($varPeople === "Less than 5")    echo ("selected=\"selected\""); ?>>Less than 5</option>
            <option value="2" <?php if ($varPeople === "More than 5")    echo ("selected=\"selected\""); ?>>More than 5</option>
            <option value="3" <?php if ($varPeople === "More than 10")    echo ("selected=\"selected\""); ?>>More than 10</option>
          </select>
          <label for="fruit"><i class="fa fa-male"></i>&nbsp;&nbsp;People</label>
        </div>
      </div>
      <div class="col-1-3 col-1-3-sm">
        <div class="controls"> <i class="fa fa-sort"></i>
          <select class="floatLabel">
            <option value="blank"></option>
            <option value="deluxe" <?php if ($varTables === "Indoor")    echo ("selected=\"selected\""); ?>>Indoor</option>
            <option value="Zuri-zimmer" <?php if ($varTables === "Outdoor")    echo ("selected=\"selected\""); ?>>Outdoor</option>
            <option value="Zuri-zimmer" <?php if ($varTables === "VIP Room")    echo ("selected=\"selected\""); ?>>VIP Room</option>
          </select>
          <label for="fruit">Tables</label>
        </div>
      </div>
      <div class="col-1-3 col-1-3-sm">
        <div class="controls"> <i class="fa fa-sort"></i>
          <select class="floatLabel">
            <option value="single-bed" <?php if ($varTime === "Breakfast")    echo ("selected=\"selected\""); ?>>Breakfast</option>
            <option value="double-bed" <?php if ($varTime === "Lunch")    echo ("selected=\"selected\""); ?>>Lunch</option>
            <!-- Add "selected >Lunch" when not using php code -->
            <option value="double-bed" <?php if ($varTime === "Dinner")    echo ("selected=\"selected\""); ?>>Dinner</option>
          </select>
          <label for="fruit">Time</label>
        </div>
      </div>
    </div>
    <div class="grid">
      <p class="info-text">Please describe your needs e.g. Annual Dinner, Party</p>
      <br>
      <div class="controls">
        <textarea name="comments" class="floatLabel" id="comments"><?= $varComment;
    ?>
</textarea>
        <label for="comments">Comments</label>
      </div>
      <button type="submit" value="Submit" class="col-1-4">Submit</button>
    </div>
  </div>
  <!-- /.form-group -->
</form>

....
</body>
</html>

【问题讨论】:

  • 澄清一下,您的问题与 phpMyAdmin 无关。您还应该使用 mysqli_ 函数,或者更好的是 PDO。
  • It doesnt show the details that I already submitted . 显示了什么?请提出正确的问题。阅读:stackoverflow.com/help/how-to-ask
  • 您的输入名称与您在 php 脚本中使用的变量不匹配。完全没有。
  • @jeroen 哪一个?是在表单代码里面吗?

标签: javascript php html mysql css


【解决方案1】:

您的表单字段名称与您的 serve.php 名​​称字段不匹配。您更新的代码应该是

<?php 
    if($_POST['formSubmit'] === "Submit")
    {
        $errorMessage = "";

        if(empty($_POST['formName']))
        {
            $errorMessage .= "<li> You forgot to enter your name! </li>";
        }


        //Doesnt Have List
        $varName = $_POST['name'];
        $varEmail = $_POST['email'];
        $varPhone = $_POST['phone'];
        $varStreet = $_POST['street'];
        $varNumber = $_POST['street-number'];
        $varCity = $_POST['city'];
        $varPostCode = $_POST['post-code'];
        $varCountry = $_POST['country'];
        //Have List
        $varPeople = $_POST['arrive'];
        $varTables = $_POST['tables'];
        $varTime = $_POST['time'];
        $varComment = $_POST['comments'];

        if(empty($errorMessage))
        {
            //Sql Connect
            $db = mysql_connect ('sql999.byethost9.com','b9_9999999','password');
            if(!$db) die("Error connecting to MySQL database.");
            mysql_select_db('b9_9999999_database',$db);

            $sql = "INSERT INTO form2 (Name, Email, Phone, Street, Number, City, PostCode, Country, People, Tables, Time, Comment) VALUES (".
            //Doesnt Have List Dropdown
            PrepSQL($varName) . ", ".
            PrepSQL($varEmail) . ", ".
            PrepSQL($varPhone) . ", ".
            PrepSQL($varStreet) . ", ".
            PrepSQL($varNumber) . ", ".
            PrepSQL($varCity) . ", ".
            PrepSQL($varPostCode) . ", ".
            PrepSQL($varCountry) . ", ".
            //Have List Dropdown
            PrepSQL($varPeople) . ", ".
            PrepSQL($varTables) . ", ".
            PrepSQL($varTime) . ", ".
            PrepSQL($varComment) . ") ";
            mysql_query($sql);

            header('Location:thankyou.html');
            exit();
              }
        }

        //function:PrepSQL()
        //use stripslashes and mysql_real_escape_string PHP functions
        //tosanitize a string for use in an SQL query
        //
        //also puts single quotes around the string
        //
        function PrepSQL($value)
        {
            //Stripslashes
            if(get_magic_quotes_gpc())
            {
                $value = stripslashes($value);
            }

            //Quote
            $value = "'" . mysql_real_escape_string($value) . "'";

            return($value);
        }
    ?>

这会解决你的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多