不能到insert表格数据到数据库使用php mysql查询

c8ib6hqw  于 2023-01-16  发布在  PHP
关注(0)|答案(2)|浏览(104)

我是非常新的php mysql,我正在使用下面的php从输入数据.我的var_dump显示所有的值,我已经正确地输入在表单中.数据库连接也是成功的.但是数据没有插入到MariaDB表.我已经交叉检查了表结构,值和数据类型是匹配的.一切看起来都是好的,但是数据库表从来没有用数据更新过.任何帮助都将不胜感激!提前感谢.

dropdown-form.php
=================
<?php
include('database.php');
 ?>

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Chiefdom Zambia</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.min.css">
<style>
.container 
 {
  border-radius: 5px;
  background-color: #f2f2f2;
  padding: 20px;
 }

  .col-25 
  {
  float: left;
  width: 25%;
  margin-top: 6px;
  }

.col-75 
{
  float: left;
  width: 75%;
  margin-top: 6px;
}

/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}

/* Responsive layout - when the screen is less than 600px wide, make the two columns stack on top of each other instead of next to each other */
@media screen and (max-width: 600px) {

  .col-25,
  .col-75,
  input[type=submit] {
    width: 100%;
    margin-top: 0;
  }
}

酋长领地授权数据收集

<div class="container">
   <div class="dependent-dropdown">
    <form method="post" action="dropdown-form.php">
    <legend style="text-align: center"> Land Info </legend>
    <legend style="text-align: center"> Country : Zambia </legend>
    <div class="input-field">
      <select name="pro_id" id="country">

        <option value="pro_id">Select your Province here</option>

        <?php

        $countryData = "SELECT id, name from countries";
        $result = mysqli_query($conn, $countryData);
        if (mysqli_num_rows($result) > 0) {
          while ($arr = mysqli_fetch_assoc($result)) {

            ?>

            <option value="<?php echo $arr['id']; ?>">
              <?php echo $arr['name']; ?>
            </option>
          <?php
          }
        }
        ?>

      </select>

    </div>
    <div class="input-field">
      <select name="dist_id" id="state">
        <option value="dist_id">Select your District here</option>
      </select>
    </div>
    <div class="input-field">
      <select name="chief_id" id="city">
        <option value="chief_id">Select your Chiefdom here </option>
      </select>
    </div>

    <input type="text" placeholder="Enter Zone Number " name="zone_no">
    <input type="text" placeholder="Enter Zone Name " name="zone_name">
    <input type="text" placeholder="Enter Locality" name="locality">
    <legend style="text-align: center"> Farmer Info </legend>
    <input type="text" placeholder="Enter Name of the Farmer" name="farmer_name"></input>
    <input type="text" placeholder="Enter NRC Number of the Farmer " name="nrc_no">
    <input type="text" placeholder="Enter Contact Number of the Farmer " name="contact_no">
    <input type="text" placeholder="Land Holding in Hectare (Ha)" name="land_holding">

    <legend style="text-align: center"> Crop Info </legend>

    <select name="crops_no" id="form-selector">
      <option value="0">Select Total Number of Crops </option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
    </select>
    
<br>
<button>submit</button>
</form>
</div>

</div>


</body>

</html>

<?php
$pro_id = filter_input(INPUT_POST, "pro_id", FILTER_VALIDATE_INT);
$dist_id = filter_input(INPUT_POST, "dist_id", FILTER_VALIDATE_INT);
$chief_id = filter_input(INPUT_POST, "chief_id", FILTER_VALIDATE_INT);
$zone_no = $_POST["zone_no"];
$zone_name = $_POST["zone_name"];
$locality = $_POST["locality"];
$farmer_name = $_POST["farmer_name"];
$nrc_no = $_POST["nrc_no"];
$contact_no = $_POST["contact_no"];
$land_holding = $_POST["land_holding"];
$crops_no = filter_input(INPUT_POST, "crops_no", FILTER_VALIDATE_INT);

ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$sql = "INSERT INTO farmer (' province`, `district`, `chiefdom`, `zone no`, `zone name`,`local`, `farmer name`, `nrc no`, `mobile no`, `land holding`, `no of crops`, ) VALUES ( ?,?,?,?,?,?,?,?,?,?,?)";

$stmt = mysqli_stmt_init($conn);

if (!mysqli_stmt_prepare($stmt, $sql)) {
die(mysqli_error($conn));
}

mysqli_stmt_bind_param(
$stmt,
"iiisssssssi",
$pro_id,
$dist_id,
$chief_id,
$zone_no,
$zone_name,
$locality,
$farmer_name,
$nrc_no,
$contact_no,
$land_holding,
$crops_no

);

if (mysqli_stmt_execute($stmt)) {
echo "Record added successfully";
} else {
echo "Error: " . mysqli_error($conn);
}
mysqli_stmt_close($stmt);

 database.php
 ============

$dbHost = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "ddl";

$conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);

if ($conn->connect_error) 
{
 die("Connection failed: " . $conn->connect_error);
}

if (mysqli_connect_errno()) {
die("Connection Error: " . mysqli_connect_error());

}

'情况如下:
我已经检查了数据库连接的错误。数据类型和值是完美的。表列和sql查询是匹配的。sql查询的手动输入接受数据并在表中更新。
我得到了以下错误:致命错误:未捕获的mysqli_sql_异常:SQL语法中有错误;查看与您的MariaDB服务器版本对应的手册,在下拉表单中的第1行的'' province , district , chiefdom , zone no , zone name , local , farmor...'附近使用正确的语法。php:255堆栈跟踪:第0个下拉列表.php(255):mysqli_stmt_prepare(对象(mysqli_stmt),'插入到远端...')#1 {main}在dropdown-form.php的第255行抛出

xqk2d5yq

xqk2d5yq1#

看起来你在字段名周围使用了一个反勾号而不是一个直引号。我建议使用一个可以突出显示语法的编辑器(有很多免费的),这样这些问题就会跳到你的眼睛里。这会把你的注意力集中到更重要的部分。

8yoxcaq7

8yoxcaq72#

这里的MySQL语法是不正确的,这正是错误发现问题的地方:

INSERT INTO farmer (`province`, `district`, ...
--                  ^ -- Fixed

一定要 * 极其 * 密切注意MySQL在错误信息中说的话。它通常会精确地指出问题所在,就像这里所做的那样。

相关问题