I have a site page that adds (via a table in a form) values for car MOT history, for a specific car ID. For example ID 600 can have various values for MOT Date and for odometer (mileometer) readings, so it’s a simple table with dates and values for each date.
The “Add New Details” form works fine, with an array, but I’ve hit some problems trying to pull that data out of mySQL so that it can be edited (and new additional values added, which will be more frequent).
The code for adding the new details was:
<?php
include ('../inc/dbconnect.php');
$caridquery = "SELECT carid FROM cars ORDER BY carid DESC LIMIT 1";
$numresults=mysql_query($caridquery);
$numrows=mysql_num_rows($numresults);
// get results
$result = mysql_query($caridquery) or die("Couldn't execute query");
// display the results returned
while ($row= mysql_fetch_array($result)) {
$caridvalue = $row["carid"];
$count++ ;
}
?>
<input type="hidden" name="addnewmot" value="1">
<table width="600" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="300" colspan="2"><?php echo 'Add MOT Details for car with ID <textarea name="_CarID" rows="1" cols="5" readonly> ' . $caridvalue . '</textarea>' ?></td>
</tr>
<tr>
<td width="150">MOT Date</td>
<td width="150">Odometer</td>
</tr>
<? // loop to create table
$output = "";
$desired_row_count = 20; // how many available entries do we want?
for ($i=0; $i<$desired_row_count; $i++) {
$output .= '
<tr>
<td width="150"><input type="text" name="mot_date['.$i.']" size="20" /></td>
<td width="150"><input type="text" name="mot_odometer['.$i.']" size="20" /></td>
</tr>';
}
echo $output;
?>
<tr>
<td width="150"><input type="submit" value="Add MOT Details" /></td>
</tr>
</table>
</form>
So it builds a table and the various values inputted get entered into rows in mySQL each with the same Car ID.
The problem is I need to output those values as not only editable but with extra table rows within the sending form, so that the admins can not only edit the details pulled from the db, but also fill in new rows (for example when the car gets a new MOT)
I tried this as a page which outputs the existing values from the db into the form table (which it does successfully) but it brings up an error on submit:
//Query for the MOT table
include ('../inc/dbconnect.php');
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
echo "<form action=\\"motdetails_updatedb.php\\" method=\\"post\\" name=\\"updatemot\\">
<table width=\\"450\\" border=\\"1\\" bordercolor=\\"#000\\" cellspacing=\\"0\\" cellpadding=\\"0\\" class=\\"cardetailsauto\\"><tr><td colspan=\\"2\\" align=\\"center\\"><strong>MOT History</strong></td></tr><tr><td width=\\"150\\"><strong>MOT Date</strong></td><td width=\\"150\\"><strong>Odometer</strong></td></tr>";
$count = 1 + $s ;
// Build SQL Query
$query2 = 'SELECT * FROM mothistorytable WHERE carid='.$refnumber;
echo $query2;
// specify the table and field names for the SQL query
$result2 = mysql_query($query2) or die("The System is undergoing maintenance at the moment and will be available shortly");
while ($row = mysql_fetch_assoc($result2))
{
$carid = $row["carid"];
$title48 = $row["motdate"];
$title49 = $row["motodometer"];
echo "<tr><td width=\\"150\\"><input type=\\"hidden\\" name=\\"_CarID\\" value='$carid' /><input name=\\"_MOTDate\\" value='$title48' /></td><td width=\\"150\\"><input name=\\"_OdometerDate\\" value='$title49' /></td></tr>";
$count++;
}
// loop to create table
$output = "";
$desired_row_count = 20; // how many available entries do you want?
for ($i=0; $i<$desired_row_count; $i++) {
$output .= '
<tr>
<td width="150"><input type="text" name="mot_date['.$i.']" size="20" /></td>
<td width="150"><input type="text" name="mot_odometer['.$i.']" size="20" /></td>
</tr>';
}
echo $output;
echo "</table><input type=\\"submit\\" value=\\"Submit MOT Changes >>>\\" name=\\"updatemot\\" /><p> </p></form>";
?>
The mySQL error is:
UPDATE mothistorytable SET carid = '', motDate = '', motOdometer = '' WHERE carid = ''(,"1.1.10","23457") //this is from my $echo $q
Warning: Cannot modify header information - headers already sent by (output started at /adminarea/motdetails_updatedb.php:29) in /adminarea/motdetails_updatedb.php on line 33
The code page that handles the inputting is:
<?php
if(isset($_POST['updatemot'])) //If the form was sent
{
//mysql_connect(DB_SERVER, DB_USER, DB_PWD) or die(mysql_error());
//mysql_select_db(DB_NAME) or die(mysql_error());
include ('../inc/dbconnect.php');
$carid = $_POST['_CarID'];
$motDate=$_POST['mot_date'];
$motOdometer=$_POST['mot_odometer'];
//echo $carid;
//echo $motOdometer;
//$q = 'INSERT INTO mothistorytable (`carid`,`motdate`,`motodometer`) VALUES ';
$q = "UPDATE mothistorytable SET carid = '$carid', motDate = '$mot_date', motOdometer = '$mot_odometer' WHERE carid = '$carid'";
// loop through motDate array and add to INSERT statement
for ($i=0; $i<count($motDate); $i++) {
if ($motDate[$i]!="" && $motOdometer[$i]!="") {
$q .= '('.$carid.',"'.$motDate[$i].'","'.$motOdometer[$i].'"),';
}
}
// remove ending comma from $q
$q = substr($q, 0, -1);
echo $q;
mysql_query($q);
//go to the next page
header('location: addnewphotos.php');
exit;
}
?>
It’s a real headache as we need the admins to be able to pull out and amend or add to the MOT details db.
Any ideas as an altrenative for how we can go about this?