Php 将多个表单上载到数据库
好的,我正在上传图片,然后是图片描述。图像已上载到数据库,但文本字段未上载 我认为我的代码是正确的,但由于某些原因,数据库中没有任何内容。我认为数组中没有存储任何内容,我不知道为什么 这是我的表格Php 将多个表单上载到数据库,php,mysql,sql,Php,Mysql,Sql,好的,我正在上传图片,然后是图片描述。图像已上载到数据库,但文本字段未上载 我认为我的代码是正确的,但由于某些原因,数据库中没有任何内容。我认为数组中没有存储任何内容,我不知道为什么 这是我的表格 <form method="post" enctype="multipart/form-data" action="process.php"> <div id="filediv"> <div id="imagefiles"> <input type="hidd
<form method="post" enctype="multipart/form-data" action="process.php">
<div id="filediv">
<div id="imagefiles">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<label>Upload File:
<input name="userfile[]" type="file" id="userfile" multiple></label>
<label>Item Name: <input name='itemname[]' type="text"></label>
<label>Item Description: <input name="itemdesc[]" type="text"></label>
<label>Item Timeframe: <input name='itemtime[]' type="text"></label>
<label>Item Donor: <input name='itemdonor[]' type="text"></label>
<label>Hidden Information: <input name='hidden[]' type="text"></label>
</div>
</div>
<br>
<input type="button" id ="thebutton" value="Add Another File" />
<input name="UploadFile" type="submit" />
</form>
上载文件:
项目名称:
项目说明:
项目时间表:
项目捐助者:
隐藏信息:
这是我的功能
function uploadFile()
{
include 'functions.php';
hasSession();
session_start();
$dbhost = '';
$dblogin = '';
$dbpass = '!';
$dbbase = '';
$conn = mysqli_connect($dbhost, $dblogin, $dbpass, $dbbase);
if(mysqli_connect_errno()){
printf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error());
exit;
}
$arrCount = count($_POST["itemname"]);
$name[] = $_POST["itemname"];
$donor[] = $_POST["itemdonor"];
$desc[] = $_POST["itemdesc"];
$time[] = $_POST["itemtime"];
$hide[] = $_POST["hidden"];
for($i=0; $i<$arrCount; $i++){
$sql = "INSERT into items (name, donor, time, desc, hidden) VALUES ('$name[$i]', '$donor[$i]', '$desc[$i]', '$time[$i]', $hide[$i]')";
}
$numFiles = count($_FILES['userfile']['name']);
for($x=0;$x<$numFiles;$x++){
//($_FILES['userfile'][$counter]['error'] == 0) &&
if (($_FILES['userfile']['size'][$x] > 0)){
$fileName = $_FILES['userfile']['name'][$x];
$tmpName = $_FILES['userfile']['tmp_name'][$x];
$fileSize = $_FILES['userfile']['size'][$x];
$fileType = $_FILES['userfile']['type'][$x];
if ((($_FILES['userfile']['type'][$x] == "image/gif")
|| ($_FILES['userfile']['type'][$x]== "image/jpeg")
|| ($_FILES['userfile']['type'][$x] == "image/png")
|| ($_FILES['userfile']['type'][$x] == "image/pjpeg")))
{
$fp=fopen($tmpName,'r');
$content=fread($fp,filesize($tmpName));
$SourceImage=imagecreatefromstring($content);
$SourceWidth=imagesx($SourceImage);
$SourceHeight=imagesy($SourceImage);
$ratio=$SourceWidth/$SourceHeight;
$DestWidth=100;
$DestHeight=$DestWidth/$ratio;
$DestinationImage=imagecreatetruecolor($DestWidth,$DestHeight);
imagecopyresampled($DestinationImage,$SourceImage,0,0,0,0,$DestWidth,$DestHeight,$SourceWidth,$SourceHeight);
ob_start();
imagejpeg($DestinationImage);
$BinaryThumbnail = ob_get_contents();
ob_end_clean();
$thumb = addslashes($BinaryThumbnail);
$content=addslashes($content);
fclose($fp);
$fileName = addslashes($fileName);
mysql_connect('hartslogmuseum.db.11661984.hostedresource.com','hartslogmuseum','Alexandria1!');
mysql_select_db('hartslogmuseum');
mysql_query("INSERT INTO images (name,size,type,content,thumbnail) VALUES ('$fileName','$fileSize','$fileType','$content','$thumb')") or die('Error, query failed');
}else{
$fp=fopen($tmpName,'r');
$content=fread($fp,filesize($tmpName));
$content=addslashes($content);
fclose($fp);
$fileName = addslashes($fileName);
$link = mysqli_connect('!');
mysqli_select_db($link,'');
mysqli_query($link,"INSERT INTO images (name,size,type,content) VALUES ('$fileName','$fileSize', '$fileType','$content')");
}
}else{
echo "Error: ". $_FILES['userfile']['error'][$x]."<br/>";
}
}
echo "<script>alert('The file(s) has been uploaded');location.replace('uploaded.php');</script>";
}
函数上传文件()
{
包括“functions.php”;
hasSession();
会话_start();
$dbhost='';
$dblogin='';
$dbpass='!';
$dbbase='';
$conn=mysqli_connect($dbhost、$dblogin、$dbpass、$dbbase);
if(mysqli\u connect\u errno()){
printf(“无法连接到MySQL服务器。错误代码:%s\n”,mysqli_connect_error());
出口
}
$arrCount=count($_POST[“itemname”]);
$name[]=$\u POST[“itemname”];
$PROVIDER[]=$_POST[“项目捐赠者”];
$desc[]=$_POST[“itemdesc”];
$time[]=$\u POST[“itemtime”];
$hide[]=$\u POST[“隐藏”];
for($i=0;$idesc
是保留字,需要用反勾号包装
例如:
`desc`
正如MonkeyZeus所说的,并且即将写相同的注释(就在同一时间),最好用反勾来包装表名和列
例如:
$sql = "INSERT into `items` (`name`, `donor`, `time`, `desc`, `hidden`) VALUES ('$name[$i]', '$donor[$i]', '$desc[$i]', '$time[$i]', $hide[$i]')";
编辑:
我注意到有一个缺少的报价
'$time[$i]', $hide[$i]'
-——————————————^
改为:
'$time[$i]', '$hide[$i]'
全行代码:
$sql = "INSERT into `items` (`name`, `donor`, `time`, `desc`, `hidden`) VALUES ('$name[$i]', '$donor[$i]', '$desc[$i]', '$time[$i]', '$hide[$i]')";
我建议您更改desc列,因为desc是order的一个保留mysql字,它的一个应用程序是:
SELECT * FROM my_table ORDER BY id DESC
因此,您不能使用desc作为列名
mysql_error()
每次查询后的函数,即
mysql_query($sql) or die(mysql_error())
因此,将该列名从desc更改为另一个不是mysql保留字的名称。
祝你好运!执行查询的代码在哪里?我可能完全疯了,但你不应该像这样调用你的值吗$name[0][$I]
,因为你是这样分配它们的$name[]=$\u POST[“itemname”]
。如果您想保持SQL不变,那么可以尝试分配如下变量:
$name=$\u POST[“itemname”];?我同意MonkeyZeus的观点,但是您可能应该更改输入名称设置。`$desc[]`实际上应该是$desc
。否则,此变量将解析为长度为1的数组,其中一对的值是itemdesc的值数组。是的,用户可以添加任意次数的表单,表单会重新出现在屏幕上。因此会有重复的表单输入。'$arrCount=count($\u POST[“itemname”])$name=$\u POST[“itemname”];$provider=$\u POST[“itemdesc”];$descr=$\u POST[“itemdesc”];$time=$\u POST[“itemtime”];$hide=$\u POST[“hidden”];for($i=0;$i查看了这个答案,所以它可能会帮助您-我谷歌“插入mysqli php输入数组括号”并找到了答案。我很难说你的文本输入到底有什么问题。@pureblood我通常建议在使用MySQLYes时将所有字段和表名都用反勾号括起来。我同意。我只是想像你写评论一样写这些。@MonkeyZeusThanks。我纠正了这个错误,但日期仍然没有到达正确的位置datebase。我编辑了我的原始帖子以显示完整的功能。图像功能工作正常,文件正在上载。只是表单的文本字段没有通过hi think$arrCount=count($\u post[“itemname”]))绝对暗示了多个项目的可能性。<代码> MySqL< <代码>和<代码> MySqLi<!还考虑上面提到的“PurRead和PHPfiddle”。
mysql_query($sql) or die(mysql_error())