Amanda-Lee Posted May 8, 2012 Share Posted May 8, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/262246-please-help-with-edit-page/ Share on other sites More sharing options...
trq Posted May 8, 2012 Share Posted May 8, 2012 Once your code has done the update, you need to redirect back to this same page in order for the changes to appear to take effect. Quote Link to comment https://forums.phpfreaks.com/topic/262246-please-help-with-edit-page/#findComment-1343924 Share on other sites More sharing options...
Amanda-Lee Posted May 8, 2012 Author Share Posted May 8, 2012 Thank you Thorpe I added the redirect page, but it does the same, when I edit for example "apple" and type in "grapes" it gives me apple again no changes were made! Quote Link to comment https://forums.phpfreaks.com/topic/262246-please-help-with-edit-page/#findComment-1343926 Share on other sites More sharing options...
Drummin Posted May 8, 2012 Share Posted May 8, 2012 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/262246-please-help-with-edit-page/#findComment-1343992 Share on other sites More sharing options...
Amanda-Lee Posted May 8, 2012 Author Share Posted May 8, 2012 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']; Quote Link to comment https://forums.phpfreaks.com/topic/262246-please-help-with-edit-page/#findComment-1344003 Share on other sites More sharing options...
Drummin Posted May 8, 2012 Share Posted May 8, 2012 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'])) ?> Quote Link to comment https://forums.phpfreaks.com/topic/262246-please-help-with-edit-page/#findComment-1344005 Share on other sites More sharing options...
Amanda-Lee Posted May 8, 2012 Author Share Posted May 8, 2012 :D THANK YOU! THANK YOU, so so much, it works great!!! Quote Link to comment https://forums.phpfreaks.com/topic/262246-please-help-with-edit-page/#findComment-1344008 Share on other sites More sharing options...
Drummin Posted May 8, 2012 Share Posted May 8, 2012 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']; } Quote Link to comment https://forums.phpfreaks.com/topic/262246-please-help-with-edit-page/#findComment-1344009 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.