【问题标题】:MYSQL - Inserting multiple inputs into a table issueMYSQL - 将多个输入插入表问题
【发布时间】:2016-03-25 03:59:09
【问题描述】:

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

有没有更好的方法来做到这一点?这是一个已知问题吗?

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 个独特元素) 会话开始();位于页面顶部。 仍在进行 sql 注入。

【问题讨论】:

    标签: php mysql insert-into


    【解决方案1】:

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

    但是您在更新的 SQL 文本中有 literals。 (为了世上所有美好美好的事物,你为什么要这么做?)

    带有绑定占位符的预处理语句是对 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 必须引发“重复键异常”。要发生该错误,至少需要定义一个 PRIMARY 和/或 UNIQUE KEY。 (从哪个或哪些列构成主键和/或唯一键的问题中并不清楚。通常,我们从 UPDATE 中省略这些列。)

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

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

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

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

    代码应该真正检查来自prepareexecute 的返回,并处理错误。发出mysqli_error 文本并不是生产就绪代码的最佳实践,但它很容易开发和调试。至少,您的代码应检查错误并具有代码块。对于开发,至少 echo/print mysqli_error。

    如果您的代码没有这样做,那么它会将虚拟小指放在虚拟嘴角,Dr.Evil 风格并说“我只是假设一切都会按计划进行。什么?”

    并确保启用 PHP 错误报告。

    你确定你的第一个脚本已经被执行了吗?

    如果还是找不到问题,那就开始添加一些额外的调试输出。

    How to debug small programs

    【讨论】:

    • 谢谢。这行得通,但非常感谢您解释我知道如何更好地理解并将继续更改我以前的脚本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    相关资源
    最近更新 更多