Jump to content

[SOLVED] Inserting 2 records into a table when checkbox is checked


mikebyrne

Recommended Posts

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
Share on other sites

  • Replies 50
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.