UnitedForums - UK Web Hosting Forum UnitedHosting Community Hosting Forums
Network and Server StatusCustomer SupportUK Web Hosting
UnitedHostingUnitedHosting Sitemap UK Hosting ForumUK Web HostingWeb Hosting ForumsUK Reseller HostingWeb Host CommunityUK Managed Dedicated ServersHosting Help and SupportUK Domain Name Registration

Go Back   UnitedForums.co.uk > UnitedHosting Community > Website Development & Scripting

Reply
 
Thread Tools Rate Thread Display Modes
Old 2nd March 2010, 02:33 PM   #1 (permalink)
galahad
Registered User
 
Join Date: Apr 2005
Posts: 459
Editing db records which were inputted as an array

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 Code:
<?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 ($rowmysql_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:

PHP Code:
//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 $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>&nbsp;</p></form>";
?> 
The mySQL error is:

PHP Code:
UPDATE mothistorytable SET carid ''motDate ''motOdometer '' WHERE carid ''(,"1.1.10","23457"//this is from my $echo $q

WarningCannot modify header information headers already sent by (output started at /adminarea/motdetails_updatedb.php:29in /adminarea/motdetails_updatedb.php on line 33 
The code page that handles the inputting is:

PHP Code:
<?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($q0, -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?
galahad is offline   Reply With Quote
Old 2nd March 2010, 04:31 PM   #2 (permalink)
galahad
Registered User
 
Join Date: Apr 2005
Posts: 459
Okay, I removed the echo's (I was using them to see if the query outputted ok), however now what's happening is it appears to run ok when I enter some values into the MOT form table and submit (i.e it goes through to the next page as specified - addnewphotos.php), but I check mySQL and they haven't entered into the db... ???
galahad is offline   Reply With Quote
Old 3rd March 2010, 12:38 AM   #3 (permalink)
m_c
Registered User
 
Join Date: Jul 2009
Posts: 21
The SQL useage is wrong from what I can see.

What I can't quite work out, is are you using one database entry to store all the details for each vehicle, or one line per mot (ie multiple lines per vehicle)?
m_c is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


All times are GMT. The time now is 06:37 PM.

UK Web Hosting  |  UK Reseller Hosting  |  UK Dedicated Servers UnitedHosting  |  UnitedSupport  |  SEO by vBSEO 3.0.0
Copyright © 1998-2009 United Communications Limited. All Rights Reserved. Registered in England and Wales 3651923 - VAT Reg No. 737662309