PHP,mySQL。使用php动作脚本保存在多个不同的表上

时间:2015-07-29 07:47:50

标签: php mysql

我正在处理一个php脚本,并希望将数据插入三个不同的表。我怎么能在php动作脚本上做到这一点。

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')";

问题是,它只在一张桌子上保存日期。

请帮助我,因为我是php的新手。

提前致谢。

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

只需逐个执行您的查询。并且不要写error_reporting(0);或错误不会显示。再加上你的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));

答案 2 :(得分:0)

很抱歉迟到的回复。

如果您使用的是mysql:(由于不安全而不推荐)

$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);
?>

如果您使用的是mysqli:(推荐),

$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();

希望这有帮助。

注意:

  1. 检查isset是否存在POST。 (假设,输入名称提交)

  2. 使用MySQLi / PDO而不是MySQL来避免SQL注入。

  3. 使用echo,print_r,var_dump等调试代码,

  4. 尝试使用相同模式的字段名称。对于ex,而不是Physical_Address,像其他字段一样使用physical_address。电话到电话。约会到job_contact_date等,