Jump to content

Recommended Posts

<?php session_start(); 

require("../db/db.php"); 
require("../db/config.php"); 
require("../db/util.php");
require("../db/Settings.php");

isloggedin();
accessneeded("A");

$id = $_GET['id'];

$sql = "SELECT name, link FROM `menu` WHERE id = '$id' LIMIT 1";
$query = mysql_query($sql) or die(mysql_error());
while ($menu = mysql_fetch_assoc($query)) {
$name1 = $menu['name'];
$link1 = $menu['link'];
}
if(isset($_POST['submit'])) {
  $name = strip_tags(mysql_real_escape_string($_POST['name']));
  $link = mysql_real_escape_string($_POST['link']);
  $sqltwo = "UPDATE menu SET name = '$name', link = '$link' WHERE id = '$id'";
  $querytwo = mysql_query($sqltwo) or die(mysql_error());
  header("Location: ../Index.php");
  exit;
}
?>

<table>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<P ALIGN=CENTER>
<tr><td>Name: <br><INPUT type="text" name="name" id="$name" value="<?php print htmlspecialchars($name1); ?>"></td></tr>
<tr><td>Link: <br><INPUT type="text" name="link" id="$link" value="<?php print htmlspecialchars($link1); ?>"></td></tr>
<tr><td><input type="submit" name="submit" value="Submit"></P></td></tr>
</form>
</table>

 

does everything except update the mysql it reffers me to index.php as if it has but it doesnt update and there are no errors.

Link to comment
https://forums.phpfreaks.com/topic/151179-solved-mysql-wont-update-please-help/
Share on other sites

Cause you need to add an extra hidden field for when you submit.  You're losing the $id...

 

Name: 

Link: 

      //ADDED***

 

 

When you submit, now you have to retrieve the id via POST like so:

 

  $id = mysql_real_escape_string($_POST['id']);  //ADDED*
  $name = strip_tags(mysql_real_escape_string($_POST['name']));
  $link = mysql_real_escape_string($_POST['link']);
  $sqltwo = "UPDATE menu SET name = '$name', link = '$link' WHERE id = '$id'";

 

 

 

Cause you need to add an extra hidden field for when you submit.  You're losing the $id...

 

<tr><td>Name: <br><INPUT type="text" name="name" id="$name" value="<?php print htmlspecialchars($name1); ?>"></td></tr>
<tr><td>Link: <br><INPUT type="text" name="link" id="$link" value="<?php print htmlspecialchars($link1); ?>"></td></tr>
<input type="hidden" name="id" value="<?php echo $id; ?>">      //ADDED***
<tr><td><input type="submit" name="submit" value="Submit"></P></td></tr>

 

 

When you submit, now you have to retrieve the id via POST like so:

 

  $id = mysql_real_escape_string($_POST['id']);  //ADDED*
  $name = strip_tags(mysql_real_escape_string($_POST['name']));
  $link = mysql_real_escape_string($_POST['link']);
  $sqltwo = "UPDATE menu SET name = '$name', link = '$link' WHERE id = '$id'";

 

Worked!, Thanks mate.

Sure, you lost your $id because when you submitted the page it had to refresh and the $_GET variable isn't in the URL anymore so it just gets reset.  You probably could have set the action with the variable name in it, but I like the hidden fields better  ;D

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.