简体   繁体   中英

How to use variable with LOAD DATA INFILE MYSQL, PHP

I have the following code, which correctly inserts data into the mysql database, however when I upload the file, that particular file will never contain a certain column called 'date' which exists in the mysql table. I would like to put it in with each row... So Basically, how do I make the last column the variable $dadate?

Here is the code:

<?php
require_once('header2.php');
require_once('access.php');
?>
<head>
<title>Upload</title> 
</head>
<body>
<?php
$filedir = addslashes($_FILES['file']['tmp_name']);
$dadate = $_POST['datadate'];
echo $dadate;
if (isset($_POST['submit'])) {
$sql="LOAD DATA LOCAL INFILE '$filedir' INTO TABLE employeehourstest FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' IGNORE 18 LINES (@dummy, @dummy, @dummy, @dummy, employeeid, @dummy, @dummy, paycode, @dummy, @dummy, @dummy, employeehours, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)";
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
if ($result) {
echo "<h1>" . "File ". $_FILES['file']['name'] ." uploaded
successfully." . "</h1>";
}
}
?>
</body>
</html>

Here is my table how it is uploaded with this code:

在此处输入图片说明

Here is the php upload result with $dadate shown:

在此处输入图片说明

Add this to the end of LOAD DATA query

SET workdate = $dadate 

see more info here

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