Jump to content

[SOLVED] Passing data into Sql query


Bifter

Recommended Posts

Hi All,

 

I have two scripts, index.php and email-details.php; index.php among other things passes data via GET to email-details.php into an sql query, however the output only displays the first row, just repeated. How do I display only the rows where the checkboxes has been checked in index.php ?

 

index.php

while ($row = mysql_fetch_assoc($resultp)) {
  echo "<tr>";
  echo "<td>" . "<div align=\"center\">". "<input type=\"checkbox\" name=\"checkbox[]\" value=\"1\" ></div></td>";
  echo "<input type=\"hidden\" name=\"id[]\" id=\"id[]\" value=\"" . $row['ID'] . "\"></td>";
  echo "<td>" . "<div align=\"center\">". $row['date'] . "</div><input type=\"hidden\" name=\"date[]\" id=\"date[]\" value=\"" . $row['date'] . "\"></td>";
  echo "<td>" . "<div align=\"center\">". $row['company'] . "</div><input type=\"hidden\" name=\"company[]\" id=\"company[]\" value=\"" . $row['company'] . "\"></td>";
  echo "<td>" . "<div align=\"center\">". $row['product'] . "</div><input type=\"hidden\" name=\"product[]\" id=\"product[]\" value=\"" . $row['product'] . "\"></td>";
  echo "<td>" . "<div align=\"center\">". $row['serial'] . "</div><input type=\"hidden\" name=\"serial[]\" id=\"serial[]\" value=\"" . $row['serial'] . "\"></td>";
  echo "<td>" . "<div align=\"center\">". $row['mac'] . "</div><input type=\"hidden\" name=\"mac[]\" id=\"mac[]\" value=\"" . $row['mac'] . "\"></td>";
  echo "<td>" . "<div align=\"center\">" . "<a href='edit-details.php?cmd=edit&id=$id'>edit</a>" . "  |  " . "<a href='' onclick='disp_confirm()';'>Delete</a>" . "</td>";
  echo "</tr>";
}

email-details.php

$checkbox = implode(', ', $_GET['checkbox']);
$date = implode(', ', $_GET['date']);
$company = implode(', ', $_GET['company']);
$product = implode(', ', $_GET['product']);
$serial = implode(', ', $_GET['serial']);
$mac = implode(', ', $_GET['mac']);
$id = implode(', ', $_GET['id']);


$rowtotal = count($_GET['checkbox']);

$result = mysql_query("SELECT * FROM users WHERE company='$company'");
while ($data=mysql_fetch_assoc($result)){
echo "Hello " .$data['firstname'] ." " . $data['surname'] . ",<br /><br />";
}
echo "Thank you for purchasing from c, below you will find a table of serial numbers and MAC address' for the products you recently purchased:<br /><br />";
echo "<table width=\"97%\" border=\"0\" align=\"center\">";
echo "<thead>
  <tr>
    <th scope=\"col\">Date</th>
    <th scope=\"col\">Product</th>
    <th scope=\"col\">Serial</th>
    <th scope=\"col\">MAC Address</th>
  </tr>
  </thead>";
$i = 0;
while ($i < $rowtotal) {
echo "<tr><td><div align=\"center\">";
      $result = mysql_query("SELECT * FROM details WHERE id='$id'"); 
while ($data=mysql_fetch_assoc($result)){
echo "" .$data['date'];
}
echo "</div></td>";
echo "<td><div align=\"center\">";
      $result = mysql_query("SELECT * FROM details WHERE id='$id'"); 
while ($data=mysql_fetch_assoc($result)){
echo "" .$data['product'];
}
echo "</div></td>";
echo "<td><div align=\"center\">";
      $result = mysql_query("SELECT * FROM details WHERE id='$id'"); 
while ($data=mysql_fetch_assoc($result)){
echo "" .$data['serial'];
}
echo "</div></td>";
echo "<td><div align=\"center\">";
      $result = mysql_query("SELECT * FROM details WHERE id='$id'"); 
while ($data=mysql_fetch_assoc($result)){
echo "" .$data['mac'];
}
echo "</div></td>";
$i++;
}

 

Thanks for looking.

Link to comment
https://forums.phpfreaks.com/topic/141355-solved-passing-data-into-sql-query/
Share on other sites

Not sure where you are really coming at with this... What is the script trying to do?

If you are trying to show what a customer has ordered, and list the products.

 

There should really be an order table, with order number, date and link to user who placed order and table with products contained in that order. Then when you post an order id for that page, you will be able to access all the associated data for that.

 

On the other hand. If you are trying to have a table where the user selects rows, and it posts the list of rows they have selected to another page. Then see the below script for reference

 

 

$i = 0;

while ($row = mysql_fetch_assoc($resultp)) {
  echo "<tr>";
  
  
  echo "<td>" . "<div align=\"center\">". "<input name=\"checked[$i]\" type=\"checkbox\" value=\"yes\"></div></td>";
echo "<input type=\"hidden\" name=\"ID[$i]\" value = \"".$row['ID']."\">";


  echo "<td>" . "<div align=\"center\">". $row['date'] . "</div><</td>";
  echo "<td>" . "<div align=\"center\">". $row['company'] . "</div></td>";
  echo "<td>" . "<div align=\"center\">". $row['product'] . "</div></td>";
  echo "<td>" . "<div align=\"center\">". $row['serial'] . "</div></td>";
  echo "<td>" . "<div align=\"center\">". $row['mac'] . "</div></td>";
  echo "<td>" . "<div align=\"center\">" . "<a href='edit-details.php?cmd=edit&id=$id'>edit</a>" . "  |  " . "<a href='' onclick='disp_confirm()';'>Delete</a>" . "</td>";
  echo "</tr>";


++$i;



}

 

