mikebyrne Posted January 28, 2008 Share Posted January 28, 2008 When a user ticks the checkbox, fills in the textbox and presses proceed I want my code to put a 1 in the Orderscompleted field and whatever text is in the textbox into my Trackno field. My code is Processed.php <form action = "processed.php" method="post"> <table width="850" border="0" cellspacing="0" cellpadding="0"> <?php // let's get some data include('adminconnect.php'); $sql = mysql_query("SELECT OrderNo, Orderdate, Custname, Amount, Shippingmet FROM admin WHERE Ordercompleted = 0"); while( $row = mysql_fetch_array($sql) ) { // loop through and display ?> <tr align="left"> <td width="33"> </td> <td><input type = "checkbox" name="order[<?php echo $row['OrderNo'];?>]" value="checked"></td> <td width="33"> </td> <td width="82"><a href="javascript:openWindow('popup_detail.html', 'NewWindow', 615, 600)" class="black"><?php echo $row['OrderNo'];?></a></td> <td width="61" align="center"><?php echo $row['Orderdate'];?></td> <td width="230" align="Left"><?php echo $row['Custname'];?></td> <td width="172"><input type="text" class="order1Form1" name="tn" value="" /></td> <td width="56" align="right"><?php echo $row['Amount'];?></td> <td width="21"> </td> <td width="136" align="center"><?php echo $row['Shippingmet'];?></td> </tr> <? } ?> </table> <!-- data finish --> <!-- --> <div id="btn"> <input type="submit" value="Process"> <div id="btnSpace"> <!-- --> <!-- --> </div> </div> <div class="clr"> <!-- --> </div> <div class="padTop16"> <!-- --> </div> <div class="clr"> <!-- --> </div> <!-- btn finish --> <div class="clr"> <!-- --> </div> <!-- data content finish --> <!-- data btm start --> <div id="containerBg3"> <div class="padTop1"> <!-- --> </div> <div class="clr"> <!-- --> </div> </div> <div class="clr"> <!-- --> </div> <!-- data btm finish --> <!-- btm start --> <div id="containerBg1"> <div class="padTop15"> <!-- --> </div> <div class="clr"> <!-- --> </div> </div> <div class="clr"> <!-- --> </div> <div id="container"> <div id="line"> <!-- --> </div> </div> <div class="clr"> <!-- --> </div> <div class="padTop16"> <!-- --> </div> <div class="clr"> <!-- --> </div> <!-- btm finish --> </form> process.php <?php include('adminconnect.php'); $tbl1 = 'admin'; foreach($_POST['order'] as $orderno => $dmy){ $sql = "UPDATE $tbl1 SET Ordercompleted = 1 WHERE OrderNo= '{$orderno}'"; mysql_query($sql) or die("Failed on order {$orderno}"); } ?> Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/ Share on other sites More sharing options...
tapos Posted January 28, 2008 Share Posted January 28, 2008 seems ur code is right. Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-451448 Share on other sites More sharing options...
mikebyrne Posted January 28, 2008 Author Share Posted January 28, 2008 It will place the 1 ok but im not sure how to code for the textboxes and insert the correct info into the Trackno field Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-451451 Share on other sites More sharing options...
mikebyrne Posted January 28, 2008 Author Share Posted January 28, 2008 Would changing the textbox code to work?? <td width="172"><input type="text" class=""Trackno[<?php echo $row['Trackno];?>]" name="tn" value="" /></td> Im guessing i need to make an array like i have for the checkbox but not sure how to code it Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-451491 Share on other sites More sharing options...
mikebyrne Posted January 28, 2008 Author Share Posted January 28, 2008 Any tips on creating an array for these textsboxs? Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-451537 Share on other sites More sharing options...
rhodesa Posted January 28, 2008 Share Posted January 28, 2008 Let me try to explain this further. OrderNo is your unique id. Everything will revolve around this number. This form is doing a couple different things. The first, is a checkbox (as we discussed earlier today) to signify that the order is complete. So, we game the checkboxes a name (order). Then for each checkbox printed, we used that name along with the OrderNo in brackets like so: name="order[12345]". Because we are using the brackets, any data submitted will be an array with the OrderNo as the keys like so: array( '123' => 'checked', '456' => 'checked', ) So, let's apply the same logic to the text boxes. We will give them a name (let's say trackno). So our form code will look like: <form action = "processed.php" method="post"> <table width="850" border="0" cellspacing="0" cellpadding="0"> <?php // let's get some data include('adminconnect.php'); $sql = mysql_query("SELECT * FROM admin WHERE Ordercompleted = 0"); while( $row = mysql_fetch_array($sql) ) { // loop through and display ?> <tr align="left"> <td width="33"> </td> <td><input type = "checkbox" name="order[<?php echo $row['OrderNo'];?>]" value="checked"></td> <td width="33"> </td> <td width="82"><a href="javascript:openWindow('popup_detail.html', 'NewWindow', 615, 600)" class="black"><?php echo $row['OrderNo'];?></a></td> <td width="61" align="center"><?php echo $row['Orderdate'];?></td> <td width="230" align="Left"><?php echo $row['Custname'];?></td> <td width="172"><input type="text" name="trackno[<?php echo $row['OrderNo];?>]" value="<?php echo htmlspecialchars($row['Trackno']);?>" /></td> <td width="56" align="right"><?php echo $row['Amount'];?></td> <td width="21"> </td> <td width="136" align="center"><?php echo $row['Shippingmet'];?></td> </tr> <? } ?> </table> </form> This way, for an order with an OrderNo of 123 for example, the checkbox will be order[123] and the text box will be trackno[123]. The value="<?php echo $row['Trackno'];?>" was left there so that it populates the field with the current value in the database (if there is one), and added the htmlspecialchars() function to it to make sure it doesn't interfere with the HTML on the page. Now, onto the file processing the submitted data. $_POST will now have 2 entries. One for 'order' and another for 'trackno'. Each of those entries will be an array of key/value pairs, where the key is the OrderNo (aka what was in the brackets) and the value will be the value of the form element. For the checkboxes, the value will be 'checked', and for the text boxes, it will be whatever you enter into them on the form. So, for each of the key/values in both, we need to update the DB. You already have it started for the checkboxes. You have the following foreach: foreach($_POST['order'] as $orderno => $dmy){ This will loop over all of the key/value pairs for 'order', each time setting $orderno to the key, and $dmy to the value. Well, we don't care about the value of the checkboxes, cus it will always be checked. But for each $orderno, we want to update the database, which you do. Now, let's apply the same logic to the trackno field. It's the same style array, but the value this time will be the text you entered into the field. So, let's start with our foreach loop: <?php foreach($_POST['trackno'] as $orderno=>$text){ //our code to update the table will be here } ?> Now, how would be update the database? Well, it's similar to the checkboxes, but instead of 1, we will use $text: <?php foreach($_POST['trackno'] as $orderno=>$text){ $text = mysql_real_escape_string(trim($text)); $sql = "UPDATE $tbl1 SET Trackno = '{$text}' WHERE OrderNo= '{$orderno}'"; mysql_query($sql) or die("Failed on order {$orderno}"); } ?> The mysql_escape_string is there to escape any 'special' characters. Put it together, and you get: <?php include('adminconnect.php'); $tbl1 = 'admin'; //Mark Completed Orders foreach($_POST['order'] as $orderno => $dmy){ $sql = "UPDATE $tbl1 SET Ordercompleted = 1 WHERE OrderNo= '{$orderno}'"; mysql_query($sql) or die("Failed on order {$orderno}"); } //Update Tracking Numbers foreach($_POST['trackno'] as $orderno=>$text){ $text = mysql_real_escape_string(trim($text)); $sql = "UPDATE $tbl1 SET Trackno = '{$text}' WHERE OrderNo= '{$orderno}'"; mysql_query($sql) or die("Failed on order {$orderno}"); } ?> Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-451569 Share on other sites More sharing options...
mikebyrne Posted January 28, 2008 Author Share Posted January 28, 2008 Thanks for spelling the reasoning out to me it's helped alot I'm getting an error on line 223 which is <td width="172"><input type="text" name="trackno[<?php echo $row['OrderNo];?>]" value="<?php echo htmlspecialchars($row['Trackno']);?>" /></td> the error msg is Parse error: syntax error, unexpected T_STRING, expecting ']' in C:\xampp\htdocs\Admin_files\Unprocessed.php on line 223 Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-451644 Share on other sites More sharing options...
mikebyrne Posted January 28, 2008 Author Share Posted January 28, 2008 Fixed it, just missing a ' But the button doesnt work. Just trying to fix it now Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-451654 Share on other sites More sharing options...
mikebyrne Posted January 28, 2008 Author Share Posted January 28, 2008 Now getting the error Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Admin_files\processed.php on line 12 processed.php <?php include('adminconnect.php'); $tbl1 = 'admin'; //Mark Completed Orders foreach($_POST['order'] as $orderno => $dmy){ $sql = "UPDATE $tbl1 SET Ordercompleted = 1 WHERE OrderNo= '{$orderno}'"; mysql_query($sql) or die("Failed on order {$orderno}"); } //Update Tracking Numbers foreach($_POST['trackno'] as $orderno=>$text){ $text = mysql_real_escape_string(trim($text)); $sql = "UPDATE $tbl1 SET Trackno = '{$text}' WHERE OrderNo= '{$orderno}'"; mysql_query($sql) or die("Failed on order {$orderno}"); } ?> Line 12 is foreach($_POST['trackno'] as $orderno=>$text){ Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-451656 Share on other sites More sharing options...
mikebyrne Posted January 28, 2008 Author Share Posted January 28, 2008 Why would this line cause an error foreach($_POST['trackno'] as $orderno=>$text){ Cant figure this out Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-451694 Share on other sites More sharing options...
revraz Posted January 28, 2008 Share Posted January 28, 2008 Check out the examples http://us.php.net/foreach Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-451698 Share on other sites More sharing options...
mikebyrne Posted January 28, 2008 Author Share Posted January 28, 2008 I was looking at similar foreach examples and couldnt see any reason why the code im using wouldnt work Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-451712 Share on other sites More sharing options...
mikebyrne Posted January 29, 2008 Author Share Posted January 29, 2008 I've no idea how to fix this error Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-452154 Share on other sites More sharing options...
rajivgonsalves Posted January 29, 2008 Share Posted January 29, 2008 because $_POST['trackno'] is not an array... thats why you get invalid argument Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-452155 Share on other sites More sharing options...
mikebyrne Posted January 29, 2008 Author Share Posted January 29, 2008 Its there to post the Trackno into the field, how can I adjust this? Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-452158 Share on other sites More sharing options...
rajivgonsalves Posted January 29, 2008 Share Posted January 29, 2008 this name="trackno[<?php echo $row['OrderNo];?>]" should be name="trackno[]" Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-452161 Share on other sites More sharing options...
mikebyrne Posted January 29, 2008 Author Share Posted January 29, 2008 Seems to have fixed the error but doesnt post the trackno figures into the field <?php include('adminconnect.php'); $tbl1 = 'admin'; //Mark Completed Orders foreach($_POST['order'] as $OrderNo => $dmy){ $sql = "UPDATE $tbl1 SET Ordercompleted = 1 WHERE OrderNo= '{$OrderNo}'"; mysql_query($sql) or die("Failed on order {$OrderNo}"); } //Update Tracking Numbers foreach($_POST['Trackno'] as $OrderNo => $text){ $text = mysql_real_escape_string(trim($text)); $sql = "UPDATE $tbl1 SET Trackno = '{$text}' WHERE OrderNo= '{$OrderNo}'"; mysql_query($sql) or die("Failed on order {$OrderNo}"); } ?> Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-452167 Share on other sites More sharing options...
rajivgonsalves Posted January 29, 2008 Share Posted January 29, 2008 this <td width="172"><input type="text" name="trackno[<?php echo $row['OrderNo];?>]" value="<?php echo htmlspecialchars($row['Trackno']);?>" /></td> should be <td width="172"><input type="text" name="trackno[]" value="<?php echo $row['OrderNo];?>,<?php echo htmlspecialchars($row['Trackno']);?>" /></td> and change your code too foreach($_POST['Trackno'] as $strKey => strValue){ list($OrderNo,$text) = explode(",",$strValue); $text = mysql_real_escape_string(trim($text)); $sql = "UPDATE $tbl1 SET Trackno = '{$text}' WHERE OrderNo= '{$OrderNo}'"; mysql_query($sql) or die("Failed on order {$OrderNo}"); } Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-452168 Share on other sites More sharing options...
mikebyrne Posted January 29, 2008 Author Share Posted January 29, 2008 Is this line correct?? <td width="172"><input type="text" name="trackno[]" value="<?php echo $row['OrderNo];?>,<?php echo htmlspecialchars($row['Trackno']);?>" /></td> Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-452177 Share on other sites More sharing options...
rajivgonsalves Posted January 29, 2008 Share Posted January 29, 2008 yes it looks correct.. Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-452178 Share on other sites More sharing options...
mikebyrne Posted January 29, 2008 Author Share Posted January 29, 2008 Just that I'm getting a error Parse error: syntax error, unexpected T_STRING, expecting ']' in C:\xampp\htdocs\Admin_files\Unprocessed.php on line 223 Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-452180 Share on other sites More sharing options...
rajivgonsalves Posted January 29, 2008 Share Posted January 29, 2008 paste your code for that particular line Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-452181 Share on other sites More sharing options...
mikebyrne Posted January 29, 2008 Author Share Posted January 29, 2008 <td width="172"><input type="text" name="trackno[]" value="<?php echo $row['OrderNo];?>,<?php echo htmlspecialchars($row['Trackno']);?>" /></td> Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-452186 Share on other sites More sharing options...
rajivgonsalves Posted January 29, 2008 Share Posted January 29, 2008 Ok got it I forgot to put a single quote here you go <td width="172"><input type="text" name="trackno[]" value="<?php echo $row['OrderNo'];?>,<?php echo htmlspecialchars($row['Trackno']);?>" /></td> Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-452188 Share on other sites More sharing options...
mikebyrne Posted January 29, 2008 Author Share Posted January 29, 2008 This puts the order number into the TrackNo textboxes? I want the textboxes to be blank and whatever the user inputs into the box goes into the Tracno field Link to comment https://forums.phpfreaks.com/topic/88226-solved-inserting-2-records-into-a-table-when-checkbox-is-checked/#findComment-452192 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.