Help Updating MySQL database tables

Hello I have created a MySQL database and tables with vehicle information and maintenance information. I have added an update link in the table output, so maintenance information can be updated. I click the link and it takes me to the maintenance html form I created. when I hit submit I have it sending to the update script. It works fine iv I am inserting a record but is not working to update the record. I am getting this error…

[b]Maintenance Info Creation Failed.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘( date_next_maint, issues_reported, date_reported, scheduled_service_date, s’ at line 1[/b]

Here is the code I have for the form page and the update page

Form Page:
[php]<?php require_once("dbcon.php"); ?>

<?php require_once("functions.php"); ?> <?php // get value of id that sent from address bar $id=$_GET['id']; // Retrieve data from database $sql="SELECT * FROM $tbl_name WHERE id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); ?>

Maintenace Report

Date of next scheduled maintenance:
Maintenace/Repair Issues Reported:
Date That Maintenace/Repair Issues Reported:
Scheduled Service Date That Maintenace/Repair Issues Reported:
Maintenace/Repair Service Performed W/Comments:
Date Maintenace/Repair Service Performed:
Date OF Follow-up Work / If Needed:
Service In Progress: No   Yes
Date Return To Use:
Time Return Use:
Nonrepaiable Issues Found W/Comments:
Date Nonrepaiable Issues Found:
Comments:
 
Cancel

 

[/php]

Update Page:

[php]<?php require_once("dbcon.php"); ?>

<?php require_once("functions.php"); ?> <?php $date_next_maint = mysql_prep($_POST['date_next_maint']); $issues_reported = mysql_prep($_POST['issues_reported']); $date_reported = mysql_prep($_POST['date_reported']); $scheduled_service_date = mysql_prep($_POST['scheduled_service_date']); $service_performed = mysql_prep($_POST['service_performed']); $date_service_performed = mysql_prep($_POST['date_service_performed']); $date_of_followup = mysql_prep($_POST['date_of_followup']); $service_in_progress = mysql_prep($_POST['service_in_progress']); $date_return_use = mysql_prep($_POST['date_return_use']); $time_return_use = mysql_prep($_POST['time_return_use']); $issues_nonrepairable = mysql_prep($_POST['issues_nonrepairable']); $date_nonrepairable_issues = mysql_prep($_POST['date_nonrepairable_issues']); $comments = mysql_prep($_POST['comments']); ?> <?php $query = "UPDATE maint( date_next_maint, issues_reported, date_reported, scheduled_service_date, service_performed, date_service_performed, date_of_followup, service_in_progress, date_return_use, time_return_use, issues_nonrepairable, date_nonrepairable_issues, comments ) VALUES ( '{$date_next_maint}', '{$issues_reported}', '{$date_reported}', '{$scheduled_service_date}', '{$service_performed}', '{$date_service_performed}', '{$date_of_followup}', '{$service_in_progress}', '{$date_return_use}', '{$time_return_use}', '{$issues_nonrepairable}', '{$date_nonrepairable_issues}', '{$comments}' )"; $result = mysql_query($query, $connection); if ($result) { // Success! redirect_to("maint_list_update.php"); } else { // Display error message. echo "

Maintenance Info Creation Failed.

"; echo "

" . mysql_error() . "

"; } ?>[/php]

Please help and a great thank you in advance.

I found some Syntax error in your query.

fixed query:

$query = "UPDATE maint SET `date_next_maint`='$date_next_maint', `issues_reported`='$issues_reported', `date_reported`='$date_reported',
	`scheduled_service_date`='$scheduled_service_date', `service_performed`='$service_performed', `date_service_performed`='$date_service_performed', 
	`date_of_followup`='$date_of_followup',`service_in_progress`='$service_in_progress', `date_return_use`='$date_return_use', `time_return_use`='$time_return_use', 
	`issues_nonrepairable`='$issues_nonrepairable', `date_nonrepairable_issues`='$date_nonrepairable_issues', `comments` ='$comments'";

i inserted the code you fixed my errors on, (Thank you very much for that!!!) but it updated all the records not just the record I selected update on. Here is the code for my table output page where I have the option to update a record. Could the problem be here?

Update Record Page -
[php]

Service Order Database <?php require_once("dbcon.php"); ?> <?php require_once("functions.php"); ?> <?php $query="SELECT * FROM service_info ORDER BY id"; $result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo “Database Output

”;
// process form when posted
if(isset($_POST[‘value’])) {
if($_POST[‘value’] == ‘Engine’) {
// query to get all Engine records
$query = “SELECT * FROM servifce_info WHERE issues_reported=‘Engine’”;
}elseif($_POST[‘value’] == ‘Transmission’) {
// query to get all Transmission records
$query = “SELECT * FROM servifce_info WHERE issues_reported=‘Transmission’”;
}elseif($_POST[‘value’] == ‘Differential’) {
// query to get all Differential records
$query = “SELECT * FROM servifce_info WHERE issues_reported=‘Differential’”;
}elseif($_POST[‘value’] == ‘Electrical’) {
// query to get all Electrical records
$query = “SELECT * FROM servifce_info WHERE issues_reported=‘Electrical’”;
}elseif($_POST[‘value’] == ‘Tires’) {
// query to get all Tires records
$query = “SELECT * FROM servifce_info WHERE issues_reported=‘Tires’”;
}elseif($_POST[‘value’] == ‘Brakes’) {
// query to get all Brakes records
$query = “SELECT * FROM servifce_info WHERE issues_reported=‘Brakes’”;
}elseif($_POST[‘value’] == ‘HVAC’) {
// query to get all HVAC records
$query = “SELECT * FROM servifce_info WHERE issues_reported=‘HVAC’”;
}elseif($_POST[‘value’] == ‘Lighting’) {
// query to get all Lighting records
$query = “SELECT * FROM servifce_info WHERE issues_reported=‘Lighting’”;
}elseif($_POST[‘value’] == ‘Accident Damage’) {
// query to get all Accident Damage records
$query = “SELECT * FROM servifce_info WHERE issues_reported=‘Accident Damage’”;
} else {
// query to get all records
$query = "SELECT * FROM servifce_info ";
}
mysql_close();
}
?>

' method='post' name='form_filter' > All Engine Transmission Differential Electrical Tires Brakes HVAC Lighting Accident
<?php $i=0; while ($i < $num) { $field1=mysql_result($result,$i,"id"); $field2=mysql_result($result,$i,"name"); $field3=mysql_result($result,$i,"number"); $field4=mysql_result($result,$i,"location"); $field5=mysql_result($result,$i,"status"); $field6=mysql_result($result,$i,"rts"); $field7=mysql_result($result,$i,"odomread"); $field8=mysql_result($result,$i,"odomdate"); $field9=mysql_result($result,$i,"date_next_maint"); $field10=mysql_result($result,$i,"issues_reported"); $field11=mysql_result($result,$i,"date_reported"); $field12=mysql_result($result,$i,"scheduled_service_date"); $field13=mysql_result($result,$i,"service_performed"); $field14=mysql_result($result,$i,"date_service_performed"); $field15=mysql_result($result,$i,"date_of_followup"); $field16=mysql_result($result,$i,"service_in_progress"); $field17=mysql_result($result,$i,"date_return_use"); $field18=mysql_result($result,$i,"time_return_use"); $field19=mysql_result($result,$i,"issues_nonrepairable"); $field20=mysql_result($result,$i,"date_nonrepairable_issues"); $field21=mysql_result($result,$i,"parts_cost"); $field22=mysql_result($result,$i,"hours"); $field23=mysql_result($result,$i,"comments"); ?> <?php $i++; } ?> [/php]
ID Vehicle Name Vehicle Number Location Status RTS Odometer Date Odometer Reading Date Date of next scheduled maintenance Maintenace/Repair Issues Reported Date Issues Reported Scheduled Service Date Issues Reported Repair Service Performed W/Comments Date Service Performed Date OF Follow-up Service In Progress 0=NO 1=YES Date Return To Use Time Return Use Nonrepaiable Issues Found W/Comments Date Issues Found Parts Cost Hours Worked Comments Update
<?php echo $field1; ?> <?php echo $field2; ?> <?php echo $field3; ?> <?php echo $field4; ?> <?php echo $field5; ?> <?php echo $field6; ?> <?php echo $field7; ?> <?php echo $field8; ?> <?php echo $field9; ?> <?php echo $field10; ?> <?php echo $field11; ?> <?php echo $field12; ?> <?php echo $field13; ?> <?php echo $field14; ?> <?php echo $field15; ?> <?php echo $field16; ?> <?php echo $field17; ?> <?php echo $field18; ?> <?php echo $field19; ?> <?php echo $field20; ?> <?php echo $field21; ?> <?php echo $field22; ?> <?php echo $field23; ?> update

use the WHERE clause in your SQL query.

eg:

"UPDATE table_name SET fieldname='value14' blah blah blah WHERE ID='$id'"

that is a just an example which update only where the ID is equal

Ok I had everything working yesterday, but when I updated a few fields and then when back to update a few others, it deleted the previous updated fields and set them to blank, so not I am trying INSERT INTO service_info WHERE id=$id and this is the error I get and these are the updated pages involved.

Maintenance Info Creation Failed.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘date_next_maint=‘testing’, date_reported=‘testing’, `scheduled_service_d’ at line 1

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘date_next_maint=‘testing’, date_reported=‘testing’, scheduled_service_d' at line 1 SQL: INSERT INTO service_infodate_next_maint='testing',date_reported='testing',scheduled_service_date='testing',service_performed='',date_service_performed='',date_of_followup='',service_in_progress='1',date_return_use='',time_return_use='',issues_nonrepairable='',date_nonrepairable_issues='',comments` =’’ WHERE id=

When I go to maint_update page, the url shows id=‘whatever record clicked’ so it is getting the id from the records page.

Here is records page service_list_update

[PHP]

Service Order Database <?php require_once("dbcon.php"); ?> <?php require_once("functions.php"); ?> <?php $query="SELECT * FROM service_info ORDER BY id"; $result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo “Database Output

”;
// process form when posted
if(isset($_POST[‘value’])) {
if($_POST[‘value’] == ‘Engine’) {
// query to get all Engine records
$query = “SELECT * FROM servifce_info WHERE issues_reported=‘Engine’”;
}elseif($_POST[‘value’] == ‘Transmission’) {
// query to get all Transmission records
$query = “SELECT * FROM servifce_info WHERE issues_reported=‘Transmission’”;
}elseif($_POST[‘value’] == ‘Differential’) {
// query to get all Differential records
$query = “SELECT * FROM servifce_info WHERE issues_reported=‘Differential’”;
}elseif($_POST[‘value’] == ‘Electrical’) {
// query to get all Electrical records
$query = “SELECT * FROM servifce_info WHERE issues_reported=‘Electrical’”;
}elseif($_POST[‘value’] == ‘Tires’) {
// query to get all Tires records
$query = “SELECT * FROM servifce_info WHERE issues_reported=‘Tires’”;
}elseif($_POST[‘value’] == ‘Brakes’) {
// query to get all Brakes records
$query = “SELECT * FROM servifce_info WHERE issues_reported=‘Brakes’”;
}elseif($_POST[‘value’] == ‘HVAC’) {
// query to get all HVAC records
$query = “SELECT * FROM servifce_info WHERE issues_reported=‘HVAC’”;
}elseif($_POST[‘value’] == ‘Lighting’) {
// query to get all Lighting records
$query = “SELECT * FROM servifce_info WHERE issues_reported=‘Lighting’”;
}elseif($_POST[‘value’] == ‘Accident Damage’) {
// query to get all Accident Damage records
$query = “SELECT * FROM servifce_info WHERE issues_reported=‘Accident Damage’”;
} else {
// query to get all records
$query = "SELECT * FROM servifce_info ";
}
mysql_close();
}
?>

' method='post' name='form_filter' > All Engine Transmission Differential Electrical Tires Brakes HVAC Lighting Accident
<?php while($row = mysql_fetch_array($result)) { ?>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['id']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['name']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['number']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['location']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['status']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['rts']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['odomread']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['odomdate']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['date_next_maint']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['issues_reported']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['date_reported']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['scheduled_service_date']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['service_performed']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['date_service_performed']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['date_of_followup']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['service_in_progress']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['date_return_use']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['time_return_use']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['issues_nonrepairable']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['date_nonrepairable_issues']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['parts_cost']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['hours']; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row['comments']; ?></font></td>
<td><a href="maint_update.php?id=<?php echo $row['id']; ?>">update</a></td>
</tr>
<?php

}
?>

<?php error_reporting(E_ALL); ?> [/PHP]

Here is update page maint_update

[PHP]<?php require_once("dbcon.php"); ?>

<?php require_once("functions.php"); ?> <?php // get value of id that sent from address bar $id=$_GET['id']; $_SESSION['id'] = $id; // Retrieve data from database $sql="SELECT * FROM service_info WHERE id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); ?>

Maintenace Report

ID Vehicle Name Vehicle Number Location Status RTS Odometer Date Odometer Reading Date Date of next scheduled maintenance Maintenance/Repair Issues Reported Date Issues Reported Scheduled Service Date Issues Reported Repair Service Performed W/Comments Date Service Performed Date OF Follow-up Service In Progress 0=NO 1=YES Date Return To Use Time Return Use Nonrepaiable Issues Found W/Comments Date Issues Found Parts Cost Hours Worked Comments Update
Date of next scheduled maintenance:
Date That Maintenace/Repair Issues Reported:
Scheduled Service Date That Maintenace/Repair Issues Reported:
Maintenace/Repair Service Performed W/Comments:
Date Maintenace/Repair Service Performed:
Date OF Follow-up Work / If Needed:
Service In Progress: No   Yes
Date Return To Use:
Time Return Use:
Nonrepaiable Issues Found W/Comments:
Date Nonrepaiable Issues Found:
Comments:
 
Cancel

 

[/PHP]

Here is update file updated_maint

[PHP]<?php session_start();?>

<?php require_once("dbcon.php"); ?> <?php require_once("functions.php"); ?> <?php $id = $_SESSION['id']; $date_next_maint = mysql_prep($_POST['date_next_maint']); $date_reported = mysql_prep($_POST['date_reported']); $scheduled_service_date = mysql_prep($_POST['scheduled_service_date']); $service_performed = mysql_prep($_POST['service_performed']); $date_service_performed = mysql_prep($_POST['date_service_performed']); $date_of_followup = mysql_prep($_POST['date_of_followup']); $service_in_progress = mysql_prep($_POST['service_in_progress']); $date_return_use = mysql_prep($_POST['date_return_use']); $time_return_use = mysql_prep($_POST['time_return_use']); $issues_nonrepairable = mysql_prep($_POST['issues_nonrepairable']); $date_nonrepairable_issues = mysql_prep($_POST['date_nonrepairable_issues']); $comments = mysql_prep($_POST['comments']); ?> <?php $query = "INSERT INTO service_info `date_next_maint`='$date_next_maint', `date_reported`='$date_reported', `scheduled_service_date`='$scheduled_service_date', `service_performed`='$service_performed', `date_service_performed`='$date_service_performed', `date_of_followup`='$date_of_followup',`service_in_progress`='$service_in_progress', `date_return_use`='$date_return_use', `time_return_use`='$time_return_use', `issues_nonrepairable`='$issues_nonrepairable', `date_nonrepairable_issues`='$date_nonrepairable_issues', `comments` ='$comments' WHERE id=$id"; $result = mysql_query($query, $connection); if ($result) { // Success! redirect_to("service_list_update.php"); } else { // Display error message. echo "

Maintenance Info Creation Failed.

"; echo "

" . mysql_error() . "

"; } echo "

Error: " . mysql_error() . " SQL: $query

"; ?> <?php ini_set('display_errors',1); error_reporting(E_ALL); ?>[/PHP]
$query = "INSERT INTO service_info SET `date_next_maint`='$date_next_maint', `date_reported`='$date_reported',
    `scheduled_service_date`='$scheduled_service_date', `service_performed`='$service_performed', `date_service_performed`='$date_service_performed', 
    `date_of_followup`='$date_of_followup',`service_in_progress`='$service_in_progress', `date_return_use`='$date_return_use', `time_return_use`='$time_return_use', 
    `issues_nonrepairable`='$issues_nonrepairable', `date_nonrepairable_issues`='$date_nonrepairable_issues', `comments` ='$comments'
	WHERE `id`='$id'";
Sponsor our Newsletter | Privacy Policy | Terms of Service