stevenm1320 Posted September 2, 2007 Share Posted September 2, 2007 Hi, i'm having trouble updating a MySQL database using the following script. Any help would be appreciated. <? # tip: its always best to have an include file with all your # database information in one location. I'll post this file next. include 'db.php'; # grab the POST variables from the HTML form, # put them into PHP variables so we can work with them $ud_id = $_POST['ud_id']; $ud_title = $_POST['ud_title']; $ud_article = $_POST['ud_article']; $ud_blurb = $_POST['ud_blurb']; $ud_author = $_POST['ud_author']; $ud_sdate = $_POST['ud_sdate']; $ud_simage = $_POST['ud_simage']; $ud_id = stripslashes($ud_id); $ud_title = stripslashes($ud_title); $ud_article = nl2br($ud_article); $ud_blurb = nl2br($ud_blurb); $ud_author = stripslashes($ud_author); $ud_sdate = stripslashes($ud_sdate); $ud_simage = stripslashes($ud_simage); # everything checks out so far, so lets add this user! $sql = mysql_query ("UPDATE news SET title='$ud_title', article='$ud_article', blurb='$ud_blurb', author='$ud_author', sdate='$ud_sdate', simage='$ud_simage' WHERE id='$ud_id'"); mysql_close(); echo 'News Story Updated Successfully. Return to Admin Area. <a href="newsadmin.php">here</a>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/67643-solved-updating-mysql-database-using-php-script/ Share on other sites More sharing options...
php_tom Posted September 2, 2007 Share Posted September 2, 2007 Did you get any errors? Also, did you remember to mysql_connect() and mysql_select_db() somewhere above the code you posted? Quote Link to comment https://forums.phpfreaks.com/topic/67643-solved-updating-mysql-database-using-php-script/#findComment-339831 Share on other sites More sharing options...
Fadion Posted September 2, 2007 Share Posted September 2, 2007 Try adding a mysql_error() to the query to see if ure getting any errors. U should try debbuging it by yourself as right now its impossible for us. $sql = mysql_query("") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/67643-solved-updating-mysql-database-using-php-script/#findComment-339843 Share on other sites More sharing options...
stevenm1320 Posted September 2, 2007 Author Share Posted September 2, 2007 I didn't get any error messages, just News Story Updated Successfully etc. I have included my database connection code which was in the db.php file. I have no clue what i am doing wrong with this. I am not seeing any errors, even after adding the "or die" code. <? # tip: its always best to have an include file with all your # database information in one location. I'll post this file next. # database connection scripts # the next 4 lines you can modify $dbhost = 'localhost'; $dbusername = 'username'; $dbpasswd = 'password'; $database_name = 'databasename'; #under here, don't touch! $connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd") or die ("Couldn't connect to server."); $db = mysql_select_db("$database_name", $connection) or die("Couldn't select database."); # grab the POST variables from the HTML form, # put them into PHP variables so we can work with them $ud_id = $_POST['ud_id']; $ud_title = $_POST['ud_title']; $ud_article = $_POST['ud_article']; $ud_blurb = $_POST['ud_blurb']; $ud_author = $_POST['ud_author']; $ud_sdate = $_POST['ud_sdate']; $ud_simage = $_POST['ud_simage']; $ud_id = stripslashes($ud_id); $ud_title = stripslashes($ud_title); $ud_article = nl2br($ud_article); $ud_blurb = nl2br($ud_blurb); $ud_author = stripslashes($ud_author); $ud_sdate = stripslashes($ud_sdate); $ud_simage = stripslashes($ud_simage); # everything checks out so far, so lets add this user! $sql = mysql_query ("UPDATE news SET title='$ud_title', article='$ud_article', blurb='$ud_blurb', author='$ud_author', sdate='$ud_sdate', simage='$ud_simage' WHERE id='$ud_id'") or die(mysql_error()); mysql_close(); echo 'News Story Updated Successfully. Return to Admin Area. <a href="newsadmin.php">here</a>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/67643-solved-updating-mysql-database-using-php-script/#findComment-339851 Share on other sites More sharing options...
Fadion Posted September 2, 2007 Share Posted September 2, 2007 echo $sql and see if all the variables are passed. Be sure to have the right columns for mysql and table name. As long as syntax is concerned, it looks ok to me. Quote Link to comment https://forums.phpfreaks.com/topic/67643-solved-updating-mysql-database-using-php-script/#findComment-339854 Share on other sites More sharing options...
stevenm1320 Posted September 2, 2007 Author Share Posted September 2, 2007 When I echo sql, all it says is 1 Quote Link to comment https://forums.phpfreaks.com/topic/67643-solved-updating-mysql-database-using-php-script/#findComment-339861 Share on other sites More sharing options...
trq Posted September 2, 2007 Share Posted September 2, 2007 Change... $sql = mysql_query ("UPDATE news SET title='$ud_title', article='$ud_article', blurb='$ud_blurb', author='$ud_author', sdate='$ud_sdate', simage='$ud_simage' WHERE id='$ud_id'"); mysql_close(); echo 'News Story Updated Successfully. Return to Admin Area. <a href="newsadmin.php">here</a>'; to... $sql = "UPDATE news SET title='$ud_title', article='$ud_article', blurb='$ud_blurb', author='$ud_author', sdate='$ud_sdate', simage='$ud_simage' WHERE id='$ud_id'" if ($result = mysql_query($sql)) { echo 'News Story Updated Successfully. Return to Admin Area. <a href="newsadmin.php">here</a>'; } else { echo "Query failed<br />" . mysql_error() . "<br />$sql"; } What do you get? Quote Link to comment https://forums.phpfreaks.com/topic/67643-solved-updating-mysql-database-using-php-script/#findComment-339869 Share on other sites More sharing options...
stevenm1320 Posted September 2, 2007 Author Share Posted September 2, 2007 Parse error: parse error, unexpected T_IF on line 37 Quote Link to comment https://forums.phpfreaks.com/topic/67643-solved-updating-mysql-database-using-php-script/#findComment-339873 Share on other sites More sharing options...
trq Posted September 2, 2007 Share Posted September 2, 2007 Sorry, my fault. Missing the ; at the and of the $sql = "...." line. Quote Link to comment https://forums.phpfreaks.com/topic/67643-solved-updating-mysql-database-using-php-script/#findComment-339875 Share on other sites More sharing options...
stevenm1320 Posted September 2, 2007 Author Share Posted September 2, 2007 When i do that, it echoes the "News Story Updated Successfully, return to admin" line, though the record is still not updating. Quote Link to comment https://forums.phpfreaks.com/topic/67643-solved-updating-mysql-database-using-php-script/#findComment-339877 Share on other sites More sharing options...
trq Posted September 2, 2007 Share Posted September 2, 2007 How do you know the record is not being updated? All messages point to success. Quote Link to comment https://forums.phpfreaks.com/topic/67643-solved-updating-mysql-database-using-php-script/#findComment-339880 Share on other sites More sharing options...
stevenm1320 Posted September 2, 2007 Author Share Posted September 2, 2007 I've checked it in phpmyadmin and also in places on the site where it is used, using ctrl+f5 to cache-refresh so i get the latest version. Quote Link to comment https://forums.phpfreaks.com/topic/67643-solved-updating-mysql-database-using-php-script/#findComment-339882 Share on other sites More sharing options...
trq Posted September 2, 2007 Share Posted September 2, 2007 Try using this then. if ($result = mysql_query($sql)) { echo mysql_affected_rows() . " rows updated"; } else { echo "Query failed<br />" . mysql_error() . "<br />$sql"; } What do you get? Quote Link to comment https://forums.phpfreaks.com/topic/67643-solved-updating-mysql-database-using-php-script/#findComment-339886 Share on other sites More sharing options...
stevenm1320 Posted September 2, 2007 Author Share Posted September 2, 2007 It's coming back with "0 rows updated" now. Quote Link to comment https://forums.phpfreaks.com/topic/67643-solved-updating-mysql-database-using-php-script/#findComment-339890 Share on other sites More sharing options...
stevenm1320 Posted September 2, 2007 Author Share Posted September 2, 2007 I just wanted to say thanks for all the help and patience. It seems that the id wasn't being picked up when the form was submitted so I changed the POST id to GET id and changed the form to pass the variable $id to the processing script. Quote Link to comment https://forums.phpfreaks.com/topic/67643-solved-updating-mysql-database-using-php-script/#findComment-339953 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.