简体   繁体   English

无法使用 php mysql 查询将表单数据插入数据库

[英]unable to insert form data to database using php mysql query

i am very new to php mysql, i am using the following php from to input data.我对 php mysql 很陌生,我正在使用以下 php from 输入数据。 my var_dump shows all the values i have entered in the form correctly.我的 var_dump 显示了我在表单中正确输入的所有值。 database connection was also successful.数据库连接也成功。 but the data are not inserted into the MariaDB table.但是数据没有插入到 MariaDB 表中。 i have cross-checked that the table structure, values and data types are matching.我已经交叉检查表结构、值和数据类型是否匹配。 everything seem to be ok but the database table was never updated with the data.一切似乎都很好,但数据库表从未用数据更新过。 any help would be much appreciated.任何帮助将非常感激。 thanks in advance.提前致谢。

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;
  }
}
Chiefdom Empowerment Data Collection 酋邦赋权数据收集
 <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()); }

` scenarios were: ` 情景是:

i have checked the database connection for errors.我已经检查了数据库连接是否有错误。 data type and values are perfect.table column and sql query are matching.数据类型和值是完美的。表列和 sql 查询是匹配的。 manual entry of sql query accepts the data and updated in the table.手动输入 sql 查询接受数据并在表中更新。

i got the following error: Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax;我收到以下错误:致命错误:未捕获 mysqli_sql_exception:您的 SQL 语法有错误; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' province , district , chiefdom , zone no , zone name , local , farmer...' at line 1 in dropdown-form.php:255 Stack trace: #0 dropdown-form.php(255): mysqli_stmt_prepare(Object(mysqli_stmt), 'INSERT INTO far...') #1 {main} thrown in dropdown-form.php on line 255检查与您的 MariaDB 服务器版本相对应的手册,以了解在下拉列表中第 1 行的“省,,酋长国,区域编号,区域名称,本地,农民...”附近使用的正确语法。php:255 堆栈trace: #0 dropdown-form.php(255): mysqli_stmt_prepare(Object(mysqli_stmt), 'INSERT INTO far...') #1 {main} 在第 255 行的 dropdown-form.php 中抛出

It looks like you are using a back tick around the field names rather than a straight quote.看起来您在字段名称周围使用反引号而不是直引号。 I would recommend using an editor that does syntax highlighting (there are lots of free ones) so these issues will jump to your eyes.我建议使用可以进行语法高亮显示的编辑器(有很多免费的),这样这些问题就会跳到您的眼前。 This will bring your focus to the parts that matter more.这将使您的注意力集中在更重要的部分。

The MySQL syntax here is incorrect, which is precisely where the error spotted a problem:此处的 MySQL 语法不正确,这正是错误发现问题的地方:

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

Do pay extremely close attention to what MySQL says in its error messages.请特别注意MySQL在其错误消息中所说的内容。 It is usually pinpointing the exact problem, and it did here, to the character.它通常会准确指出角色的确切问题,在这里也是如此。

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

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