MYSQL - 将多个输入插入 table 问题

MYSQL - Inserting multiple inputs into a table issue

我一直在使用这个代码(更改变量名称),将信息输入到我整个网站的数据库中,我从来没有遇到过问题。 这是我厌倦了一次插入数据库的最多变量。没有数据只是不会插入的错误消息。

有更好的方法吗?这是一个已知问题吗?

PHP

<?php
if (isset($_POST['Save'])) {
  $f_name = $_POST['franchise_name'];
  $f_email = $_POST['fran_email'];
  $f_mangn = $_POST['mang_name'];
  $f_addline_1 = $_POST['franc_address'];
  $f_addline_2 = $_POST['address2'];
  $f_city = $_POST['city'];
  $f_pcode = $_POST['pcode'];
  $f_phone = $_POST['franc_phone'];

  $insert_franc_dets = "INSERT INTO Franchise_manager_account(Area_Name,Franchise_email,Fran_Fname,Fran_business_add_line1,Fran_business_add_line2,Fran_City,fran_Postcode,Fran_Contact_Num) 
  VALUES (?,?,?,?,?,?,?,?)
  ON DUPLICATE KEY 
  UPDATE
  Area_Name = '$f_name',
  Franchise_email = '$f_email',
  Fran_Fname = '$f_mangn',
  Fran_business_add_line1 = '$f_addline_1',
  Fran_business_add_line2 = '$f_addline_2',
  Fran_City = '$f_city',
  fran_Postcode = '$f_pcode',
  Fran_Contact_Num = '$f_phone'";

  $c = mysqli_prepare($dbc, $insert_franc_dets);
  //new
  // $stmt = mysqli_prepare($dbc, $insert_c);
  //debugging
  //$c = mysqli_prepare($dbc, $insert_franc_dets)  or die(mysqli_error($dbc));

  mysqli_stmt_bind_param($c,'sssssssi', $f_name, $f_email, $f_mangn, $f_addline_1, $f_addline_2, $f_city, $f_pcode, $f_phone);

  /* execute query */
  $execute = mysqli_stmt_execute($c);

  // if inserted echo the following messges
  if ($execute) {
    echo "<script> alert('Addrrss Saved')</script>";
  } else {
    echo "<b>Oops! we have an issue </b>";
  }
}
$dbc->close();
?>

HTML

<form id="franchiseDets" action ="Franchise-Details.php" method="POST">


  <!--                franchise details form-->
  <div class="field">

      <input type="text" name="franchise_name" id="fran_name" placeholder="e.g One Delivery Leeds" pattern="[a-zA-Z]" 
             autofocus required tabindex="1">
      <br>

      <input type="email" name="fran_email" id="fran_email" placeholder="leeds@one-delivery.co.uk" required tabindex="2">
      <br>

      <input type="text" name="mang_name" id="name" placeholder="Joe Blogs" required tabindex="3">
      <br>


      <input type="text" name="franc_address" id="address1" placeholder="Address Line 1" tanindex="4">
      <input type="text" name="address2" id="address2" placeholder="Address Line 2" tabindex="5">
      <input type="text" name="city" id="city" placeholder="Town/City" tabindex="6">
      <input type="text" name="pcode" id="pcode" placeholder="Postcode" tabindex="7">
      <br>


      <input type="tel" name="franc_phone" id="phone" placeholder="Customer service number" min="10" maxlength="11" pattern="[0-9]{3}[-][0-9]{4}[-][0-9]{4}" 
             required title="Please provide your customer service number in the following format: 000-0000-0000" tabindex="8">

<input type="submit" name="Save" value="Save">
</form>
      <br>
  </div>

TABLE(3 个唯一元素) Session_start();位于页面顶部。 仍在研究 sql 注入。

您正在为插入使用绑定占位符。 (这是好事)

但是您在更新的 SQL 文本中有 文字 。 (看在世间一切美好美好的份上,你为什么要那样做?)

带有绑定占位符的准备好的语句可以防御SQL注入。但这项工作只完成了一半。

只需使用 VALUES() 函数来引用将插入到列中的值(如果插入有效)。我没有查看您为更新部分中的哪些列分配了哪些值,下面的示例分配了为插入提供的值。

 INSERT INTO franchise_manager_account
      ( area_name
      , franchise_email
      , fran_fname
      , fran_business_add_line1
      , fran_business_add_line2
      , fran_city
      , fran_postcode
      , fran_contact_num
      ) VALUES
      ( ?
      , ?
      , ?
      , ?
      , ?
      , ?
      , ?
      , ?
      )
 ON DUPLICATE KEY 
 UPDATE area_name               = VALUES(area_name)
      , franchise_email         = VALUES(franchise_email)
      , fran_fname              = VALUES(fran_fname)
      , fran_business_add_line1 = VALUES(fran_business_add_line1)
      , fran_business_add_line2 = VALUES(fran_business_add_line2)
      , fran_city               = VALUES(fran_city)
      , fran_postcode           = VALUES(fran_postcode)
      , fran_contact_num        = VALUES(fran_contact_num)

要执行 UPDATE 部分,INSERT 必须引发 "duplicate key exception"。要发生该错误,至少需要定义一个 PRIMARY and/or UNIQUE KEY。 (从哪个或哪些列组成主 and/or 唯一键的问题中不清楚。通常,我们 从更新中省略 这些列。)

如果我需要在更新中提供 不同的 值,我会使用 SQL 表达式来执行任何检查或操作(例如,不要替换非 NULL 值,或添加到现有值等

       , col7  = IF(col7 IS NULL, VALUES(col7), col7) 
       , col8  = col8 + VALUES(col8)
       , col9  = CONCAT(col9,',',VALUES(col9))
       , colA  = ?

如果它是一个完全不同的值,那么我会为它提供一个绑定占位符,就像在 INSERT 中一样。

至于为什么没有插入行...如果是 SQL 注入导致了问题,比如其中一个值包含单引号或其他内容,上面的模式应该可以解决这个问题.

代码应该真正检查 prepareexecute 中的 return,并处理错误。发出 mysqli_error 文本不是生产就绪代码的最佳实践,但它很容易开发和调试。至少,您的代码应该检查错误并有一个代码块。为了开发,至少echo/print mysqli_error.

如果您的代码没有这样做,那么它将把它的虚拟小指放在它的虚拟嘴角,Dr.Evil 风格并说 "I just assume it will all go to plan. What?"

并确保 PHP 错误报告已启用。

您确定您的第一个脚本正在执行吗?

如果仍然找不到问题,请开始添加一些额外的调试输出。

How to debug small programs