# PHP dynamic page + return results into a dynamic table



## shuggans (Jan 23, 2013)

***Unsolved***
Having trouble with the following code.  The page worked until I tried to create the table to return results into rows with - now I get a 500 error when runnign the script.  Any help is greatly appreciated.


```
<?php
mysql_connect("localhost", "invuser", "password") or die(mysql_error());
mysql_select_db("ITinventory") or die(mysql_error());

$result = mysql_query("SELECT * FROM Equipment")
or die(mysql_error());  
  echo "<html>";
  echo "<body bgcolor=0066FF link=ffffff vlink=ffffff alink=ffffff>";
  echo "<div style='width: 100%; height: 100%;'>"; 
  echo "<font color=ffffff>";
  echo '<h1 align=center>Inventory Listing</h1>';
  echo '<a href="./index.html">Back</a>';
  echo "<center><table style='width: 65%; background-color: 000066;'><tr><td width='100%'>";
  echo "<font color=ffffff>";
  echo '<h3>Current Items:</h3>';
  echo "<div style='width: 100%; height: 350px; overflow: scroll; background-color: 0066FF;'>";
  echo "<table width="100%">";
  $count = 0;
while ($row = mysql_fetch_array( $result ))
{
  echo "<tr>";
  echo "<td width="25%">";
  echo "ID: ".$row['ID']." | ";
  echo "</td>";
  echo "<td width="25%">";
  echo "Name: ".$row['Name']." | ";
  echo "</td>";
  echo "<td width="25%">";
  echo "Type: ".$row['Type']." | ";
  echo "</td>";
  echo "<td width="25%">";
  echo "Model: ".$row['Model']." | ";
  echo "</td>";
  echo "</tr>";
  echo '<br />';
  $count++;
}
echo "</table>";
echo '</font>';
echo '</div>';
echo "</font>";

echo '</td></tr></table>';
echo "<table style='width: 65%; background-color: 000066;'><tr><td width=32%><a href='./index.html'><font color=ffffff><b>Back</b></a> to inventory search page.</font></td>";
echo "<td width=32%' align='right'><font color=ffffff>Item Count: <font color=ffff00>".$count."</font></td></tr>";
echo '</table></center><br /><br /><br />';
echo "</div>";
echo "</html>";

?>
```


----------



## shuggans (Jan 23, 2013)

Nevermind - needed to format these right:

```
echo "<table width='100%'>";
```

More issues below though...


----------



## W1zzard (Jan 23, 2013)

you probably want to turn on error reporting in php.ini. that will give much better error messages

also i suggest to separate code and design more. put the php code at the start, save data into php variables and then use a minimum of php code in the html section, basically just echo and foreach. that also saves you from having all those echos around.

something like that

```
<?php 
$foo='bar';
?>
<html><body><?php echo $foo; ?></body></html>
```


----------



## Aquinus (Jan 23, 2013)

CLI PHP can be your best friend. I plopped your code in a file named "testop.php".


```
~$ php -l testop.php 
PHP Parse error:  syntax error, unexpected T_LNUMBER, expecting ',' or ';' in testop.php on line 17
Errors parsing testop.php
```


----------



## shuggans (Jan 24, 2013)

In the same project: I have an edit button and a delete button to manipulate the checkbox items.  Delete is working for multiple + single items, but Trying to repopulate selected items into a table for editing when the edit button is used is more of a challenge. (will add text fields later),  cant seem to get the loop right...

