Jump to content

Recommended Posts

Hey,

I'm trying to update a row in a table through PHP but it just doesn't update anything, and doesn't return an error.

Here is the code including the HTML with the form:

 

// get value of id that sent from address bar
$id=$_GET['id'];


// Retrieve data from database
$sql="SELECT * FROM $table_name WHERE id='$id'";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);
?>
<html>
<head>
	<title>Menu Admin</title>
	<style>* {font-family: Arial; font-size: 12px;}</style>
</head>

<body>
	<center>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="6"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Cat ID</strong></td>
<td align="center"><strong>Menu ID</strong></td>
<td align="center"><strong>Cat title</strong></td>
<td align="center"><strong>File name</strong></td>
<td align="center"><strong>Class name</strong></td>
</tr>
<tr>
<td> </td>
<td align="center"><input name="cat_id" type="text" id="cat_id" value="<? echo $rows['cat_id']; ?>"></td>
<td align="center"><input name="menu_id" type="text" id="menu_id" value="<? echo $rows['menu_id']; ?>" size="15"></td>
<td><input name="cat_title" type="text" id="cat_title" value="<? echo $rows['cat_title']; ?>" size="15"></td>
<td><input name="file_name" type="text" id="file_name" value="<? echo $rows['file_name']; ?>" size="15"></td>
<td><input name="class_name" type="text" id="class_name" value="<? echo $rows['class_name']; ?>" size="15"></td>
</tr>
<tr>
<td> </td>
<td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?
mysql_close();
?>

 

And this is the "update_ac.php":

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
mysql_query("SET NAMES utf8;");

// update data in mysql database
$sql="UPDATE $table_name SET cat_id='$cat_id', menu_id='$menu_id', cat_title='$cat_title', file_name='$file_name', class_name='$class_name' WHERE id='$id'";
$result=mysql_query($sql);

// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}

else {
echo "ERROR";
}

 

Anyone knows what's the problem?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/131826-update-doesnt-work-mysql/
Share on other sites

Yeah you never grab the input field from the first page with the $_POST method you use.  You can't use the variables. 

 

Try adding this to the top of update_rc.php:

 

$cat_id = $_POST['cat_id'];
$menu_id = $_POST['menu_id'];
$cat_title = $_POST['cat_title'];
$file_name = $_POST['file_name'];
$class_name = $_POST['class_name'];
$id = $_POST['id'];

Yeah you never grab the input field from the first page with the $_POST method you use.  You can't use the variables. 

 

Try adding this to the top of update_rc.php:

 

$cat_id = $_POST['cat_id'];
$menu_id = $_POST['menu_id'];
$cat_title = $_POST['cat_title'];
$file_name = $_POST['file_name'];
$class_name = $_POST['class_name'];
$id = $_POST['id'];

 

Yes! Works!!

Thank you very much :)

Maq's suggestion is a good one, as it will better your code. But, many servers have a setting that creates variables based on the keys and values of $_REQUEST ($_POST or $_GET) variables. It sounds like you were used to a server with that setting turned on, but as you have learned, you should never assume that this will work.

Maq's suggestion is a good one, as it will better your code. But, many servers have a setting that creates variables based on the keys and values of $_REQUEST ($_POST or $_GET) variables. It sounds like you were used to a server with that setting turned on, but as you have learned, you should never assume that this will work.

 

Yeah.. guess my web hosting server has this setting turned on.

 

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.