0

I am working on one php script and would like to insert data to three different tables. How can I do that on php action script.

error_reporting(0);
$datee=$_POST['date'];
$company=$_POST['company'];
$PAddress = $_POST['PAddress'];
$recruiter=$_POST['recruiter'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];

$company=$_POST['company'];
$agents=$_POST['agents'];
$resumes = $_POST['resumes'];
$structure=$_POST['structure'];

$sql =  "INSERT INTO job_spec_contact (contact_info_key, datee,company_name,Physical_Address, recruitment_person,email,Telephone)
                         VALUES('null','$datee','$company','$PAddress','$recruiter','$email','$telephone')";

        "INSERT INTO job_company_infor (info_key, company_specialization,no_of_agents,no_of_resumes, org_structure)
                         VALUES('null','$company','$agents','$resumes','$structure')";

The problem is, it is only saving date on one table.

please assist me as I am new to php.

Thanks in advance.

4
  • php.net/manual/en/mysqli.multi-query.php Commented Jul 29, 2015 at 7:50
  • 1
    You turned off error reporting, did you know that? Commented Jul 29, 2015 at 7:50
  • In your code, there is no mysqli_query that executes the querries Commented Jul 29, 2015 at 7:53
  • STOP! you're vulnerable to sql injection! Moreover, multiple queries in a single statement are NOT a good idea. Commented Jul 29, 2015 at 8:56

3 Answers 3

2
 $link = mysqli_connect("host", "username", "password", "database");
 $sql =  "INSERT INTO job_spec_contact (contact_info_key, datee,company_name,Physical_Address, recruitment_person,email,Telephone)
         VALUES('null','$datee','$company','$PAddress','$recruiter','$email','$telephone') ;";

 $sql . = "INSERT INTO job_company_infor (info_key, company_specialization,no_of_agents,no_of_resumes, org_structure)
         VALUES('null','$company','$agents','$resumes','$structure')";
 mysqli_multi_query($link, $sql);
Sign up to request clarification or add additional context in comments.

Comments

0

Sorry for late reply.

If you are using mysql: (Not recommended due to unsecure)

$conn = mysql_connect('localhost','username','password', true, 65536) or die("cannot connect");
mysql_select_db('YourDBName') or die("cannot use database");

if(isset($_POST['Submit'])){
     $datee = $_POST['date'];
     $company = $_POST['company'];
     $PAddress = $_POST['PAddress'];
     $recruiter = $_POST['recruiter'];
     $email = $_POST['email'];
     $telephone = $_POST['telephone'];
     $company = $_POST['company'];
     $agents = $_POST['agents'];
     $resumes = $_POST['resumes'];
     $structure = $_POST['structure'];
}

$result = mysql_query("
    INSERT INTO job_spec_contact (contact_info_key, datee, company_name, Physical_Address, recruitment_person,email,Telephone)
                         VALUES('null','$datee','$company','$PAddress','$recruiter','$email','$telephone');

    INSERT INTO job_company_infor (info_key, company_specialization,no_of_agents,no_of_resumes, org_structure)
                         VALUES('null','$company','$agents','$resumes','$structure');

");

if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}


while ($row = mysql_fetch_assoc($result)) {
    echo $row['datee'];
    echo $row['company_name'];
    ......
}

mysql_free_result($result);
?>

If you are using mysqli: (Recommended),

$conn = mysqli_connect('localhost','username','password') or die("cannot connect");
mysqli_select_db($conn, 'YourDBName') or die("cannot use database");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if(isset($_POST['Submit'])){
     $datee = $_POST['date'];
     $company = $_POST['company'];
     $PAddress = $_POST['PAddress'];
     $recruiter = $_POST['recruiter'];
     $email = $_POST['email'];
     $telephone = $_POST['telephone'];
     $company = $_POST['company'];
     $agents = $_POST['agents'];
     $resumes = $_POST['resumes'];
     $structure = $_POST['structure'];
}

$query  = "INSERT INTO job_spec_contact (contact_info_key, datee, company_name, Physical_Address, recruitment_person, email, Telephone)
                         VALUES('null','$datee','$company','$PAddress','$recruiter','$email','$telephone')";
$query .= "INSERT INTO job_company_infor (info_key, company_specialization, no_of_agents, no_of_resumes, org_structure)
                         VALUES('null','$company','$agents','$resumes','$structure')";

if ($mysqli->multi_query($query)) {
    do {
        if ($result = $mysqli->store_result()) {
            while ($row = $result->fetch_row()) {
                printf("%s\n", $row[0]);//test here your values.
               //$datee = $row['datee'];
            }
            $result->free();
        }
    } while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();

Hope this helps.

Note:

  1. Check always POST is present or not by isset. (assume, input name Submit)

  2. Use MySQLi/PDO instead of MySQL to avoid SQL injection.

  3. Debug code by using echo, print_r, var_dump, etc.,

  4. Try to use field names are in same pattern. For ex, Instead of Physical_Address, use physical_address like other fields. Telephone to telephone. datee to job_contact_date, etc.,

Comments

0

Just execute your queries one by one. And DO NOT write error_reporting(0); or the errors won't show. Plus where is your DB ?

 $sql1 =  "INSERT INTO job_spec_contact (contact_info_key, datee,company_name,Physical_Address, recruitment_person,email,Telephone)
             VALUES('null','$datee','$company','$PAddress','$recruiter','$email','$telephone')";

$sql2 = "INSERT INTO job_company_infor (info_key, company_specialization,no_of_agents,no_of_resumes, org_structure)
             VALUES('null','$company','$agents','$resumes','$structure')";

mysqli_query($db,$sql1) or die('error '.$sql1.'<br>'.mysqli_error($db));
mysqli_query($db,$sql2) or die('error '.$sql2.'<br>'.mysqli_error($db));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.