Mrwoodchuck Posted March 21, 2012 Share Posted March 21, 2012 Hi all, just to get this out of the way i have now been working on html / css / php / mysql coding for about 3 weeks. So i dont know too much, on to the problem... I am creating a database for asset managment in and out of our shop I have setup the db and it works with no issues. So I am currently trying to add fluff as i put it. The guys in the shop want the web page to have a table of information on what is in the shop and when they click on the asset number in the table they want it to open a new page where you can edit the database information of that item. I have no clue how to emplement this after reading diffrent ideas for about 4 hours today I am just stuck and confused. any ideas or tips would be helpfull This first block of code is my status page. <style> tr {font-size:24px;} button {width:120px} </style> <body topmargin="50px"> <?php session_start(); include("../../Connections/db_connection.php"); $table = "shop_inventory"; mysql_select_db("$db_database", $connect); unset ($db_username, $db_password); $currenttime = getdate(); $result = mysql_query("SELECT * FROM shop_inventory WHERE In_Shop='1'"); $test = mysql_fetch_array($result); echo "<table border='1' align='center' style='text-align:center' cellpadding='10' width='100%'> <tr> <th>Asset number</th> <th>Building</th> <th>Room number</th> <th>In date</th> </tr>"; $_SESSION['array_row'] = $test; while($row = mysql_fetch_array($result)) { $style = ''; //if ($currenttime - $row['Entered_Shop_Time'] == 0 || 1) $style = 'style="background-color:#00FF00"'; echo "<tr>"; echo "<td> <button onClick=../subpages/edititems.php>" . $row['Asset'] . "</button> </td>"; echo "<td>" . $row['Building'] . "</td>"; echo "<td>" . $row['Room_Number'] . "</td>"; echo "<td $style>" . date("F j, Y", strtotime($row['Entered_Shop_Time'])) . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($connect); ?> </body> This second block of code is the page where i want to dump the table info into for editing. <?php session_start(); include("../../Connections/db_connection.php"); $table = "shop_inventory"; mysql_select_db("$db_database", $connect); unset ($db_username, $db_password); //$Asset = $_POST['Asset']; $Session = $_SESSION['array_row']; //$data = mysql_query("SELECT * FROM shop_inventory WHERE Asset=$session"); //$query = mysql_query($data) or die ("Cannot select database." . mysql_error()); //$Session = mysql_fetch_array($data); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Add Items</title> </head> <body> <div style=" padding-top:5px;"> <h1><center>Add New Items</center></h1> </div> <form name="myForm" action="afteredititems.php" method="post"> Asset Number: <input type="text" name="Asset" value="<?php echo $Session['Asset']?>"/><br /><br /> Manufacture: <input type="text" name="Manufacture" value="<?php echo $Session['Manufacture']?>"/><br /><br /> Model: <input type="text" name="Model" value="<?php echo $Session['Model']?>"/><br /><br /> Serial Number: <input type="text" name="Serial_Number" value="<?php echo $Session['Serial_Number']?>"/><br /><br /> Building: <input type="text" name="Building" value="<?php echo $Session['Building']?>"/><br /><br /> Room Number: <input type="text" name="Room_Number" value="<?php echo $Session['Room_Number']?>"/><br /><br /> <input type="hidden" name="In_Shop" value="1"/> Type of Equipment: <select name="Type" value="<?php echo $Session['Type']?>"> <option value="desktop">Desktop</option> <option value="laptop">Laptop</option> <option value="ipad">Ipad</option> <option value="ipod">Ipod</option> <option value="projector">Projector</option> <option value="printer">Printer</option> </select><br /> <br /> <center><input type="submit" value="Add Item"/></center> </form> </body> </html> And lastly I have a page that redisplays the edited information. <?php //$Asset = $_POST['Asset']; //$data = mysql_query("SELECT * FROM shop_inventory WHERE Asset=$Asset"); //$query = mysql_query($data) or die ("Cannot select database." . mysql_error()); //$data2 = mysql_fetch_array($data); include("../../Connections/db_connection.php"); $table = "shop_inventory"; mysql_select_db("$db_database", $connect); unset ($db_username, $db_password); $Asset = $_POST['Asset']; $Manufacture = $_POST['Manufacture']; $Model = $_POST['Model']; $Sn = $_POST['Serial_Number']; //<---- possibly fubar check sn everywhere $Building = $_POST['Building']; $Room_Number = $_POST['Room_Number']; $Type = $_POST['Type']; $data = mysql_query("UPDATE `shop_inventory` SET Manufacture='$Manufacture', Model='$Model', Serial_Number='$Sn', Building='$Building', Room_Number='$Room_Number', Type='$Type' WHERE Asset=$Asset"); //$query = mysql_query($data) or die("could not execute query.". mysql_error()); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <!-- display changes --> Asset: <?php echo $Asset ?><br /> Manufacture: <?php echo $Manufacture ?><br /> Model: <?php echo $Model ?><br /> Serial number: <?php echo $Sn ?><br /> Building: <?php echo $Building ?><br /> Room Number: <?php echo $Room_Number ?><br /> Type: <?php echo $Type ?><br /><br /> </body> </html> I know there is a lot of poorly formated code and more then likely it is hard to read as I was going to do cleanup after I had all my features working. After doing all the digging i found out about sessions and how they are used to pass data, I thought it would be a good method but I just could not figure out how to send information from the table if the table is reciving its info from an array. any help would be appreciated thanks. Quote Link to comment https://forums.phpfreaks.com/topic/259458-table-array-information-noob/ Share on other sites More sharing options...
DataSpy Posted March 22, 2012 Share Posted March 22, 2012 If you've learned this much in three weeks I'd say you're doing really well, keep up the good work!!! I'm not a good programmer or anything but I know a little and the first thing I would say is learn use mysqli instead of mysql, you can read more about it here. http://www.php.net/manual/en/book.mysqli.php Quote Link to comment https://forums.phpfreaks.com/topic/259458-table-array-information-noob/#findComment-1330047 Share on other sites More sharing options...
DataSpy Posted March 22, 2012 Share Posted March 22, 2012 I wrote a nice long reply but I got distracted so the page timed out and I lost everything I wrote Anyways, you want to pass the data in the URL and then retrieve it on the next page using the GET method Status Page Instead of this echo "<td> <button onClick=../subpages/edititems.php>" . $row['Asset'] . "</button> </td>"; use this, I'm using a link instead of a button but you can change it to meet your needs <a href=\"edititems.php?Asset=$row[Asset]\" target=\"_blank\">Edit</a> Edit Items page //this is the data you got from status page $Asset = mysqli_real_escape_string($con, trim($_GET['Asset'])); $query = "SELECT * from shop_inventory WHERE Asset = '$Asset'"; $result = mysqli_query($con, $query) or die(mysqli_error($con)); if($result) { $row = mysqli_fetch_array($result, MYSQLI_ASSOC); echo" <form name=\"myForm\" action=\"afteredititems.php\" method=\"post\"> Asset Number: <input type=\"tex\t" name=\"Asset\" value=\"$row[Asset]" /> "; } Quote Link to comment https://forums.phpfreaks.com/topic/259458-table-array-information-noob/#findComment-1330061 Share on other sites More sharing options...
DataSpy Posted March 22, 2012 Share Posted March 22, 2012 Here's a revision of the above code for the edit items page, this code lets you know if the update was successful, this will show you how to use one page to display, edit, and then update the data in one page <?php include("../../Connections/db_connection.php"); // if update button has been pressed, this updates the db if(isset($_POST['update'])) { // assign vars $Asset = mysqli_real_escape_string($con, trim($_POST['Asset'])); $Manufacture = mysqli_real_escape_string($con, trim($_POST['Manufacture'])); $query = "UPDATE shop_inventory SET Manufacture='$Manufacture', ect... WHERE Asset = '$Asset'"; $result = mysqli_query($con, $query) or die(mysqli_error($con)); if($result) { ?> <script language = "javascript"> alert('Update Successful'); </script> <?php } else { ?> <script language = "javascript"> alert('Update Failed'); </script> <?php } } //this is the data you got from status page $Asset = mysqli_real_escape_string($con, trim($_GET['Asset'])); $query = "SELECT * from shop_inventory WHERE Asset = '$Asset'"; $result = mysqli_query($con, $query) or die(mysqli_error($con)); if($result) { $row = mysqli_fetch_array($result, MYSQLI_ASSOC); echo" <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\"> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> <title>Add Items</title> </head> <body> <form name=\"myForm\" action=\"$_SERVER[php_SELF]\" method=\"post\"> Asset Number: $row[Asset]<br /> Manufacture: <input type=\"text\" name=\"Manufacture\" value=\"$row[Manufacture]" /><br /> <input type=\"hidden\" name=\"Asset\" value=\"$row[Asset]" /> <input type=\"submit\" name=\"update\" value=\"Update\"> </form> </body> </html> "; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/259458-table-array-information-noob/#findComment-1330065 Share on other sites More sharing options...
Drummin Posted March 22, 2012 Share Posted March 22, 2012 Here's another version to play with. Not about to redo whole thing but inserts new records and updates records. <?php session_start(); include("../../Connections/db_connection.php"); $table = "shop_inventory"; mysql_select_db("$db_database", $connect); $date=date('Y-m-d'); $currenttime = getdate(); if (isset($_GET['edit']) && ctype_digit($_GET['edit'])){ $_SESSION['item_number']=$_GET['edit']; header("location: samplepage.php"); exit; } if (isset($_POST['cancel'])){ unset($_SESSION['item_number']); header("location: samplepage.php"); exit; } if (isset($_POST['additem'])){ $Manufacture = mysql_real_escape_string(trim($_POST['Manufacture'])); $Model = mysql_real_escape_string(trim($_POST['Model'])); //<---- possibly fubar check sn everywhere $Sn = mysql_real_escape_string(trim($_POST['Serial_Number'])); $Building = mysql_real_escape_string(trim($_POST['Building'])); $Room_Number = mysql_real_escape_string(trim($_POST['Room_Number'])); $Type = mysql_real_escape_string(trim($_POST['Type'])); $data ="INSERT INTO `shop_inventory` (Manufacture,Model,Serial_Number,Building,Room_Number,Type,Entered_Shop_Time) VALUES('$Manufacture', '$Model','$Sn','$Building','$Room_Number','$Type','$date')"; $query = mysql_query($data) or die("could not execute query.". mysql_error()); } if (isset($_POST['updateitem'])){ $Asset = mysql_real_escape_string($_SESSION['item_number']); $Manufacture = mysql_real_escape_string(trim($_POST['Manufacture'])); $Model = mysql_real_escape_string(trim($_POST['Model'])); //<---- possibly fubar check sn everywhere $Sn = mysql_real_escape_string(trim($_POST['Serial_Number'])); $Building = mysql_real_escape_string(trim($_POST['Building'])); $Room_Number = mysql_real_escape_string(trim($_POST['Room_Number'])); $Type = mysql_real_escape_string(trim($_POST['Type'])); $data ="UPDATE `shop_inventory` SET Manufacture='$Manufacture', Model='$Model', Serial_Number='$Sn', Building='$Building', Room_Number='$Room_Number', Type='$Type' WHERE Asset=$Asset"; $query = mysql_query($data) or die("could not execute query.". mysql_error()); unset($_SESSION['item_number']); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Add Items</title> <style type="text/css"> .large tr{ font-size:18px; } button {width:120px} </style> </head> <body> <?php if (!isset($_SESSION['item_number'])){ $sql = "SELECT * FROM shop_inventory "; $result = mysql_query($sql) or die("could not execute shop_inventory query.". mysql_error()); $numrows=mysql_num_rows($result); if ($numrows>=1){ echo "<table border='1' align='center' style='text-align:center' cellpadding='3' width='100%' class='large'> <tr> <th>Asset number</th> <th>Building</th> <th>Room number</th> <th>In date</th> </tr>"; while($row = mysql_fetch_array($result)) { $style = ''; if ($date=="{$row['Entered_Shop_Time']}"){ $style = 'style="background-color:#00FF00"'; } echo "<tr>"; echo "<td><input type=\"button\" onClick=\"location.href='samplepage.php?edit={$row['Asset']}'\" value='Edit Item {$row['Asset']}' /></td>"; echo "<td>" . $row['Building'] . "</td>"; echo "<td>" . $row['Room_Number'] . "</td>"; echo "<td $style>" . date("F j, Y", strtotime($row['Entered_Shop_Time'])) . "</td>"; echo "</tr>"; } echo "</table>"; }//if($result) ?> <div style=" padding-top:5px;"> <h1>Add New Items</h1> </div> <form name="myForm" action="samplepage.php" method="post"> Manufacture: <input type="text" name="Manufacture" /><br /><br /> Model: <input type="text" name="Model" /><br /><br /> Serial Number: <input type="text" name="Serial_Number" /><br /><br /> Building: <input type="text" name="Building" /><br /><br /> Room Number: <input type="text" name="Room_Number" /><br /><br /> <input type="hidden" name="In_Shop" value="1"/> Type of Equipment: <select name="Type"> <option value="desktop">Desktop</option> <option value="laptop">Laptop</option> <option value="ipad">Ipad</option> <option value="ipod">Ipod</option> <option value="projector">Projector</option> <option value="printer">Printer</option> </select><br /> <br /> <center><input type="submit" name="additem" value="Add Item"/></center> </form> <?php }//if (isset($_SESSION['item_number'])) else{ $sql = "SELECT * FROM shop_inventory WHERE Asset='{$_SESSION['item_number']}'"; $result = mysql_query($sql) or die("could not execute shop_inventory query.". mysql_error()); $row=mysql_fetch_assoc($result); $types=array("desktop","laptop","ipad","ipod","projector","printer"); ?> <div style=" padding-top:5px;"> <h1>Edit Item</h1> </div> <form name="myForm" action="samplepage.php" method="post"> Manufacture: <input type="text" name="Manufacture" value="<?php echo "{$row['Manufacture']}"; ?>" /><br /><br /> Model: <input type="text" name="Model" value="<?php echo "{$row['Model']}"; ?>" /><br /><br /> Serial Number: <input type="text" name="Serial_Number" value="<?php echo "{$row['Serial_Number']}"; ?>" /><br /><br /> Building: <input type="text" name="Building" value="<?php echo "{$row['Building']}"; ?>" /><br /><br /> Room Number: <input type="text" name="Room_Number" value="<?php echo "{$row['Room_Number']}"; ?>" /><br /><br /> <input type="hidden" name="In_Shop" value="1" value="" /> Type of Equipment: <select name="Type"> <?php foreach($types as $k => $v){ echo "<option value=\"$v\"" .($row['Type']=="$v" ? ' selected="selected"' : '') . ">$v</option>"; } ?> </select><br /> <br /> <center><input type="submit" name="cancel" value="Cancel"/> <input type="submit" name="updateitem" value="Update Item"/></center> </form> <?php } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/259458-table-array-information-noob/#findComment-1330067 Share on other sites More sharing options...
Mrwoodchuck Posted March 22, 2012 Author Share Posted March 22, 2012 WOW, Thank you guys this is some great stuff, I have adapted some of the code to my site and it works really well. I will definitely take a look at mysqli and see what the difference is. After this I am defiantly staying on this site for any further help on this project, and to offer any help i can to other noobs. Again thanks a lot guys. Quote Link to comment https://forums.phpfreaks.com/topic/259458-table-array-information-noob/#findComment-1330198 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.