![]() |
|
|
#1 |
![]() Join Date: Dec 2007
Location: Central Nebraska
Posts: 170 (0.09/day)
Thanks: 85
Thanked 7 Times in 6 Posts
|
PHP dynamic page + return results into a dynamic table
***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 by shuggans; Jan 24, 2013 at 08:18 PM. |
|
|
|
|
|
#2 |
![]() Join Date: Dec 2007
Location: Central Nebraska
Posts: 170 (0.09/day)
Thanks: 85
Thanked 7 Times in 6 Posts
|
Nevermind - needed to format these right:
Code:
echo "<table width='100%'>"; Last edited by shuggans; Jan 24, 2013 at 05:18 PM. |
|
|
|
|
|
#3 |
|
Benevolent Dictator
Join Date: May 2004
Location: Stuttgart, Germany
Posts: 13,770 (4.18/day)
Thanks: 184
Thanked 10,219 Times in 3,162 Posts
|
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> |
|
|
|
|
|
#4 |
![]() Join Date: Jan 2012
Location: Dover, New Hampshire, USA
Posts: 4,258 (8.85/day)
Thanks: 1,274
Thanked 1,326 Times in 984 Posts
|
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
__________________
MyHeat |
|
|
|
|
|
#5 |
![]() Join Date: Dec 2007
Location: Central Nebraska
Posts: 170 (0.09/day)
Thanks: 85
Thanked 7 Times in 6 Posts
|
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);
?>
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);
?>
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);
?>
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 Kreij; Jan 26, 2013 at 03:56 AM. |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| dynamic disk | bigscale | General Software | 6 | Aug 4, 2010 10:03 AM |
| Using PHP to load multiple elements into a single page | new_rez | Programming & Webmastering | 15 | May 21, 2008 01:59 AM |
| pppoe and dynamic ip | bassmasta | General Hardware | 2 | Oct 1, 2007 11:48 PM |
| Dynamic Disk | watts289 | General Hardware | 7 | Mar 6, 2007 12:28 PM |
| Dynamic Disks vs Basic Disks/Basic Disks vs Dynamic Disks (differences etc.) | Alec§taar | General Software | 0 | Nov 28, 2006 01:55 PM |