繁体   English   中英

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

[英]PHP - Transfer Data from table to another within a database

我还是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
$query = mysql_query("SELECT * FROM employee_database WHERE Employee_Name= '$username'");
$result = mysql_query($query) or die(mysql_error());

在第一行中, mysql_query()返回一个资源

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

mysql_query()需要SQL查询字符串作为参数。

轻松修复:

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

一些进一步的提示

  1. 在咨询论坛之前发送谷歌错误消息
  2. 在咨询论坛之前阅读文档
  3. 不要在PHP中使用mysql扩展,不推荐使用
  4. 使用预准备语句 - 您的代码容易受到SQL注入攻击

首先,我必须告诉您有关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' 

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM