Ok so whilst I have been working on my PHP and MySQL skills I am new to inserting data into multiple tables in one go. I have done some reading around the subject and I appreciate the basics and the importance of normalised data etc and as such the need to put the information into various tables.
I have pieced the code below together from some of my previous work as well as the tutorial offered at http://www.desilva.biz/mysql/insertid.html . The issue I currently face is that the tutorial I learnt the code from for inputting into various tables was not based around an array of data and whilst I have got it to almost work I cannot use my fuelrecords_id because where I have to call it in my current code it has not yet been defined. Therefore to make my code work at present I just have to use a comma for the column.
Finally I would like to perfect a way to make the if statements work correctly with the array data so if a 0 or blank is submitted as part of the array a new row is not inserted with just 0's in my database tables for that respective row of data
<?php
$wedrf=trim($_POST['FR_WE']);
list($d, $m, $y) = explode('/', $wedrf);
$mk=mktime(0, 0, 0, $m, $d, $y);
$wed_refor=strftime('%Y-%m-%d',$mk);
$con = mysql_connect("ip","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("jbsrint", $con);
//New Code
$row_data = array();
foreach($_POST['VEH_LIST_REG'] as $row=>$VEH_LIST_REG) {
$WEDATE=$wed_refor;
$VEH_LIST_REG=mysql_real_escape_string($VEH_LIST_REG);
$FR_DIE_L=mysql_real_escape_string($_POST['FR_DIE_L'][$row]);
$FR_DIE_C=mysql_real_escape_string($_POST['FR_DIE_C'][$row]);
$FR_PET_L=mysql_real_escape_string($_POST['FR_PET_L'][$row]);
$FR_PET_C=mysql_real_escape_string($_POST['FR_PET_C'][$row]);
$FR_OIL_L=mysql_real_escape_string($_POST['FR_OIL_L'][$row]);
$FR_OIL_C=mysql_real_escape_string($_POST['FR_OIL_C'][$row]);
$row_data[] = "(',', '$VEH_LIST_REG', '$WEDATE')";
$row_data1[] = "(',','$FR_DIE_L', '$FR_DIE_C', ',')";
$row_data2[] = "(',', '$FR_PET_L', '$FR_PET_C', ',')";
$row_data3[] = "(',', '$FR_OIL_L', '$FR_OIL_C', '$FR_OIL_C')";
}
if (!empty($row_data)) {
$query = 'INSERT INTO fuelrecords(FR_ID, VEH_LIST_REG, FR_WE) VALUES '.implode(',', $row_data);
#$result = mysql_query( $query);
# get fuelrecord id
$fuelrecords_ID = mysql_insert_id();
# if the user submitted diesel information
if( isset($FR_DIE_L) )
{
# and insert the diesel details
$sql = "INSERT INTO fuelrecords_die(FRD_ID,FR_DIE_L,FR_DIE_C,fuelrecords_ID) VALUES ".implode(',', $row_data1);
$result = mysql_query( $sql);
}
# if the user submitted petrol information
if( isset($FR_PET_L) )
{
# and insert the diesel details
$sql = "INSERT INTO fuelrecords_pet(FRP_ID,FR_PET_L,FR_PET_C,fuelrecords_ID) VALUES ".implode(',', $row_data2);
$result = mysql_query( $sql);
}
# if the user submitted oil information
if( isset($FR_OIL_L) )
{
# and insert the oil details
$sql = "INSERT INTO fuelrecords_oil(FRO_ID,FR_OIL_L,FR_OIL_C,fuelrecords_ID) VALUES ".implode(',', $row_data3);
$result = mysql_query( $sql);
}
if (mysql_query($query))
echo '<font color=\"FFFFFF\" size=\"3px\">Successful inserted</font>';
else
echo '<font color=\"FFFFFF\" size=\"3px\">Insert failed</font>';
}
?>
<?php
mysql_close($con)
?>
Tables are as follows:
fuelrecords
FR_ID (Auto increment)
VEH_LIST_REG
FR_WE
fuelrecords_die
FRD_ID (AUTO INCREMENT)
FR_DIE_L
FR_DIE_C
fuelrecords_ID (foreign ID from fuelrecords)
fuelrecords_pet
FRP_ID (AUTO INCREMENT)
FR_PET_L
FR_PET_C
fuelrecords_ID (foreign ID from fuelrecords)
fuelrecords_oil
FRO_ID (AUTO INCREMENT)
FR_OIL_L
FR_OIL_C
fuelrecords_ID (foreign ID from fuelrecords)
Basically the purpose is to log vehicle fuel usage and cost. As there wont always be data for petrol, diesel and oil hence the separate tables so only needed dat is logged. Hope this clarifies
A always all help and assistance is much appreciated.