a while back I did a php script that was suppose to take a record id that was taken from a webform and update it if the record was already in the Mysql table or add it if it wasn’t. I always updates just fine but I’ve never gotten it to add a record. At the time it didn’t matter becuase I was going to be the only one using it.
I’m now making a new page and this has to work now. Been pulling out my hair. Any ideas?
[php]
<?php
// saving script
// connect to the server
mysql_connect( '208.***.***.***, '*****', '*****' )
or die( "Error! Could not connect to database: " . mysql_error() );
// select the database
mysql_select_db( '******' )
or die( "Error! Could not select the database: " . mysql_error() );
// get the variables from the URL request string
$id = $_REQUEST['id'];
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$dob = $_REQUEST['dob'];
$status = $_REQUEST['status'];
$address = $_REQUEST['address'];
$city = $_REQUEST['city'];
$state = $_REQUEST['state'];
$zip = $_REQUEST['zip'];
$work = $_REQUEST['work'];
$email = $_REQUEST['email'];
$web1 = $_REQUEST['web1'];
$web2 = $_REQUEST['web2'];
$message = $_REQUEST['message'];
// if $id is not defined, we have a new entry, otherwise update the old entry
if( $id )
{
$query = "UPDATE `Classmates` SET `id`='$id', `firstname`='$firstname', `lastname`='$lastname', `dob`='$dob',
`status`='$status', `address`='$address', `city`='$city', `state`='$state', `zip`='$zip', `work`='$work',
`email`='$email', `web1`='$web1', `web2`='$web2',`message`='$message' WHERE `id`='$id'";
}
else
{
$query = "INSERT INTO `Classmates` ( `id,`firstname`,`lastname`,`dob`,`status`,`address`,`city`,`state`,`zip`,`work`,`email`,`web1`,`web2`,`message` )
VALUES ( '$id','$firstname','$lastname','$dob, '$status','$address','$city','$state','$zip','$work','$email','$web1','$web2','$message' )";
}
// save the info to the database
$results = mysql_query( $query );
// print out the results
if( $results )
{
echo( "Successfully saved the entry." );
}
else
{
die( "Trouble saving information to the database: " . mysql_error() );
}
?>
[/php]
Mod Edit - Added php tags for readability