PHP - 将数据从表传输到数据库中的另一个表

时间:2014-01-02 08:38:34

标签: php mysql sql

我还是PHP编程的新手,我的目标是

- 将数据从表传输到另一个表

<html> <head> <title>Sending Application Form by Email</title> </head> <body>

<?PHP

include ("dbase.php");

if(isset($_COOKIE['ID_my_site']))
    {
    $username = $_COOKIE['ID_my_site'];
    $query = mysql_query("SELECT * FROM employee_database WHERE Employee_Name= '$username'");
    $result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result))
    {       
    $insert = mysql_query ("INSERT INTO `leave_requested`
                            (`Employee_Name`,
                            `Employee_ID`,
                            `Designation`,
                            `Department`,
                            `Days_on_Leave`, 
                            `Leave_Start_On`,
                            `Leave_End_On`,
                            `Reason for Leave`)
                     SELECT `Employee_Name`,
                            `Employee_ID`,
                            `Designation`,
                            `Department`,
                            '$Days',
                            '$Start',
                            '$End',
                            '$Reason'
                       FROM `employee_database`
                      WHERE `Employee_Name` = '$username'");
/*  
    ("INSERT INTO leave_requested (Employee_Name, Employee_ID, Designation, Department, Days_on_Leave, Leave_Start_On, Leave_End_On, Reason for Leave)
    SELECT Employee_Name, Employee_ID, Designation, Department FROM employee_database




    VALUES ('$row[Employee_Name]','$row[Employee_ID]','$row[Designation]','$row[Department]','$_POST['No_Days']','$_POST['StartDate']','$_POST['EndDate']','$_POST['Leave_Reason']')");
    */
    }
}
mysql_close ($qry);
?>

<?php
    require("C:\AppServ\www\Project\PHPMailer_5.2.4\class.phpmailer.php");
    $mail = new PHPMailer();
$mail->IsSMTP();

//GMAIL config
$mail->SMTPAuth   = true; //                                                    Enable SMTP authentication
$mail->SMTPSecure = "tls"; //                                                   Sets the prefix to the server
$mail->Host       = "smtp.gmail.com"; //                                        Sets GMAIL as the SMTP server
$mail->Port       = '587'; //                                                   Sets the SMTP port for the GMAIL server

$mail->Username = ("my@gmail.com"); //                              SMTP UserName (Email Address)
$mail->Password = "******"; //                                              SMTP PassWord (Email Address PassWord)
$mail->SMTPDebug = 2; //                                                        Enables SMTP debug information (for testing)## 1 = errors and messages ## 2 = messages only ##

$mail->From = $mail->Username; //                                               Sender Email Account on Email
$mail->FromName = 'HR Management'; //                                                   Sender Name on Email

$mail->AddAddress('my@yahoo.com', 'JD'); //                 Add a recipient Email and Name
$mail->WordWrap = 50;
$mail->IsHTML(true);

$mail->Subject = '$username have added Leave Requested'; //                                         Email Subject
$mail->Body    = 'This is the HTML message body <b>in bold!</b>'; //            HTML Plain Text
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; // HTML Function

if(!$mail->Send())
{
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
    echo 'Message has been sent';
 ?>
</body>
</html>

如果有人能够发送2个sql查询的示例,我会很高兴。感谢

我现在得到的错误是一个奇怪的错误,我之前没有遇到过这种类型的错误。 有没有人知道这个错误。请为此错误建议解决方案。感谢

页面错误

*You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #4' at line 1

3 个答案:

答案 0 :(得分:2)

$query = mysql_query("SELECT * FROM employee_database WHERE Employee_Name= '$username'");
$result = mysql_query($query) or die(mysql_error());

在第一行中,mysql_query()返回resource

在第二行中,您将返回值传递回另一个mysql_query()的调用。

mysql_query()期望SQL查询字符串作为参数。

轻松修复:

$query = "SELECT * FROM employee_database WHERE Employee_Name= '$username'";
$result = mysql_query($query) or die(mysql_error());

一些进一步的提示:

    在咨询论坛之前
  1. google error messages
  2. 在咨询论坛之前阅读文档
  3. 不要使用mysql extension in PHP, it is deprecated
  4. 使用预准备语句 - 您的代码很容易SQL injection attacks

答案 1 :(得分:0)

首先,我必须告诉您有关SQL注入容易出错的查询。不推荐使用第二个mysql_*函数,不再支持这些函数。

你可以用sinle查询来做。您可以使用此查询:

 INSERT INTO `leave_requested` (`Employee_Name`, 
                              `Employee_ID`, 
                              `Designation`, 
                              `Department`, 
                              `Days_on_Leave`, 
                              `Leave_Start_On`, 
                              `Leave_End_On`, 
                              `Reason for Leave`) 
                       SELECT `Employee_Name`, 
                              `Employee_ID`, 
                              `Designation`, 
                              `Department`, 
                              '$_POST[\'No_Days\']', 
                              '$_POST[\'StartDate\']', 
                              '$_POST[\'EndDate\']', 
                              '$_POST[\'Leave_Reason\']' 
                         FROM `employee_database` 
                        WHERE `Employee_Name` = '$username' 

答案 2 :(得分:0)

INSERT SELECT之类的东西。 (我已经链接到MySQL文档,但据我所知它是标准的SQL。)