我有一张表格:
<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_name
和product_brand
。隐藏的输入“id”值不会进入表格......
如何记录所有3个值:id
,product_name
和product_brand
?
答案 0 :(得分:0)
我看到一些引号丢失了,我强烈建议你cast
(强制)将id转换为整数并使用mysql_real_escape_string
来表示字符串项。否则,如果有人想要受到伤害,他可以编辑您隐藏的HTML输入字段并读出您的数据库。 Read more about it
我还建议你不要在SQL查询中使用$_POST
var。而是尝试使用专用数组,因此您知道它已针对SQL注入进行处理,但您也可能希望在使用数据之前对数据执行更多操作。在我看来,修改$_POST
变量是一种不好的做法。完全按照$_POST
的方式离开//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");
}
。更容易调试问题。并修改数组的副本。
第三;而是使用PHP MySQLi函数(或PDO),因为旧函数已被弃用。
input.php
{{1}}
答案 1 :(得分:0)
在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
答案 2 :(得分:0)
执行以下操作
$ order =&#34; INSERT INTO wp_userdata (id,product_name,product_brand) VALUES (&#39;&#34; $ _ POST [mycustomid]&#34;&#39 ;, &#39;&#34; $ _ POST [名称]&#34;&#39 ;, &#39;&#34; $ _ POST [品牌]&#34;&#39;)&#34 ;;
有些时候wordpress会保留很少的关键字请查看我的代码