Jump to content

edit records in mysql


zeusthegreat

Recommended Posts

have too pages

 

an inventory_list

 

 

<?php

session_start();

if(!isset($_SESSION["manager"])){

header("location: admin_login.php");

exit();

}

// Be sure to check that this manager SESSION value is in fact in the database

 

$managerID = preg_replace('#[^0-9]#i','', $_SESSION["id"]); //filter everyhting but numbers and letters

$manager = preg_replace('#[^A-Za-z0-9]#i','', $_SESSION["manager"]); //filter everything but numbers and letters

$password = preg_replace('#[^A-Za-z0-9]#i','', $_SESSION["password"]); //filter everything but numbers and letters

//Run mySQL query to be sure that this person is an admin and that their password session varequals the database information

//Connect to the MySQL database

include "../storescripts/connect_to_mysql.php";

$sql= mysql_query("SELECT * FROM admin WHERE id= '$managerID' AND username= '$manager' AND password= '$password' LIMIT 1"); //query the person --make sure person exists in database----

$existCount= mysql_num_rows($sql); //count the row nums

if($existCount == 0){ //evaluate the count

header("location: ../index.php");

exit();

}

?>

<?php

//error reporting

error_reporting(E_ALL);

ini_set('display_errors', '1');

?>

<?php

// Delete Item Question to admin, and Delete product

if (isset($_GET['deleteid'])){

echo 'Do you really want to delete produst with ID of'.$_GET['deleteid'] .'? <a href="inventory_list.php?yesdelete=' .$_GET['deleteid'] .'">Yes</a>/<a href="inventory_list.php">No</a>';

exit();

}

if (isset($_GET['yesdelete'])){

//remove item from system and delete its picture if they choose

//delete from database

$id_to_delete=$_GET['yesdelete'];

$sql=mysql_query("DELETE FROM products WHERE id='$id_to_delete' LIMIT 1")or die (mysql_error());

//unlink the image from server

// Remove The Pic------------------------------------

$pictodelete=("../inventory_images/$id_to_delete.jpg");

if (file_exists($pictodelete)){

unlink($pictodelete);

}

header("location:inventory_list.php");

exit();

}

 

?>

<?php

//parse the form data and add inventory item to the system

if (isset($_POST['product_name'])) {

 

$product_name = mysql_real_escape_string($_POST['product_name']);

$price = mysql_real_escape_string($_POST['price']);

$category = mysql_real_escape_string($_POST['category']);

$subcategory = mysql_real_escape_string($_POST['subcategory']);

$details = mysql_real_escape_string($_POST['details']);

//see if that product name is an identical match to another product in the system

$sql = mysql_query("SELECT id FROM products WHERE product_name='$product_name' LIMIT 1");

$productMatch = mysql_num_rows ($sql); //count the output ammount

if ($productMatch > 0){

echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="inventory_list.php">click here</a>';

exit();

//Add this product into the database now

}

 

$sql = mysql_query("INSERT INTO products(product_name, price, details, category, subcategory, date_added) VALUES('$product_name','$price','$details','$category','$subcategory',now())") or die (mysql_error()); //now adds todays date

$pid = mysql_insert_id();

//place image in the folder

$newname = "$pid.jpg";

move_uploaded_file( $_FILES['fileField']['tmp_name'], "../inventory_images/$newname");

header("location: admin_login_list.php");

exit();

 

 

}

?>

<?php

//This block grabs the whole list for viewing

$product_list = "";

$sql = mysql_query("SELECT * FROM products");

$productCount = mysql_num_rows($sql);//count the the output ammount

if ($productCount > 0){

while($row = mysql_fetch_array($sql)){

$id = $row["id"];

$price = $row["price"];

$product_name = $row["product_name"];

$date_added = strftime("%b %d, %Y", strtotime($row["date_Added"])); // Notice here how we changed it from _added to _Added

$product_list .= "$id - $product_name -  £$price -  $date_added    <a href='inventory_edit.phppid=$id'>edit</a>••<a href='inventory_list.php?deleteid=$id'>  delete</a><br/>";

}

 

}else{

$product_list = "You have no products listed in your store Yet";

 

}

?>

 

 

 

 

 

 

 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'>http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">'>http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Inventory list</title>

<link rel="stylesheet" href="../style/style.css" type="text/css" media="screen" />

<style type="text/css">

body {

background-color: #FFF;

}

</style>

</head>

 

<body>

<div align="center"></div>

<div align="center" id="mainWrapper">

 

 

 

<?php include_once("../template_header.php"); ?>

 

<?php include_once("../template_sidebar.php"); ?>

 

 

 

 

 

<div align="left" id="pageContent">

<div align="right"style="margin-right:32px"><a href="inventory_list.php#inventoryForm">+Add New Inventory Item</a></div>

