简体   繁体   中英

PHP - Transfer Data from table to another within a database

I'm still new to PHP programming, my goal here is to

-transfer data from Table to another Table

<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>

I would be glad if anyone were to able to send an example of 2 sql query. Thanks

The error now I'm getting is an odd one, i haven't encounter this type of ERROR before. Do anyone have knowledge on this ERROR. Please suggest a solution to this ERROR. Thanks

Page Error

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

In the first line, mysql_query() returns a resource .

In the second line, you pass the return value back into another call to mysql_query() .

mysql_query() expects a SQL query string as parameter.

Easy fix:

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

Some further tips:

  1. google error messages before consulting on a forum
  2. read the documentation before consulting a forum
  3. don't use mysql extension in PHP, it is deprecated !
  4. use prepared statements - your code is prone to SQL injection attacks

First I have to tell you about your query that it is prone to SQL injection. Second mysql_* functions are deprecated and no more will be supported.

You can do it with sinle query. You may use this 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`, 
                              '$_POST[\'No_Days\']', 
                              '$_POST[\'StartDate\']', 
                              '$_POST[\'EndDate\']', 
                              '$_POST[\'Leave_Reason\']' 
                         FROM `employee_database` 
                        WHERE `Employee_Name` = '$username' 

There is such a thing as INSERT SELECT . (I've linked to MySQL documentation, but so far as I know it's standard SQL.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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