• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

PHP dynamic page + return results into a dynamic table

Joined
Dec 22, 2007
Messages
184 (0.03/day)
Location
Central Nebraska
System Name CORSAIR
Processor AMD Phenom 1090T x6 @3.2
Motherboard Gigabyte Ga-78LMT-S2P
Cooling High efficiency dust cooling
Memory 8GB GSkill DDR3 1333
Video Card(s) Sapphire Radeon HD3870 512MB GDDR4 PCI-e toxic
Storage Seagate SV35.3 ST3250310SV 250GB SATAII(Windows 7 Pro x64), 320GB Samsung SATAII(Storage and SuSE)
Display(s) Dell 20" LCD, Dell 17" LCD
Power Supply Antec Cool Blue 650W Modular
Software Windows 7 Professional SP1, Visual Studio 2010 Ultimate
***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.

Code:
<?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>";

?>
 
Last edited:
Joined
Dec 22, 2007
Messages
184 (0.03/day)
Location
Central Nebraska
System Name CORSAIR
Processor AMD Phenom 1090T x6 @3.2
Motherboard Gigabyte Ga-78LMT-S2P
Cooling High efficiency dust cooling
Memory 8GB GSkill DDR3 1333
Video Card(s) Sapphire Radeon HD3870 512MB GDDR4 PCI-e toxic
Storage Seagate SV35.3 ST3250310SV 250GB SATAII(Windows 7 Pro x64), 320GB Samsung SATAII(Storage and SuSE)
Display(s) Dell 20" LCD, Dell 17" LCD
Power Supply Antec Cool Blue 650W Modular
Software Windows 7 Professional SP1, Visual Studio 2010 Ultimate
Nevermind - needed to format these right:
Code:
 echo "<table width='100%'>";

More issues below though...
 
Last edited:

W1zzard

Administrator
Staff member
Joined
May 14, 2004
Messages
27,051 (3.71/day)
Processor Ryzen 7 5700X
Memory 48 GB
Video Card(s) RTX 4080
Storage 2x HDD RAID 1, 3x M.2 NVMe
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 10 64-bit
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
Code:
<?php 
$foo='bar';
?>
<html><body><?php echo $foo; ?></body></html>
 

Aquinus

Resident Wat-man
Joined
Jan 28, 2012
Messages
13,147 (2.94/day)
Location
Concord, NH, USA
System Name Apollo
Processor Intel Core i9 9880H
Motherboard Some proprietary Apple thing.
Memory 64GB DDR4-2667
Video Card(s) AMD Radeon Pro 5600M, 8GB HBM2
Storage 1TB Apple NVMe, 4TB External
Display(s) Laptop @ 3072x1920 + 2x LG 5k Ultrafine TB3 displays
Case MacBook Pro (16", 2019)
Audio Device(s) AirPods Pro, Sennheiser HD 380s w/ FIIO Alpen 2, or Logitech 2.1 Speakers
Power Supply 96w Power Adapter
Mouse Logitech MX Master 3
Keyboard Logitech G915, GL Clicky
Software MacOS 12.1
CLI PHP can be your best friend. I plopped your code in a file named "testop.php".

Code:
~$ php -l testop.php 
PHP Parse error:  syntax error, unexpected T_LNUMBER, expecting ',' or ';' in testop.php on line 17
Errors parsing testop.php
 
Joined
Dec 22, 2007
Messages
184 (0.03/day)
Location
Central Nebraska
System Name CORSAIR
Processor AMD Phenom 1090T x6 @3.2
Motherboard Gigabyte Ga-78LMT-S2P
Cooling High efficiency dust cooling
Memory 8GB GSkill DDR3 1333
Video Card(s) Sapphire Radeon HD3870 512MB GDDR4 PCI-e toxic
Storage Seagate SV35.3 ST3250310SV 250GB SATAII(Windows 7 Pro x64), 320GB Samsung SATAII(Storage and SuSE)
Display(s) Dell 20" LCD, Dell 17" LCD
Power Supply Antec Cool Blue 650W Modular
Software Windows 7 Professional SP1, Visual Studio 2010 Ultimate
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...
Code:
<?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:
Code:
<?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?

Code:
<?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?
 
Last edited by a moderator:
Top