if ($_POST['submit']){

$size = count($_POST['rawID']);
$i = 0;
while ($i < $size) {

if (($_POST['checked'][$i]=='yes')){


$id = $_POST['ID'][$i]);
$query = "SELECT * FROM details WHERE id='$id'";

$result = mysql_query($query) or die(mysql_error());


while($data = mysql_fetch_array($result)){
echo "" .$data['date'];
echo "" .$data['product'];
echo "" .$data['serial'];
echo "" .$data['mac'];

}
++$i;
}
}

Basicly after searching a database of products sold this is returned within index.php with checkboxs next to each row returned, if the checkbox is ticked I would like that row sent to email-details.php where they will be emailed, the email part is not an issue, but for now I would like them to be displayed in a table in the browser.

 

The code im currently is as follows:

 

index.php

<?php
while ($row = mysql_fetch_assoc($resultp)) {
  echo "<tr>";
  $id = $row['ID'];
  echo "<td>" . "<div align=\"center\">". "<input name=\"checked[$id]\" type=\"checkbox\" value=\"yes\"></div></td>";
  echo "<input type=\"hidden\" name=\"rawID\" value = \"".$row['ID']."\">";
  echo "<td>" . "<div align=\"center\">". $row['date'] . "</div><input type=\"hidden\" name=\"date\" id=\"date\" value=\"" . $row['date'] . "\"></td>";
  echo "<td>" . "<div align=\"center\">". $row['company'] . "</div><input type=\"hidden\" name=\"company\" id=\"company\" value=\"" . $row['company'] . "\"></td>";
  echo "<td>" . "<div align=\"center\">". $row['product'] . "</div><input type=\"hidden\" name=\"product\" id=\"product\" value=\"" . $row['product'] . "\"></td>";
  echo "<td>" . "<div align=\"center\">". $row['serial'] . "</div><input type=\"hidden\" name=\"serial\" id=\"serial\" value=\"" . $row['serial'] . "\"></td>";
  echo "<td>" . "<div align=\"center\">". $row['mac'] . "</div><input type=\"hidden\" name=\"mac\" id=\"mac\" value=\"" . $row['mac'] . "\"></td>";
  echo "<td>" . "<div align=\"center\">" . "<a href='edit-details.php?cmd=edit&id=$id'>edit</a>" . "  |  " . "<a href='' onclick='disp_confirm()';'>Delete</a>" . "</td>";
  echo "</tr>";
}
mysql_free_result($resultp);
<?

 

email-details.php

<?php
echo "<table width=\"97%\" border=\"0\" align=\"center\">";
echo "<thead>
  <tr>
    <th scope=\"col\">Date</th>
    <th scope=\"col\">Product</th>
    <th scope=\"col\">Serial</th>
    <th scope=\"col\">MAC Address</th>
  </tr>
  </thead>";
$id = $_GET['rawID'];
$size = count($_GET['checked'][$id]=='yes');
echo $size;
$ia = 0;
while ($ia < $size) {
$id = $_GET['rawID'];
if (($_GET['checked'][$id]=='yes')){
$query = "SELECT * FROM details WHERE id='$id'";
$result = mysql_query($query) or die(mysql_error());
while($data = mysql_fetch_array($result)){
echo "<td><div align=\"center\">";
echo "" .$data['date'];
echo "</div></td>";
echo "<td><div align=\"center\">";
echo "" .$data['product'];
echo "</div></td>";
echo "<td><div align=\"center\">";
echo "" .$data['serial'];
echo "</div></td>";
echo "<td><div align=\"center\">";
echo "" .$data['mac'];
echo "</div></td>";
}
}
++$ia;
}

?>
</tr></table>
<?

 

The problem is only the last row is returned, no matter how many checkboxes are ticked.

 

I hope i have explained it a bit better this time - fingers crossed!

Try

 


echo "<form name=\"form1\" method=\"post\" action='email-details.php' enctype=\"multipart\form-data\">";
echo "<div align=\"center\"><table>";
while ($row = mysql_fetch_assoc($resultp)) {
  echo "<tr>";

  echo "<td><input name=\"checked[".$row['ID']."]\" type=\"checkbox\" value=\"yes\"></td>";

  echo "<td>". $row['date'] . "</div></td>";
  echo "<td>". $row['company'] . "</div></td>";
  echo "<td>". $row['ItemTitle'] . "</div></td>";
  echo "<td>". $row['serial'] . "</div></td>";
  echo "<td>". $row['mac'] . "</div></td>";
  echo "<td>" . "<a href='edit-details.php?cmd=edit&id=$id'>edit</a>" . "  |  " . "<a href='' onclick='disp_confirm()';'>Delete</a>" . "</td>";
  echo "</tr>";

}
echo "<input type='submit'>";
echo "</table></div>";

 

 

 


echo "<table width=\"97%\" border=\"0\" align=\"center\">";
echo "<thead>
  <tr>
    <th scope=\"col\">Date</th>
    <th scope=\"col\">Product</th>
    <th scope=\"col\">Serial</th>
    <th scope=\"col\">MAC Address</th>
  </tr>
  </thead>";


foreach ($_POST['checked'] AS $key => $val){
$query = "SELECT * FROM details WHERE id='$key'";
$result = mysql_query($query) or die(mysql_error());
while($data = mysql_fetch_array($result)){
echo "<tr><td><div align=\"center\">";
echo "" .$data['date'];
echo "</div></td>";
echo "<td><div align=\"center\">";
echo "" .$data['product'];
echo "</div></td>";
echo "<td><div align=\"center\">";
echo "" .$data['serial'];
echo "</div></td>";
echo "<td><div align=\"center\">";
echo "" .$data['mac'];
echo "</div></td></tr>";

}
}




?>
</tr></table>
<?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.