【问题标题】:Pass hidden input value through the form to mysql database table通过表单将隐藏的输入值传递给 mysql 数据库表
【发布时间】:2014-08-08 20:10:32
【问题描述】:

我有一个表格:

<table border="1">
  <tr>
    <td align="center">Form Input Employees Data</td>
  </tr>
  <tr>
    <td>
      <table>
        <form method="post" action="input.php">
<input type="hidden" name="id" value="1234">
        <tr>
          <td>Product Name</td>
          <td><input type="text" name="name" size="20">
          </td>
        </tr>
        <tr>
          <td>Brand</td>
          <td><input type="text" name="brand" size="40">
          </td>
        </tr>
        <tr>
          <td></td>
          <td align="right"><input type="submit" name="submit" value="Sent"></td>
        </tr>
</form>
        </table>

而我的 input.php 是:

<?
//the example of inserting data with variable from HTML form
//input.php
mysql_connect("localhost","xxx","xxx");//database connection
mysql_select_db("xxxx_xxx");




//inserting data order
$order = "INSERT INTO wp_userdata
            (id, product_name, product_brand)
            VALUES
            ('$_POST[id]',
            '$_POST[name]',
            '$_POST[brand]')";

//declare in the order variable
$result = mysql_query($order);  //order executes
if($result){
    echo("<br>Input data is succeed");
} else{
    echo("<br>Input data is fail");
}
?>

当我点击发送按钮时,新行被添加到数据库表中,但只记录product_nameproduct_brand。隐藏的输入“id”值没有进入表...

如何让它记录所有 3 个值:idproduct_nameproduct_brand

【问题讨论】:

  • print_r($_POST) 在您的input.php 文件中并在此处发布结果。
  • Array ([id] => comparebest [name] => Test14 [brand] => dfg [submit] => Sent )
  • 您的id 是此处的文字。 id 的数据库中可能有 int 数据类型
  • 现在一切正常,不知道这时候出了什么问题
  • 不要相信用户提交的输入!

标签: php html mysql database forms


【解决方案1】:

我看到一些引号丢失,我强烈建议您将cast(强制)id 为整数并将mysql_real_escape_string 用于字符串项。否则,如果有人想要伤害,他可以编辑您隐藏的 HTML 输入字段并读出您的数据库。 Read more about it

我还建议您不要在 SQL 查询中使用 $_POST var。而是尝试为它使用一个专用数组,这样您就知道它已经针对 SQL 注入进行了处理,而且您可能希望在使用它之前对数据做更多的事情。在我看来,修改$_POST 变量是一种不好的做法。原样离开$_POST。更容易调试问题。并修改数组的副本。

第三;而是使用 PHP MySQLi 函数(或 PDO),因为不推荐使用旧函数。

input.php

//input.php
$sqli_handle = mysqli_connect("localhost","xxx","xxx");//database connection
mysqli_select_db($sqli_handle, "xxxx_xxx");

//convert the POST data to safe DB data
$data = $_POST;
$data['id'] = (int)$data['id'];
$data['name'] = mysqli_real_escape_string($sqli_handle, $data['name']);
$data['brand'] = mysqli_real_escape_string($sqli_handle, $data['brand']);

//inserting data order
$order = "INSERT INTO wp_userdata
            (id, product_name, product_brand)
            VALUES
            ('".(int).$data['id']."',
            '".$data['name']."',
            '".$data['brand']."')";

$result = mysqli_query($sqli_handle, $order);
if($result){
    echo("<br>Input data is succeed");
}
else{
    echo("<br>Input data is fail");
}

【讨论】:

    【解决方案2】:

    在您的 input.php 文件中,您必须使用变量插值,请执行以下操作:

            $id = (int) $_POST[id]; // Cast this to int because, I think you must have integer type date for ID column in your database
    
            $order = "INSERT INTO wp_userdata
            (id, product_name, product_brand) 
            VALUES ({$id}, {$_POST[name]}, {$_POST[brand]})";
    

    有关插值的更多信息 - 请点击此链接:PHP variable interpolation vs concatenation

    【讨论】:

      【解决方案3】:

      进行以下更改

      $order = "插入 wp_userdata (id、product_name、product_brand) 价值观 ('".$_POST[mycustomid]."', '".$_POST[name]."', '".$_POST[品牌]."')";

      wordpress 有时会保留一些关键字,请检查我的代码

      【讨论】:

        猜你喜欢
        • 2017-01-31
        • 1970-01-01
        • 2021-05-30
        • 2011-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-19
        相关资源
        最近更新 更多