Forum Moderators: coopster

Message Too Old, No Replies

I am building a testimonial script but ran into a problem

Everything works, but having issue with updating to database

         

php4U

7:08 pm on Oct 24, 2007 (gmt 0)

10+ Year Member



I am writing a comment/testimonial script, and I am running into a problem when I attempt to update a record stored in mySQL.

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.

eelixduppy

9:26 pm on Oct 24, 2007 (gmt 0)



Firstly, the status variable is missing a semicolon at the end, so that's what is giving you the error:

$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.

FourDegreez

2:56 am on Oct 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't see you passing the actual id field in the form.

php4U

3:13 am on Oct 25, 2007 (gmt 0)

10+ Year Member



I have looked at this too long to miss the ; at the end of the status variable LOL.

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.

php4U

3:47 am on Oct 25, 2007 (gmt 0)

10+ Year Member



I found some code that combines all the steps into one file. :) It creates a link to click for the info you want to edit, and then it displays the information for that record. Once you update what you want it saves it back to the database. This is nice because, I no longer have to work with 2 files and passing everything to the second file. The code is less than 70 lines, so if anyone wants to see the code just let me know if you think you might need it for something. I did notice that it doesn't use the real_escape_string either so I will take your suggestion eelixduppy and check that out for security.