Update/add record to database table Problems, PLEASE HELP

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

please use php tag…
[php][/php]

If you had used the [php] tags, you would have seen the highlighting, and that there’s a LOT of missing quotes and other errors in your PHP script. Also use

[php]error_reporting(E_ALL);[/php]

To pick up on syntax, parse and other errors.
After a LOT of refactoring and problem solving, this is what I came up with.

[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 $myVars = array("id", "firstname", "lastname", "dob", "status", "address", "city", "state", "zip", "work", "email", "web1", "web2", "message"); for ($i = 0; $i < count($myVars); $i++) { ${$myVars[$i]} = mysql_real_escape_string($_POST[$myVars[$i]]); } // if $id is not defined, we have a new entry, otherwise update the old entry if (is_numeric($id)) { $query = "UPDATE Classmates SET"; for ($i = 0; $i < count($myVars); $i++) { if ($i > 0) { $query .= ","; } $query .= " ".$myVars[$i]." = '".${$myVars[$i]}."'"; } $query .= " WHERE id = ".$id; } else { $query = "INSERT INTO Classmates ("; for ($i = 0; $i < count($myVars); $i++) { if ($i > 0) { query .= ","; } $query .= " ".$myVars[$i]; } $query .= ") VALUES ("; for ($i = 0; $i < count($myVars); $i++) { if ($i > 0) { query .= ","; } $query .= " '".$myVars[$i]."'"; } $query .= ")"; } // 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]

Thanks for the help but one thing. Everyone keeps telling me to "use PHP tags"but the tutorials I’ve read have said that <?php and ?> are correct php tags. What am I missing?

I found this after a search for acceptable PHP tags.

Opening Tag Closing Tag

<? ?> <?php ?>

<% %>

Yes, those are appropriate tags to instantiate a PHP script (and I would recommend everyone to consistenly use <?php and ?> as it’s unambiguous).

However, what we were talking about was [php] and [/php] tags, in order to display PHP code with highlighting in your posts. Compare:

PHP without [php] tags:

<?php if (true) { echo "bladibla"; } ?>

PHP with [code] tags:

<?php
if (true) {
  echo "bladibla";
}
?>

PHP with [php] tags:
[php]

<?php if (true) { echo "bladibla"; } ?>

[/php]

Also, read this announcement, which explains them quite nicely.

Sponsor our Newsletter | Privacy Policy | Terms of Service