Jump to content

table array information "noob"


Mrwoodchuck

Recommended Posts

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.

 

 

 

 

 

Link to comment
Share on other sites

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]" />
   ";
}

 

 

 

 

 

Link to comment
Share on other sites

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>
   ";
}
?>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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