Jump to content

Please help with edit page


Amanda-Lee

Recommended Posts

Hi can anyone please help me, I have to create a edit page that retrieves information from a list so that a user can change the text. The page looks fine but when I enter data in the fields and update it leaves the page as it was retrieved.

I am a beginner! My code looks like this:

 

<form method = "get" action = "edit.php">
<?php

  //check to see if user is logged on
  session_start();
  if (!(isset($_SESSION['login']) && $_SESSION['login'] != "")) {
  	header ("Location:login.php");
  }

  include('connect.php');  //connection details to database in a connect.php page
  
  $product_id = $_GET['product_id'];
  
  $query = "SELECT * FROM products WHERE product_id = '$product_id'";
  $result = mysql_query($query);
  
  while($row = mysql_fetch_array($result)){
  $product_id = $row['product_id'];
  echo "<table>";
  echo "<tr>";
  echo "<td><input name = 'product_id' type = 'hidden' value ='$row[product_id]'></td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Product Name:</td><td><input name = 'pname' type = 'text' value ='$row[product_name]'></td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Product Range:</td><td><input name = 'prange' type = 'text' value ='$row[product_range]'></td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Product Price:</td><td><input name = 'pprice' type = 'text' value ='$row[product_price]'></td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td><input type = 'submit' name = 'Submit' value = 'Update'></td>";
  echo "</tr>";
  echo "</table>";
  }
  
   //if form was submitted
    if ($_SERVER['REQUEST_METHOD'] == 'POST'){
  
    //get values from fields
    $productname = $_POST['pname'];
    $range = $_POST['prange'];
    $price1 = $_POST['pprice'];
    $price = (int)$price1;
    
    if ($productname == "" || $range == "" || $price == "" ) {
        $errorMessage .= "Please fill in all text boxes";
        }
        else {
        $errorMessage = "";
        }
    
  
    
    $query = "UPDATE products SET product_name = '$productname', product_range = '$range', product_price = '$price' WHERE product_id = '$product_id'";
    $result = mysql_query($query);
    print "<br> $productname from range $range with a price of $price has been updated!";
    } 
      
?>

  

 

Thank you in advance!

Link to comment
Share on other sites

Try to keep processing above display so updates will be reflected with page reloads.  Avoid having anything sent to the browser if using a header() tag a before starting and updating sessions.  Your for had action as GET put processing was POST.  Missing </form> tag.  See how this one works.

<?php
//check to see if user is logged on
session_start();
if (!(isset($_SESSION['login']) && $_SESSION['login'] != "")) {
header ("Location:login.php");
}

include('connect.php');  //connection details to database in a connect.php page 

//if form was submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST'){

//get values from fields
$product_id = $_POST['product_id'];
$productname = $_POST['pname'];
$range = $_POST['prange'];
$price1 = $_POST['pprice'];
$price = (int)$price1;

if (empty($productname) || empty($range) || empty($price)) {
   $errorMessage .= "Please fill in all text boxes";
   }
   else {
   $errorMessage = "";

	$query = "UPDATE products SET product_name = '$productname', product_range = '$range', product_price = '$price' WHERE product_id = '$product_id'";
	$result = mysql_query($query);
	print "<br> $productname from range $range with a price of $price has been updated!";
	} 
}		
	  
$product_id = $_GET['product_id'];

$query = "SELECT * FROM products WHERE product_id = '$product_id'";
$result = mysql_query($query);
echo "<form method = \"post\" action = \"edit.php\">";  
while($row = mysql_fetch_array($result)){

echo "<table>";
echo "<tr>";
echo "<td><input name = 'product_id' type = 'hidden' value ='{$row['product_id']}'></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Product Name:</td><td><input name = 'pname' type = 'text' value ='{$row['product_name']}'></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Product Range:</td><td><input name = 'prange' type = 'text' value ='{$row['product_range']}'></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Product Price:</td><td><input name = 'pprice' type = 'text' value ='{$row['product_price']}'></td>";
echo "</tr>";
echo "<tr>";
echo "<td><input type = 'submit' name = 'Submit' value = 'Update'></td>";
echo "</tr>";
echo "</table>";
}
echo "</form>";  


?>

Link to comment
Share on other sites

Drummin, thank you for your help.

 

I tried your code but it gives me the following error:

Notice: Undefined index: product_id in C:\Program Files (x86)\EasyPHP-5.3.3\www\edit.php on line 47

 

Line 47 is:


$product_id = $_GET['product_id'];

:shrug:

 

 

Link to comment
Share on other sites

Well really, that whole section were you query DB table and show for should be wrapped in an IF statement as it relies on having the GET value.

 

<?php
//check to see if user is logged on
session_start();
if (!(isset($_SESSION['login']) && $_SESSION['login'] != "")) {
header ("Location:login.php");
}

include('connect.php');  //connection details to database in a connect.php page 

//if form was submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST'){

//get values from fields
$product_id = $_POST['product_id'];
$productname = $_POST['pname'];
$range = $_POST['prange'];
$price1 = $_POST['pprice'];
$price = (int)$price1;

if (empty($productname) || empty($range) || empty($price)) {
   $errorMessage .= "Please fill in all text boxes";
   }
   else {
   $errorMessage = "";

	$query = "UPDATE products SET product_name = '$productname', product_range = '$range', product_price = '$price' WHERE product_id = '$product_id'";
	$result = mysql_query($query);
	print "<br> $productname from range $range with a price of $price has been updated!";
	} 
}

if (isset($_GET['product_id']) || isset($_POST['product_id'])){	
if (isset($_GET['product_id'])){		  
	$product_id = $_GET['product_id'];
}
if (isset($_POST['product_id'])){		  
	$product_id = $_POST['product_id'];
}

$query = "SELECT * FROM products WHERE product_id = '$product_id'";
$result = mysql_query($query);
echo "<form method = \"post\" action = \"edit.php\">";  
while($row = mysql_fetch_array($result)){

echo "<table>";
	echo "<tr>";
	echo "<td><input name = 'product_id' type = 'hidden' value ='{$row['product_id']}'></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>Product Name:</td><td><input name = 'pname' type = 'text' value ='{$row['product_name']}'></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>Product Range:</td><td><input name = 'prange' type = 'text' value ='{$row['product_range']}'></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>Product Price:</td><td><input name = 'pprice' type = 'text' value ='{$row['product_price']}'></td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td><input type = 'submit' name = 'Submit' value = 'Update'></td>";
	echo "</tr>";
echo "</table>";
}
echo "</form>";  
}//if (isset($_GET['product_id']))

?>

Link to comment
Share on other sites

Please see update, so variable $product_id is available regardless of GET or POST.

if (isset($_GET['product_id']) || isset($_POST['product_id'])){	
if (isset($_GET['product_id'])){		  
	$product_id = $_GET['product_id'];
}
if (isset($_POST['product_id'])){		  
	$product_id = $_POST['product_id'];
}

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.