Forum Moderators: coopster
I have a page that displays the data fields from the database (name, email, testimonial, status)into text fields like the following...
(shortened code)(pulls all record info from database and displays it in a form I can change and update)
<form method="post" action="update_ac.php" name="Form" />
<td width="3%"><? echo $rows['id'];?></td>
<td width="23%"><input name="name" type="text" id="name" size="30" value="<? echo $rows['name'];?>"></td>
<td width="26%"><input name="email" type="text" id="email" size="30" value="<? echo $rows['email'];?>"></td>
<td width="41%"><textarea name="testimonial" cols="70" rows="5" id="testimonial"><? echo $rows['testimonial'];?></textarea></td>
<td width="6%"><SELECT NAME="status" size="1" id="status">
<option selected></option>
<option value="1">1</option>
<option value="2">2</option>
</select></td>
<td width="6%"><input class="buttons" name="submit_pages" type="submit" value="Save" /></td>
</tr>
</table>
</form>
===========================================
update_ac.php
<?php
include("dbinfo.inc.php");
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$id=$_POST['id'];
$name=$_POST['name'];
$email=$_POST['email'];
$testimonial=$_POST['testimonial'];
//$status=$_POST['status']
$sql="UPDATE testimonial SET name='$name', email='$email', testimonial='$testimonial', status='$status' WHERE id='$id'";
$result=mysql_query($sql);
// if successfully updated
if($result){
echo "Updated Successfully";
echo "<BR>";
echo "<a href='update.php'>Update another record</a>";
}
else {
echo "ERROR";
}
// close connection
mysql_close();
?>
I kept getting an unexpected T_VARIABLE error in update_ac.php so I commented out the status var and it shows that it worked, but no info is updated. I have tried many different things and looked at this awhile, and wanted to see if anyone could spot something wrong. I tried to shorten the code as much as possible, and made the vars bold in hopes that they would help to spot a possible issue.
To explain what I am trying to do
---------------------------------
The main point of this is that when the comment is originally submitted that form passes a hidden value of 1 into the database. The page that actually displays those comments to viewers has to have a value of 2. I am trying to use this update page to change the value of $status to 2 and edit a comment(correct spelling etc.) The final output page will display all "approved" comments. I hope that makes since. Thank you for any help.
$status=$_POST['status'];
Also, if nothing is going on, what is mysql giving back to you? Try the following to see if you are getting any errors:
$result=mysql_query($sql) or die([url=http://www.php.net/mysql-error]mysql_error[/url]());
Lastly, you should be using mysql_real_escape_string [php.net] to escape your POST variables so that your script isn't exploited. Read up on the documentation to see what I'm talking about. Make sure, however, that you don't have magic quotes enabled.
I added the or die(mysql_error()); after mySQL query and didn't get any error from my database. The page that displays the info to edit seems to work ok, but now when I click save which sends me to update_ac.php I get the message that it was "Updated Successfully" but nothing updates in the database.
I tried some different code to test deleting <a href="delete_ac.php?id=<? echo $rows['id'];?>"> which is given for each row in the database, and this works to delete that record. delete_ac.php just runs a DELETE query which I thought might work the same way to UPDATE but it doesn't. Again, I appreciate the help because I have learned quite a bit here.