```
<?php
  $term = $_POST[SelectedItems];
  $delete = $_POST[delete];
  $edit = $_POST[edit];
$connection = mysql_connect("localhost", "invuser", "Password");
if (!$connection)
  {
  die('Error: ' . mysql_error());
  }

mysql_select_db("ITinventory", $connection);


if ($delete == 'Delete') {

  while (list ($key,$val) = @each ($term)) {
    $result="DELETE FROM Equipment WHERE ID = $val";
    mysql_query($result,$connection);
    header("Location: ./index.php");
    exit();
  }
}
else if ($edit == 'Edit') {
  echo "<HTML>";
  echo "<Body>";
  echo "<table border='1' width='100%'>";
  while (list ($key,$val) = @each ($term)) {
    $result="SELECT * FROM Equipment WHERE ID = '$val'";
    $row = mysql_fetch_array( $result );

//    while ($row = mysql_fetch_array( $result )) {
    echo '<tr>';
    echo '<td width=50%>';
   echo $row['Name'];
    echo '</td>';
//    echo '<td>';
//    echo $row['Type'];
//    echo '</td>';
//    echo '<td>';
//    echo $row['Model'];
//    echo '</td>';
//    echo '<td>';
//    echo $row['SerialNumber'];
//    echo '</td>';
//    echo '<td>';
//    echo $row['ITAssetTag'];
//    echo '</td>';
//    echo '<td>';
//    echo $row['Location'];
//    echo '</td>';
 //   echo '<td>';
//    echo $row['Notes'];
//    echo '</td>';
    echo '</tr>';
//}
  }
echo "</table>";
  echo "</body>";
  echo "</html>";
}





//}
//
//}
//}


mysql_close($connection);
?>
```

What I'm trying to do is receive the ID numbers from the previous pages form(working), and for each of those return it's data from the mySQL table into a table on the page.

I worked through it! Here is what I came up with:

```
<?php
  $term = $_POST[SelectedItems];
  $delete = $_POST[delete];
  $edit = $_POST[edit];
$connection = mysql_connect("localhost", "invuser", "Password");
if (!$connection)
  {
  die('Error: ' . mysql_error());
  }

mysql_select_db("ITinventory", $connection);


if ($delete == 'Delete') {

  while (list ($key,$val) = @each ($term)) {
    $result="DELETE FROM Equipment WHERE ID = $val";
    mysql_query($result,$connection);
    header("Location: ./index.php");
    exit();
  }
}
else if ($edit == 'Edit') {
  echo "<HTML>";
  echo "<Body>";
  while (list ($key,$val) = @each ($term)) 
  {
    $result="SELECT * FROM Equipment WHERE ID = $val";
    $fetch = mysql_fetch_array(mysql_query($result,$connection));
    echo $fetch['Name'] . " - " . $fetch['Type'] . " <br />";
  
    
  }
  echo "</body>";
  echo "</html>";
}
mysql_close($connection);
?>
```

Other issue below-

I have the code from the post above returning results into a table format - each item into text fields.  I have an update button below the text field that I want to be able to update the displayed records in the mysql database with... but have NO clue where to start with that.  How can I assign a custom ID which I can call in the update script to update the records?


