Jump to content

[SOLVED] Updating MySQL database using PHP script


stevenm1320

Recommended Posts

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>'; 

?> 

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>'; 

?>

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?

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. :)

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.