Hello:

I'm trying to insert my form two table in mysql. I have the following but only insert to one table (ajax table query) is successfull. Any ideas what I'm doing wrong.

<?php
//error_reporting(0);
//error_reporting(E_ERROR | E_WARNING | E_PARSE);
error_reporting(E_ALL);

$host = "localhost";
$login_name = "dsdsdt";
$password = ".........";

$link=mysql_connect("$host","$login_name","$password");		
mysql_select_db("shop", $link); 
//or die("Could not find database");
//echo mysql_errno() . ": " . mysql_error(). "\n";

// define each variable
if(isset($_POST['submit']))//this defines any variable that are not define below
  {
//ajax table
$firstname= mysql_real_escape_string($_POST['firstname']);
$lastname= mysql_real_escape_string($_POST['lastname']);
$address= mysql_real_escape_string($_POST['address']);
$city= mysql_real_escape_string($_POST['city']);
$state= mysql_real_escape_string($_POST['state']);
$zipcode= mysql_real_escape_string($_POST['zipcode']);
$add_type= mysql_real_escape_string($_POST['add_type']);
$areacode=mysql_real_escape_string($_POST['areacode']);
$phoneprefix=mysql_real_escape_string($_POST['phoneprefix']);
$phonesufix=mysql_real_escape_string($_POST['phonesufix']);
$tel_type=mysql_real_escape_string($_POST['tel_type']);
$cellareacode=$_POST['cellareacode'];
$cellprefix=mysql_real_escape_string($_POST['cellprefix']);
$cellsufix=mysql_real_escape_string($_POST['cellsufix']);
$email=mysql_real_escape_string($_POST['email']);
$commentonclient=mysql_real_escape_string($_POST['commentonclient']);
$theDate=mysql_real_escape_string($_POST['theDate']);
$servicedesc=($_POST['servicedesc']);
$servicedesc= "<br/>".'On '.date('m/d/y'.' @ '.'h:i')."<br/>".'We performed the following:<br/>'.""."\n$servicedesc<br/><br/>----End of Service Report----"."<br/>";//this lines seem to have corrected the displaying of text in service history
//$inputbox= "\n".'On '.date('m/d/y'.' @ '.'h:i')."\n".'We performed the following:'."\n"."\n$inputbox\n\n----End of Service Report----"."\n";

//second table
$lastname= mysql_real_escape_string($_POST['lastname']);
$dhtmlgoodies_subcategory= mysql_real_escape_string($_POST['dhtmlgoodies_subcategory']);
$clientID=mysql_real_escape_string($_POST['clientID']);
$registeredstate=mysql_real_escape_string($_POST['registeredstate']);
$commentonclient=mysql_real_escape_string($_POST['commentonclient']);
$theDate=mysql_real_escape_string($_POST['theDate']);
$dhtmlgoodies_category=mysql_real_escape_string($_POST['dhtmlgoodies_category']);
$caryear=mysql_real_escape_string($_POST['caryear']);
$servicearea=mysql_real_escape_string($_POST['servicearea']);
$currentmileage=mysql_real_escape_string($_POST['currentmileage']);
$servicedesc=($_POST['servicedesc']);
$servicedesc= "<br/>".'On '.date('m/d/y'.' @ '.'h:i')."<br/>".'We performed the following:<br/>'.""."\n$servicedesc<br/><br/>----End of Service Report----"."<br/>";//this lines seem to have corrected the displaying of text in service history
//$inputbox= "\n".'On '.date('m/d/y'.' @ '.'h:i')."\n".'We performed the following:'."\n"."\n$inputbox\n\n----End of Service Report----"."\n";

//ajax table query
$mysql_query=("INSERT ajax_client SET firstname='$firstname', lastname='$lastname', address='$address', city='$city', state='$state', zipcode='$zipcode', add_type='$add_type', areacode='$areacode', phoneprefix='$phoneprefix', phonesufix='$phonesufix', tel_type='$tel_type', 
cellareacode='$cellareacode', cellprefix='$cellprefix', cellsufix='$cellsufix', cell_type='$cell_type', email='$email', commentonclient='$commentonclient', servicearea='$servicearea', servicedesc ='$servicedesc'");
$result=mysql_query($mysql_query);
if(!$result){die("Error: ".mysql_error());

$id = mysql_insert_id();

//vehicle table
$mysql_query=("INSERT additional_cars SET id='$id', lastname='$lastname',  clientID='$clientID', registeredstate='$registeredstate', dhtmlgoodies_category='$dhtmlgoodies_category', dhtmlgoodies_subcategory='$dhtmlgoodies_subcategory',caryear='$caryear', servicearea='$servicearea', 
currentmileage='$currentmileage', servicedesc ='$servicedesc'");

echo("<p>Error adding data" . 
mysql_error() . "</p>"); 

mysql_close($link);
}
?>

No errors are produced

Best,
Mossa

Recommended Answers

All 8 Replies

Member Avatar for diafol

Have

$result=mysql_query($mysql_query);

after the vehicle table as well.

Have

$result=mysql_query($mysql_query);

after the vehicle table as well.

Thanks for the reply, I have added as instructed above; however, still not inserting in the second table.
any other ideas...

Dear Mossa,

You need to execute the MySql_Query after line 64, as you've done on line 58.
hope this will solve your problem...

Dear Mossa,

You need to execute the MySql_Query after line 64, as you've done on line 58.
hope this will solve your problem...

A great New Year to you ejaz111, and thanks for the post. I previously updated the query with

$result=mysql_query($mysql_query);

at exactly where you pointed out. Unfortunately, that did not resolve my issue.

Any other thoughts!
Best

Member Avatar for diafol

Are you sure all fieldname are valid. What error do you get, if any?

Are you sure all fieldname are valid. What error do you get, if any?

I've check again. I can seem to identify the issue...and no erros are produced!

Member Avatar for diafol

OK, try this:

$mysql_query=("INSERT additional_cars SET id='$id', lastname='$lastname',  clientID='$clientID', registeredstate='$registeredstate', dhtmlgoodies_category='$dhtmlgoodies_category', dhtmlgoodies_subcategory='$dhtmlgoodies_subcategory',caryear='$caryear', servicearea='$servicearea', 
currentmileage='$currentmileage', servicedesc ='$servicedesc'");

//ADD THIS:

echo "INSERT additional_cars SET id='$id', lastname='$lastname',  clientID='$clientID', registeredstate='$registeredstate', dhtmlgoodies_category='$dhtmlgoodies_category', dhtmlgoodies_subcategory='$dhtmlgoodies_subcategory',caryear='$caryear', servicearea='$servicearea', 
currentmileage='$currentmileage', servicedesc ='$servicedesc'";

Now, copy the screen output. Open up phpMySQL (or your favourite MySQL GUI) and paste it into the SQL (or Query) box. Run the query, see what happens.

Thank you very much for the follow up. I actually got the issue resolved earlier with the following:

//ajax table query
$mysql_query=("INSERT ajax_client SET  customerid='$accountid', firstname='$firstname', lastname='$lastname', address='$address', city='$city', state='$state', theDate='$theDate',
zipcode='$zipcode', add_type='$add_type', areacode='$areacode', phoneprefix='$phoneprefix', phonesufix='$phonesufix', tel_type='$tel_type', 
cellareacode='$cellareacode', cellprefix='$cellprefix', cellsufix='$cellsufix', cell_type='$cell_type', email='$email', commentonclient='$commentonclient'");

$result=mysql_query($mysql_query); 
if(!$result){die("Error: ".mysql_error()); 
} 

//vehicle table
$mysql_query=("INSERT additional_cars SET id=LAST_INSERT_ID(), firstname='$firstname', lastname='$lastname', address='$address', city='$city', state='$state', 
zipcode='$zipcode',clientID='$clientID', registeredstate='$registeredstate', dhtmlgoodies_category='$dhtmlgoodies_category', 
dhtmlgoodies_subcategory='$dhtmlgoodies_subcategory',caryear='$caryear', currentmileage='$currentmileage', servicedesc ='$servicedesc'");

$result=mysql_query($mysql_query);
if(!$result){die("Error: ".mysql_error());

adding this

id=LAST_INSERT_ID()

did the job.

Again again thanks for the help!
Best,
Mossa

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.