```
<?php
  $term = $_POST[SelectedItems];
  $delete = $_POST[delete];
  $edit = $_POST[edit];
$connection = mysql_connect("localhost", "invuser", "Password");
if (!$connection)
  {
  die('Error: ' . mysql_error());
  }

mysql_select_db("ITinventory", $connection);


if ($delete == 'Delete') {

  while (list ($key,$val) = @each ($term)) {
    $result="DELETE FROM Equipment WHERE ID = $val";
    mysql_query($result,$connection); 
  }
  header("Location: ./index.php");
  exit();
}
else if ($edit == 'Edit') {
  echo "<HTML>";
  echo "<body bgcolor=0066FF link=ffffff vlink=ffffff alink=ffffff>";
  echo "<div style='width: 100%; height: 100%;'>"; 
  echo "<font color=ffffff>";
  echo '<h1 align=center>Inventory Listing</h1>';
  echo "<center><table style='width: 100%; background-color: 000066; color:ffffff;'><tr><td width='100%'>";
  echo "<b>Edit Items:</b>";
  echo "<div style='width: 100%; height: 450px; overflow: scroll; background-color: 0066FF;'>";
  echo "<table border=1 style='color:ffffff; border-collapse: collapse;' border='0px' width='100%'>";
  echo "<form action='UpdateItems.php' method='post' border='1px'>"; 
  echo '<tr>';
  echo '<td width=2%>';
  echo "<b><u>ID</u></b>";
  echo '</td>';
  echo '<td width=11%>';
  echo "<b><u>Item Name</u></b>";
  echo '</td>';
  echo '<td width=7%>';
  echo "<b><u>Item Type</u></b>";
  echo '</td>';
  echo '<td width=10%>';
  echo "<b><u>Model Number</u></b>";
  echo '</td>';
  echo '<td width=9%>';
  echo "<b><u>Serial Number</u></b>";
  echo '</td>';
  echo '<td width=9%>';
  echo "<b><u>Asset Number</u></b>";
  echo '</td>';
  echo '<td width=10%>';
  echo "<b><u>Location</u></b>";
  echo '</td>';
  echo '<td width=47%>';
  echo "<b><u>Notes</u></b>";
  echo '</td>';
  while (list ($key,$val) = @each ($term)) 
  {
    $result="SELECT * FROM Equipment WHERE ID = $val";
    $fetch = mysql_fetch_array(mysql_query($result,$connection));
    $ID =  $fetch['ID'];
    $Name = $fetch['Name'];
    $Type = $fetch['Type'];
    $Model = $fetch['Model'];
    $SerialNumber = $fetch['SerialNumber'];
    $ITAssetTag = $fetch['ITAssetTag'];
    $Location = $fetch['Location'];
    $Notes = $fetch['Notes'];
    echo '<tr>';
    echo '<td width=2%>';
    echo "<input type='text' size='2' value='$ID' name='ID.$val' />";
    echo '</td>';
    echo '<td width=11%>';
    echo "<input type='text' size='17' value='$Name' name='Name.$val' />";
    echo '</td>';
    echo '<td width=7%>';
    echo "<input type='text' size='9' value='$Type' name='Type.$val' />";
    echo '</td>';  
    echo '<td width=10%>';
    echo "<input type='text' size='14' value='$Model' name='Model.$val' />";
    echo '</td>';  
    echo '<td width=9%>';
    echo "<input type='text' size='13' value='$SerialNumber' name='SerialNumber.$val' />";
    echo '</td>';  
    echo '<td width=10%>';
    echo "<input type='text' size='12' value='$ITAssetTag' name='ITAssetTag.$val' />";
    echo '</td>';  
    echo '<td width=10%>';
    echo "<input type='text' size='14' value='$Location' name='Location.$val' />";
    echo '</td>';  
    echo '<td width=47%>';
    echo "<input type='text' size='75' value='$Notes' name='Notes.$val' />";
    echo '</td>';
    echo "</tr>";   
  }
  echo "</table>";
  echo "</div>";
  echo "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td width=25% align=right>";
  echo "<a href='./index.php'><font color='ffffff'><b>Cancel</b></font></a><font color=000066>_____</font><input type='submit' name='UpdateItems' value='Update' />";
  echo "</td>";
  echo "</tr>";
  echo "</table>";
  echo "</div>";
  echo "</form>";
  echo "</body>";
  echo "</html>";
}
mysql_close($connection);
?>
```

Any ideas? How would I do a "for each strign that starts with 'ID_'" loop?

This is my current POST data output from print_r($_POST):

Array ( [ID_77] => 77 [Name_77] => WCDH10079TEST [Type_77] => Computer [Model_77] => ModelA [SerialNumber_77] => SN-XXXX [ITAssetTag_77] => IT-PC-01 [Location_77] => Quality [Notes_77] => This isnt a real computer [ID_80] => 80 [Name_80] => WCDH10079TEST2 [Type_80] => Computer [Model_80] => ModelB [SerialNumber_80] => SN-XXXX [ITAssetTag_80] => IT-PC-02 [Location_80] => Case Mgmt [Notes_80] => This isnt a real pc either [UpdateItems] => Update ) 

When I have 2 records selected through the page posted above.  Do I need to restructure the way the page above sends post data or is there a way to break this out into updating multiple individual records? Or have each line of textboxes send seperate arrays of post data with the same input button?


----------

