0

I want insert some rows into mysql, each row shold be like this:

<input name="code[]" value="{code item dari data mysql}" /> 
<input name="name[]" value="{nama dari data mysql}" /> 
<input name="hrg[]" value="{harga dr mysql}" /> 
<input name="qty[]" value="{qty diinput sendiri}" /> 
<input name="subt[]" value="{autocalculated saat mengisi qty}" />

The processor:

<?php //if i press submit on form, then: 
for($i=0;$i<count($qty);$i++) 
{ 
   $query="INSERT INTO transaksi VALUES(NULL, '$code[$i]', '$name[$i]' , '$price[$i]' , '$qty[$i]' , '$subt[$i]') ";
   mysql_query($query); 
} ?>

But it produces bad results:
The datas inserted beeing like these:

id | code | name | price | qty | subt
1, 'A','A',100,2,200
2, 'R','R',100,2,200
3, 'R','R',100,2,200
4, 'A','A',100,2,200
5, 'Y','Y',100,2,200

$code and $nama variables all contains data: "ARRAY", so it read them as $code[0] = "A", $code[1]="R" etc.

what's wrong with my code?
plz help me.

2
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by accountholder
0

Well the first problem is you didn't specify a type of input. This isn't the main problem, but causes the document to be an invalid (X)HTML page. I will look more into your problem and see what the real cause of it is...

EDIT:
I notice that in the first snippet, you have qty containing an array, but in your PHP you are using it as a single variable.

The other thing you should note is making an input an array only works when there is multiple inputs of the same type with that exact name.

Edited by FlashCreations: n/a

0

in my original code, i specified the input type.
Bellow is my original code:
Plz note, i loop the row, so there was some fields with same name.

<?php
$no=1;
$sq=$sql("SELECT * FROM pin_product ORDER BY price, name");
while($row=$ros($sq))
{
?>        <tr>
          <td><div align="right"><?=$no?>.</div></td>
          <td><div align="center"><!--input type="hidden" name="id[]" id="" value="<?=$row[id]?>" /-->
		      <input name="id[]" type="hidden" id="id<?=$no?>" value="<?=$no?>" />
		      <input name="codes[]" type="text" id="code<?=$no?>" value="<?=$row['code']?>" size="1" maxlength="5" />
		  </div></td>
          <td><!--?=$row[name]?-->
            <input name="name[]" type="text" id="name[]" /></td>
          <td><div align="right">
            <input name="price[]" type="text" id="price<?=$no?>" size="10" maxlength="10" value="<?=$row[price]?>" style="border:0" readonly="" dir="rtl" />
          </div></td>
          <td><div align="right">
            <input name="pin[]" type="text" id="pin<?=$no?>" size="3" maxlength="3" value="<?=$row[amount]?>" style="border:0" dir="rtl" readonly="" />
          </div></td>
          <td><div align="right">
            <input name="qty[]" type="text" id="qty<?=$no?>" size="3" maxlength="3"  onfocus="startHitung()" onblur="stopHitung()" />
          </div></td>
          <td><div align="right">
            <input name="sub[]" type="text" id="sub<?=$no?>" size="8" maxlength="8" readonly="" style="border:0" dir="rtl" />
          </div></td>
          <td><div align="right">
            <input name="sub2[]" type="text" id="sub2<?=$no?>" size="8" maxlength="8" readonly="" style="border:0" dir="rtl" />
          </div></td>
          </tr>
<tr>
<? 
$no++;
} ?>

then the processor is like this:

if($actions=="add")
{
  $jmlh=count($qty);
  for($i=0; $i < $jmlh; $i++)
 {
	if($qty[$i]>0)
	{
		echo "$name[$i]-$codes[$i]-$qty[$i]-$sub2[$i]-$sub[$i]<br />";
		//$sql("INSERT INTO pin_transaction VALUES(NULL, '$dates', NOW(), '$reguid', 'BUY', '$codes[$i]', $qty[$i], '$sub2[$i]', '$sub[$i]', 1)")or die(mysql_error());
             // $name[$i] and $codes[$i] produced A,R,R,A and Y :((
	}
}

}

making an input an array only works when there is multiple inputs of the same type with that exact name.

yes, all fields using [] are multiple rows. i looped it, but in first html code, i only show 1 row. i said as "each".

I notice that in the first snippet, you have qty containing an array, but in your PHP you are using it as a single variable.

correct, i was googling, and i found that's the method.
What i do not understand to is: why my code produced splitted "ARRAY" word?
Thanx Sir.

Edited by accountholder: looping

0

sir, i solve it self.
the problem is:
i call the above "post" method form mixed with "get" method using $_REQUEST[].

I create the new one and it worked because all is in "post" method.

<?php

$konek=mysql_connect("localhost", "root","caroline");
$selek=mysql_select_db("glife2u");
$sql=mysql_query;
$ros=mysql_fetch_array;
$name=$_POST['name'];
$descr=$_POST['descr'];
$amount=$_POST['amount'];
$action=$_POST['action'];

if($action=="add")
{
$limit = count($amount);
for($i=0;$i<$limit;$i++) {
    $sql("INSERT INTO kelas VALUES (NULL,'$name[$i]','$descr[$i]','$amount[$i]')");
	} 
}
?>
<form action="" method="post" name="xxx">
<table width="100%" cellpadding="1" cellspacing="1">

<tr>
  <td width="7%">No.</td>
<td width="17%" height="22">Nama</td>
<td width="17%">Kegiatan</td>
<td width="59%">Kelas</td>
</tr>
<?
for($e=1;$e<=3;$e++)
{
?>
<tr>
  <td><?=$e?></td>
<td><input name="name[]" type="text" /></td>
<td><input name="descr[]" type="text" id="descr[]" /></td>
<td><input name="amount[]" type="text" id="amount[]" size="1" maxlength="1" /></td>
</tr>
<? } ?>
<tr>
  <td colspan="4"><input name="action" type="hidden" id="action" value="add">  <input type="submit" name="Submit" id="Submit" value="Submit" /></td>
  </tr>
</table>
</form>

btw thanx 4 ur hep

Edited by accountholder: n/a

0

Oh Ok...I understand the problem now! Sorry about that. Well I'm glad you solved your problem, all I ask is that you mark this thread as solved so others don't attempt to solve this already fixed problem.
Thanks!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.