Mysql数据将在表列中显示“Array”

时间:2012-05-07 04:36:11

标签: php mysql


我想问一下我的php编码..
我正在做一些数组编码,但是当我提交数据时,在我的数据库中会在'description'和'qty'列显示一个数组。
我用mysql .. 下面是我的数组编码..

$item = array(
'item_description' => '',
'price' => '',
'qty' => '',
'amount' => '',
);
foreach ($item as $key => $value){
$item_description = $_POST['item_description'][$key];
$price = $_POST['price'][$key];
$qty = $_POST['qty'][$key];
$amount = $_POST['amount'][$key];}
}

这是我的插入查询。

$sql1="INSERT INTO payment_item (payment_id, payment_item_id, item_description, price, qty, amount)
VALUES
('$last_insert_payment_id','NULL','$_POST[item_description]','$_POST[price]','$_POST[qty]','$_POST[amount]')";

这是我的表格:

<form>
<h1>Payment Item</h1>
Payment ID :<input id="payment_id" name="payment_id" type="text"><br>
<input type="button" value="Add Row" onClick="addRow('tableID')"/>
<input type="button" value="Delete Row" onClick="deleteRow('tableID')"/>
<table class="table" bgcolor="#CCCCCC">
<thead>
<tr>
<td></td>
<td><center>Item Description</center></td>
<td><center>Price (RM)</center></td>
<td><center>Month</center></td>
<td><center>Amount (RM)</center></td>
</tr>
</thead>
<tbody id="tableID">
<tr>
<td><input type="checkbox" name="chk"></td>
<td>
<select name="item_description">
    <option value="deposit">Deposit</option>
    <option value="rental">Rental</option>
    <option value="stamp">Stamp Duty</option>
    <option value="process">Process Fee</option>
    <option value="ap">AP</option>
</select>
</td>
<td><input id="price" name="price" type="text"></td>
<td><input id="month" name="qty" type="text"></td>
<td><input id="amount" name="amount" type="text"></td>
</tr>
</tbody>
</table>
<input name="submit" type="submit" value="Submit">
<input name="reset" type="reset" value="Reset">
</form>

我很抱歉我的英语很差..
但我真的需要别人的帮助..
谢谢..

2 个答案:

答案 0 :(得分:0)

我不知道你的$ _POST包含什么。但我认为你需要这个

<?php 
$item = array(
   'item_description' => '',
   'price' => '',
   'qty' => '',
   'amount' => '',
);
foreach ($item as $key => &$value)
{
    $value = $_POST[$key];
}
?>

编辑: 您必须指定表单方法

<form method="post">

最后INSERT查询为

$sql1="INSERT INTO payment_item (payment_id, payment_item_id, item_description, price,qty, amount) VALUES ('$last_insert_payment_id','NULL','$item[item_description]','$item[price]','$item[qty]','$item[amount]')";

答案 1 :(得分:0)

当PHP尝试将数组转换为字符串时,会出现一个单词'Array' 所以 - 对于字段显示为数组,您必须以某种方式处理它,取决于您的应用程序逻辑,将您拥有的任何数组转换为正确的字符串。

这个问题与mysql无关。

另请记住,您必须格式化SQL字符串。至少做

$var = mysql_real_escape_string($var);

表示您在引号中查询的每个变量