<div align="left" style="margin-left:24px;">

<h2>Inventory List</h2>

<?php echo $product_list; ?>

</div>

<hr />

 

<div>

<a name="inventoryForm" id="inventoryForm"></a>

<h3 align="center">

↓ Add New Inventory Item Form ↓

</h3>

</div>

<form action="inventory_list.php" enctype="multipart/form-data" name="myForm" id="myForm" method="post">

<table width="90%" border="0" cellspacing="0" cellpadding="6">

<tr>

<td width="20%" align="right">Product Name</td>

<td width="80%"><label>

<input name="product_name" type="text" id="product_name" size="64" />

</label></td>

</tr>

<tr>

<td align="right">Product Price</td>

<td><label>

£

<input name="price" type="text" id="price" size="12" />

</label></td>

</tr>

<tr>

<td align="right">Category</td>

<td><label>

<select name="category" id="category">

<option value="Clothing">Clothing</option>

</select>

</label></td>

</tr>

<tr>

<td align="right">Subcategory</td>

<td><select name="subcategory" id="subcategory">

<option value=""></option>

<option value="Hats">Hats</option>

<option value="Pants">Pants</option>

<option value="Shirts">Shirts</option>

</select></td>

</tr>

<tr>

<td align="right">Product Details</td>

<td><label>

<textarea name="details" id="details" cols="64" rows="5"></textarea>

</label></td>

</tr>

<tr>

<td align="right">Product Image</td>

<td> </td>

</tr>

<tr>

<td> </td>

<td><label>

<input type="submit" name="button" id="button" value="Add This Item Now" />

<input type="file" name="fileField" id="fileField" />

</label></td>

</tr>

</table>

</form>

 

 

 

<br/>

<br/>

</div>

<?php include_once("../template_footer.php"); ?>

 

 

</div>

</body>

</html>

 

 

i use this page too add records and to delete them

 

then this page

 

 

inventory_edit.php

 

 

<?php

session_start();

if(!isset($_SESSION["manager"])){

header("location: admin_login.php");

exit();

}

// Be sure to check that this manager SESSION value is in fact in the database

 

$managerID = preg_replace('#[^0-9]#i','', $_SESSION["id"]); //filter everyhting but numbers and letters

$manager = preg_replace('#[^A-Za-z0-9]#i','', $_SESSION["manager"]); //filter everything but numbers and letters

$password = preg_replace('#[^A-Za-z0-9]#i','', $_SESSION["password"]); //filter everything but numbers and letters

//Run mySQL query to be sure that this person is an admin and that their password session varequals the database information

//Connect to the MySQL database

include "../storescripts/connect_to_mysql.php";

$sql= mysql_query("SELECT * FROM admin WHERE id= '$managerID' AND username= '$manager' AND password= '$password' LIMIT 1"); //query the person --make sure person exists in database----

$existCount= mysql_num_rows($sql); //count the row nums

if($existCount == 0){ //evaluate the count

header("location: ../index.php");

exit();

}

?>

<?php

//error reporting

error_reporting(E_ALL);

ini_set('display_errors', '1');

?>

 

<?php

//parse the form data and add inventory item to the system

if (isset($_POST['product_name'])) {

 

$pid = mysql_real_escape_string($_POST['thisID']);

$product_name = mysql_real_escape_string($_POST['product_name']);

$price = mysql_real_escape_string($_POST['price']);

$category = mysql_real_escape_string($_POST['category']);

$subcategory = mysql_real_escape_string($_POST['subcategory']);

$details = mysql_real_escape_string($_POST['details']);

//see if that product name is an identical match to another product in the system

$sql = mysql_query("UPDATE products SET product_name='$product_name', price='$price', details='$details', category='$category', subcategory='$subcategory' WHERE id='$pid'");

 

 

//Add this product into the database now

 

if ($_FILES['fileField']['tmp_name'] != "") {

 

 

//place image in the folder

$newname = "$pid.jpg";

move_uploaded_file($_FILES['fileField']['tmp_name'], "../inventory_images/$newname");

 

 

}

header("location: inventory_list.php");

exit();

}

?>

 

 

<?php

// Gather this products full information for in serting automatically into the edit form below on page

 

if (isset($_GET['pid'])){

//This block grabs the whole list for viewing

$targetID= $_GET['pid'];

 

$sql = mysql_query("SELECT * FROM products WHERE id='$targetID' LIMIT 1");

$productCount = mysql_num_rows($sql);//count the the output ammount

if ($productCount > 0){

while($row = mysql_fetch_array($sql)){

$product_name = $row["product_name"];

$price = $row["price"];

$category = $row["category"];

$subcategory = $row["subcategory"];

$details = $row["details"];

$date_added = strftime("%b %d, %Y", strtotime($row["date_Added"])); // Notice here how we changed it from _added to _Added

 

 

}

 

}else{

echo "Does not exist!";

exit();

}

}

 

