0

I have this PHP code/form:

<form method="post" action="tickets_report2.php">
<table width="800" border="0" cellspacing="5" cellpadding="5">
  <tr>
    <td><strong>Select</strong></td>
    <td><strong>Company</strong></td>
  </tr>
  <?php
  $sql="SELECT * from customer ";
  $rs=mysql_query($sql,$conn) or die(mysql_error());
  $counter=0;
  while($result=mysql_fetch_array($rs)) {
    $counter++;
    echo '<tr>
       <td><input type="checkbox" value="'.$result["sequence"].'" name="checkbox'.$counter.'" /></td>
       <td>'.$result["company"].'</td>
    </tr>';
  }
  echo '<input type="hidden" name="counter" value="'.$counter.'" />';
  ?>
  </table>
  <input type="submit" name="submit" value="Next" />
</form>

and on the form action page:

<table width="800" border="0" cellspacing="5" cellpadding="5">
  <tr>
    <td><strong>Company</strong></td>
  </tr>
  <?php
  for($i=1; $i<=$_POST["counter"]; $i++) {
    if($_POST["checkbox$i"]) {
      $sql="SELECT * from customer where sequence = '".$i."' ";
      $rs=mysql_query($sql,$conn) or die(mysql_error());
      $result=mysql_fetch_array($rs);       
      echo '<tr>
                    <td>'.$result["company"].'</td>
      </tr>';
    }
  }
  ?>
</table>

lets say i check the checkbox on the form for the row in the database with sequence of 278, the SQL Query should say SELECT * from customer where sequence = '278' but its showing SELECT * from customer where sequence = '1'

i think its using the counter rather than the customer sequence

what do i need to change to get this working correctly so it selected the correct customer(s)

6
  • Just a little question back: Why do you think that code should display that data at all? Commented Aug 31, 2013 at 14:40
  • can you post the result of print_r($_REQUEST) from the form action page? Commented Aug 31, 2013 at 14:43
  • This could be your solution: add every checkbox to an array by doing so: <input type="checkbox" name="chknames[]" value="'.$result["sequence"].'"/. after that you can just do if(isset($_POST['chknames'])) Commented Aug 31, 2013 at 14:44
  • what is the value of counter when you POST it Commented Aug 31, 2013 at 14:44
  • if i echo $_POST["counter"] its 112 Commented Aug 31, 2013 at 14:48

2 Answers 2

1

On the form action page it should read:

if($_POST["checkbox$i"]) {
    $sql="SELECT * from customer where sequence = '".$_POST["checkbox$i"]."' ";

Note: sanitize your inputs.

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

Comments

0

form page:

<form method="post" action="tickets_report2.php">
<table width="800" border="0" cellspacing="5" cellpadding="5">
  <tr>
    <td><strong>Select</strong></td>
    <td><strong>Company</strong></td>
  </tr>
<?php
$sql="SELECT * from customer ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$counter=0;
while($result=mysql_fetch_array($rs))
{
    $counter++;
    echo '<tr>
         <td><input type="checkbox" value="'.$result["sequence"].'" name="checkbox['.$counter.']" /></td>
         <td>'.$result["company"].'</td>
         </tr>';
}
?>
</table>
<input type="submit" name="submit" value="Next" />
</form>

action page:

<table width="800" border="0" cellspacing="5" cellpadding="5">
  <tr>
    <td><strong>Company</strong></td>
  </tr>
<?php
foreach($_POST["checkbox"] as $value)
{
        $sql="SELECT * from customer where sequence = '".$value."' ";
        $rs=mysql_query($sql,$conn) or die(mysql_error());
        $result=mysql_fetch_array($rs);     
        echo '<tr>
                    <td>'.$result["company"].'</td>
                  </tr>';
}
?>
</table>

Comments

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.