Forum Moderators: coopster

Message Too Old, No Replies

insert form variable in 1 table and while loop variables (array) in 2

         

diginamics

8:31 pm on Jan 27, 2011 (gmt 0)

10+ Year Member



First page adds a new job number, then the order page loaded with the job number id as a get id. Basically there is a while loop in the order page which shows products/services client can order and he chooses what he requires 'one or eight services' (8 in total) and some other variables like date of order and client name etc. Here is the order item code

The first sql statement executes fine, but in the second sql query nothing happens

Have a look at this image and you will understand where the problem is

[dubads.com ]

[PHP]
$sql="insert into job_order(order_num,order_date,order_customer_id,
order_remarks)
values(".$_GET['id'].",NOW(),".$_POST['companyBox'].",'".$_POST['remarkBox']."');";
$res=mysql_query($sql);



foreach($_POST as $key => $value) {


if(!empty($value)) {


$order="INSERT INTO orderprod (order_num,prod_id,order_amount,teeth_amount) VALUES
('$_GET[id]','$value','$value','$value');";
$orderres=mysql_query($order) or die(mysql_error());
}
}
?>
[/PHP]
=============

This is the formI have removed parts which are irrelevant. Please note that dateBox and companyBox are not required to be looping as they are only for first table,

[PHP]
echo "<form action=".$config_basedir."./vieworder.php?id=".$_GET['id']." name=form1 method=post>";
?>
<table>
<tr>
<td><h4>JOB ORDER</H4></TD><TD></TD>
</TR>
<TR>
<TD>ORDER NUMBER</TD><TD><?PHP ECHO $_GET['id'] ?></td>
</tr>
<tr>
<td>ORDER DATE</td><td><input type=text name=dateBox></td>
</tr>

<tr>
<td>COMPANY NAME</td><td>
<?PHP
$sql="select * from customers";
$res=mysql_query($sql);
echo "<select name=companyBox><option value=''>Please select</option>";
WHILE($fetch=mysql_fetch_assoc($res)){

echo "<option value='".$fetch['id']."'>".$fetch['cust_name']."</option>";}
echo "</select>";
echo "</td></tr>";
?>



</table>

<table>
<tr>
<th>ITEM</th><th>QUANTITY</th><th>N0. of Teeths</th>
</tr>
<tr>
<?PHP
$sql="select * from products";
$res=mysql_query($sql);

WHILE($fetch=mysql_fetch_assoc($res)){

echo "<td><input type=text name=desBox value='".$fetch['prod_id']."'>".$fetch['prod_name']."</td>
<td><input type=text name=quantBox></td>
<td><input type=text name=teethBox></td>";
echo "</tr>";}
echo "</table>";
?>
[/PHP]

Have a look at this image and you will understand where the problem is

[dubads.com...]

Matthew1980

8:54 pm on Jan 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there diginamics,

Welcome to WebmasterWorld!

If the second query is the issue, have you actually tried to echo the sql to screen then, place an exit; directly after it so that you can see exactly how the string is being populated? That would be the first action to take.

Also, $_POST & $_GET sanitising is an absolute MUST, please, for the safety of your DB don't place that data into your sql string, as it has the potential to harm your hard work. This is good practise even if this is only an intranet..

Not quite sure what your doing here: $key.' => '.trim(strip_tags($value));

You could do with putting some error handling on the mysql_query() functions too, during development this is essential:-

mysql_query() or die(msql_error());

The once you iron the kinks out just remove that little bit - or shove it in a ternary and set a constant to act as a switch for it :)

Also, you could do with echoing the $_POST/$_GET array after the submit is pressed, to see exactly how the array stands, typically if the last entry is being inserted this means that the loop isn't functioning correctly, so echo the sql to see. After looking at the picture again, I see what you mean, that and a fresh cuppa :)

That's all I can see for now..

Cheers,
MRb

diginamics

9:11 pm on Jan 27, 2011 (gmt 0)

10+ Year Member



Hi MRB:
Thanks for the response and welcoming note.

Please check the image and you will understand where I am having trouble.

Of course I will be santising the $_POST AND $_GET. It is only for now. With regards to the $key.'=>.... I had removed that earlier.

I am using mysql_query() or die(mysql_error()); in the second query.

As I wrote earlier the first query parses through. Even the second query inserts one row into the table (orderprod) but that is the problem. There are eight services(rows) and the client can require anywhere from 1 to 8 services, but the problem is it still only insert the last row (#8).

Please check this image and you would understand.

[dubads.com ]

Matthew1980

9:40 pm on Jan 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there diginamics,

Ok, understood, and from what I can see from the code you have posted you haven't closed the form tag & I can't see a submit button; but that's not to say it isn't there.

From what I can see you are only wanting to cycle through the submitted data, and only insert IF the values from text boxes have value (I would suggest doing a preg_match to sanitise the input to ensure that you only get int's passed - just to be safe.) and skip if that particular text box is empty, as it can be isset($_POST['a_name']) && empty($_POST['a_name']) (have state but empty...)

I'm pretty sure that you would need to set a counter up so that the input boxes have individual names, because if the names are the same, then loop iterations will be overwritten, until the last one is parsed.

Before you get the Db involved, just get the array passed into the script, and check that it loops through as expected, check that only the boxes your filling have state, then proceed from there.

I hope that makes sense; it's tired and I'm late :)

Cheers,
MRb

diginamics

9:51 pm on Jan 27, 2011 (gmt 0)

10+ Year Member



Hi MRB:

I am note an expert in PHP. If it is not a trouble for you can you please type how to set up a counter at your convenience. In any case, thanks for you help.

diginamics

10:04 pm on Jan 27, 2011 (gmt 0)

10+ Year Member



Here is the output by echoeing the query.

I need only the array values (in the while loop) and it is bringing out the date, clientid, remarks and even the submit as well.

Anyways, at your convenience whenever you have time please have a look, (when it is not tired and you are not late)

INSERT INTO orderprod (order_num,prod_id,order_amount,teeth_amount) VALUES ('1000','2010-10-10','2010-10-10','2010-10-10');INSERT INTO orderprod (order_num,prod_id,order_amount,teeth_amount) VALUES ('1000','1','1','1');INSERT INTO orderprod (order_num,prod_id,order_amount,teeth_amount) VALUES ('1000','8','8','8');INSERT INTO orderprod (order_num,prod_id,order_amount,teeth_amount) VALUES ('1000','12','12','12');INSERT INTO orderprod (order_num,prod_id,order_amount,teeth_amount) VALUES ('1000','2','2','2');INSERT INTO orderprod (order_num,prod_id,order_amount,teeth_amount) VALUES ('1000','asf','asf','asf');INSERT INTO orderprod (order_num,prod_id,order_amount,teeth_amount) VALUES ('1000','submit','submit','submit');

diginamics

10:17 pm on Jan 27, 2011 (gmt 0)

10+ Year Member



Hi MRB:

Thanks anyway, I got it resolved by making array of only the while loop variables