Jump to content

UPDATE doesn't work... (MySQL)


zurih

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.

 

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.