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
https://forums.phpfreaks.com/topic/262246-please-help-with-edit-page/
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>";  


?>

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']))

?>

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'];
}

Archived

This topic is now archived and is closed to further replies.

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