0


I want to ask about my php coding..
I'am doing some array coding but when I submit my data, in my database is will display an array at the 'description' and 'qty' column..
I use mysql.. Below is my array coding..

$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];}
}

This is my insert query.

$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]')";

This is my form:

<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>

I'am sorry for my poor english language..
But I really need any help from someone for this..
Thank you..

4
  • A var_dump($_POST) should tell you why that happens. Commented May 7, 2012 at 4:38
  • Somewhere then you're casting an array to a string. If you cast an array to a string, it casts to "Array". Try to figure out why that's happening. Commented May 7, 2012 at 4:38
  • For sure, I really didn't know well about using array. If I just use 'foreach' loop in my coding, did it give some result?? Commented May 7, 2012 at 4:43
  • Can I now how can I put some coding here because I just new at this site. I want to put my insert query like as Norse ask.. Commented May 7, 2012 at 4:49

2 Answers 2

1

A word 'Array' appears when PHP is trying to cast an array as a string.
So for the field appeared as Array you have to process it somehow, depends on your application logic, converting whatever array you have to a proper string.

The question has nothing to do with mysql though.

Also remember that you have to format your SQL strings. At the very least do

$var = mysql_real_escape_string($var);

for the every variable you're putting into query, and wrap it single quotes in the query.

Sign up to request clarification or add additional context in comments.

Comments

0

I don't know what your $_POST contains. But i assume you need this

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

EDIT: You have to specify a form method

<form method="post">

and finally the INSERT query as

$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]')";

3 Comments

@dlnGd0nG it doesn't work. Still an "Array" result was display in my database.
Post your insert query and the form (tip: click edit under the question)
@dlnGd0nG i already change it but now it didn't display anything. Item description that was choose by drop down menu and the price, qty and amount that input also didn't display in my database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.