?>

 

 

 

 

 

 

 

<!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>Inventory list</title>

<link rel="stylesheet" href="../style/style.css" type="text/css" media="screen" />

<style type="text/css">

body {

background-color: #FFF;

}

</style>

</head>

 

<body>

<div align="center"></div>

<div align="center" id="mainWrapper">

 

 

 

<?php include_once("../template_header.php"); ?>

 

<?php include_once("../template_sidebar.php"); ?>

 

 

 

 

 

<div align="left" id="pageContent">

<div align="right"style="margin-right:32px"><a href="inventory_list.php#inventoryForm">+Add New Inventory Item</a></div>

<div align="left" style="margin-left:24px;">

<h2>Inventory List</h2>

<?php echo $product_list; ?>

</div>

<hr />

 

<div>

<a name="inventoryForm" id="inventoryForm"></a>

<h3 align="center">

↓ Add New Inventory Item Form ↓

</h3>

</div>

<form action="inventory_edit.php" enctype="multipart/form-data" name="myForm" id="myForm" method="post">

<table width="90%" border="0" cellspacing="0" cellpadding="6">

<tr>

<td width="20%" align="right">Product Name</td>

<td width="80%"><label>

<input name="product_name" type="text" id="product_name" size="64" value="<?php echo $product_name; ?>" />

</label></td>

</tr>

<tr>

<td align="right">Product Price</td>

<td><label>

£

<input name="price" type="text" id="price" size="12" value="<?php echo $price; ?>" />

</label></td>

</tr>

<tr>

<td align="right">Category</td>

<td><label>

<select name="category" id="category">

<option value="Clothing">Clothing</option>

</select>

</label></td>

</tr>

<tr>

<td align="right">Subcategory</td>

<td><select name="subcategory" id="subcategory">

<option value="<?php echo $subcategory; ?>"><?php echo $subcategory; ?></option>

<option value="Pants">Pants</option>

<option value="Shirts">Shirts</option>

</select></td>

</tr>

<tr>

<td align="right">Product Details</td>

<td><label>

<textarea name="details" id="details" cols="64" rows="5"><?php echo $details; ?></textarea>

</label></td>

</tr>

<tr>

<td align="right">Product Image</td>

<td> </td>

</tr>

<tr>

<td> </td>

<td><label>

<input name="thisID" type="hidden" value="<?php echo $targetID; ?>" />

<input type="submit" name="button" id="button" value="Make Changes" />

<input type="file" name="fileField" id="fileField" />

</label></td>

</tr>

</table>

</form>

 

 

 

<br/>

<br/>

</div>

<?php include_once("../template_footer.php"); ?>

 

 

</div>

</body>

</html>

 

 

now i use dreamweaver cs5 and it says no syntax errors, when i type the exact link into page it come up with two errors

 

firstly

 

Notice: Undefined variable: product_list in C:\wamp\www\myNewweb\storeadmin\inventory_edit.php on line 125

 

but i defined it quite clearly.

 

so on the inventory_edit page i deleted the line

 

 

<?php echo $product_list; ?>

 

 

and it worked! have i done this right, because i do not want it to hit me later in this database.

Edited by talos
Link to comment
Share on other sites

As with another persons post, i have a hard time reading through this to know exactly what everythings doing, however it seems as though in inventory_edit.php you have not defined $product_list anywhere.

 

in inventory_list.php you have, but inventory_edit is seperate and cannot access that variable's contents, thus making undefined. (unless im missing an include() somewhere in inventory_edit where you've brought this info over from inventory_list??)

 

 

I suggest making this whole block of code, it's own public function that you can use to retrieve your list:

 

<?php
function getProductList() {
//This block grabs the whole list for viewing
$product_list = "";
$sql = mysql_query("SELECT * FROM products");
$productCount = mysql_num_rows($sql);//count the the output ammount
if ($productCount > 0){
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$price = $row["price"];
$product_name = $row["product_name"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_Added"])); // Notice here how we changed it from _added to _Added
$product_list .= "$id - $product_name -  £$price -  $date_added    <a href='inventory_edit.phppid=$id'>edit</a>••<a href='inventory_list.php?deleteid=$id'>  delete</a><br/>";
}

}else{
$product_list = "You have no products listed in your store Yet";

}

return $product_list;
}
?>

 

 

then in inventory_edit call the function:

 

<h2>Inventory List</h2>
<?php echo getProductList(); ?>
</div>

 

and call the function the same way in inventory_list as well to clean things up.

 

 

Summed up, it appears inventory list has the variable declared, but inventory_edit does not. Hope this helps.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...