将PHP数组与其他列一起放入MySQL [英] Putting PHP array into MySQL with additional columns
问题描述
我有一个关于PHP数组并将其作为单个记录插入MySQL数据库的问题.我对数组进行了排序,并且可以正常工作.
I have a question about PHP arrays and inserting them as single records into a MySQL database. I have the array sorted and that is working as it should.
这就是我需要的数组:
$files = array();
foreach($_FILES['file']['tmp_name'] as $key => $tmp_name )
{
$files[$key] = array
(
$file_name = $_FILES['file']['name'][$key],
$file_type=$_FILES['file']['type'][$key],
$file_size =$_FILES['file']['size'][$key],
$file_tmp =$_FILES['file']['tmp_name'][$key]
);
}
这是我将数组作为单独的行插入到数据库中所需要的:
This is what I have for the array to insert them as separate rows in to the database:
$new = array();
foreach($files as $key => $value)
{
$new[] = "'".implode("','", $value)."'";
}
$query = "(".implode("), (",$new).")";
$sqlone = "INSERT INTO files (filename, filetype, filesize, filetempname) VALUES ".$query."";
if (!mysql_query($sqlone, $conn))
{
die("Error: " . mysql_error().".");
}
我遇到的问题是:我想向查询中添加其他信息,但是我不确定如何做到这一点.
The issue I am running into is this: I want to add extra information to the query but I am not entirely sure how to do this.
我希望能够添加对文件附加到的电子邮件的引用.我基本上希望查询如下:
I want to be able to add a reference to the email that the files were attached to. I basically want the query to be as follows:
$sqlone = "INSERT INTO files (filename, filetype, filesize, filetempname, mailid //this is the extra column in the database) VALUES ".$query.", '1'// this is the corresponding value";
我遇到的问题是,尝试向其中添加更多信息时出现错误.
The issue I am running in to is that I get an error when trying to add extra information to it.
你们有没有可以给我的指点?
Are there any pointers you guys could give me?
预先感谢
推荐答案
只需更改添加括号的位置(并引用您的输入):
Just change where your parenthesis is added (and quote your inputs):
$query = "('".implode("'), ('",$new);
$sqlone = "INSERT INTO files (filename, filetype, filesize, filetempname, mailid) VALUES ".$query."', '1')";
应生成SQL:
INSERT INTO files (filename, filetype, filesize, filetempname, mailid) VALUES
('<file_name>', '<file_type>', '<file_size>', '<file_tmp_name>', '1')
这篇关于将PHP数组与其他列一起放入MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!