当我没有在上传输入中附加任何文件时,PHP INSERT 错误
当我未附加任何文件时,在 Postgresql 数据库中插入某些内容时出现错误。我有一个表单,其中包含一些输入,我将其插入到一个表中,还有一个输入用于将多个文件和一些信息上传到其他表。
空上传输入的错误:
致命错误:在第 60 行对 C:\wamp\www\arn\upload.php 中的非对象调用成员函数prepare()
LINE 60: $sth = $dbh->prepare("INSERT INTO arn_info (arn , modelos , familias , Problema , solucao) VALUES ( ? , ? , ? , ? , ? )");
我的表单:
<?php
$sth = $dbh->query("SELECT * FROM arn_info");
$sth->setFetchMode(PDO::FETCH_ASSOC);
if($sth){
$row = $sth->fetch();
echo '<form method="post" action="upload.php" enctype="multipart/form-data" >';
echo '<input type="hidden" name="id" />';
echo 'ARN: <input type="text" name="arn" /><br />';
echo 'Familias: <input type="text" name="familias" /> <br />';
echo 'Problema: <textarea class="tinymce" name="problema" cols="30" rows="10">';
echo '</textarea>';
echo 'Solução: <textarea class="tinymce" name="solucao" cols="30" rows="10">';
echo '</textarea>';
echo '<input type="hidden" name="MAX_FILE_SIZE" value="2000000">';
echo 'Anexos: <input name="userfile[]" type="file" class="multi" id="userfile">';
echo '<input name="upload" type="submit" class="box" id="upload" value="ADD ARN">';
echo '</form>';
}
?>
我的上传和插入 PHP:
<?php
if(isset($_POST['upload']))
{
$uploadDir = 'uploads/';
$fileArn = $_POST['arn'];
$id = $_POST['id'];
$arn = $_POST['arn'];
$modelos = $_POST['modelos'];
$familias = $_POST['familias'];
$problema = $_POST['problema'];
$solucao = $_POST['solucao'];
foreach ($_FILES["userfile"]["error"] as $key => $error)
{
if ($error == UPLOAD_ERR_OK)
{
$fileName = $_FILES['userfile']['name'][$key];
$tmpName = $_FILES['userfile']['tmp_name'][$key];
$fileSize = $_FILES['userfile']['size'][$key];
$fileType = $_FILES['userfile']['type'][$key];
$folderName = $_POST['arn'] . "/";
if (!file_exists ( $uploadDir . $folderName )) {
$arnFolder = mkdir($uploadDir . $folderName, 0777);
}
$filePath = $uploadDir . $folderName . basename($fileName);
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error adding Files";
exit;
}
include 'includes/connection.php';
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
if (!file_exists ($fileName)) {
$info = array ($fileName , $fileSize , $fileType , $filePath , $fileArn );
$sth = $dbh->prepare("INSERT INTO upload2 (name, size, type, path , id_arn ) VALUES ( ? , ? , ? , ? , ? )");
$sth->execute($info);
}
}
} //END OF LOOP
$parametros = array($arn,$modelos,$familias,$problema,$solucao);
$sth = $dbh->prepare("INSERT INTO arn_info (arn , modelos , familias , problema , solucao) VALUES ( ? , ? , ? , ? , ? )");
$sth->execute($parametros);
if($sth){
header("location: admin.php?inserted=1");
}
}
?>
如果我附加一个或多个文件一切正常!
谢谢
I have an error inserting some content in a Postgresql Database when I don't attach any file. I have a form with some inputs that I insert in one table and one input to upload multiple files and some info to other table.
The error with empty upload input:
Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\arn\upload.php on line 60
LINE 60: $sth = $dbh->prepare("INSERT INTO arn_info (arn , modelos , familias , problema , solucao) VALUES ( ? , ? , ? , ? , ? )");
MY FORM:
<?php
$sth = $dbh->query("SELECT * FROM arn_info");
$sth->setFetchMode(PDO::FETCH_ASSOC);
if($sth){
$row = $sth->fetch();
echo '<form method="post" action="upload.php" enctype="multipart/form-data" >';
echo '<input type="hidden" name="id" />';
echo 'ARN: <input type="text" name="arn" /><br />';
echo 'Familias: <input type="text" name="familias" /> <br />';
echo 'Problema: <textarea class="tinymce" name="problema" cols="30" rows="10">';
echo '</textarea>';
echo 'Solução: <textarea class="tinymce" name="solucao" cols="30" rows="10">';
echo '</textarea>';
echo '<input type="hidden" name="MAX_FILE_SIZE" value="2000000">';
echo 'Anexos: <input name="userfile[]" type="file" class="multi" id="userfile">';
echo '<input name="upload" type="submit" class="box" id="upload" value="ADD ARN">';
echo '</form>';
}
?>
MY UPLOAD AND INSERT PHP:
<?php
if(isset($_POST['upload']))
{
$uploadDir = 'uploads/';
$fileArn = $_POST['arn'];
$id = $_POST['id'];
$arn = $_POST['arn'];
$modelos = $_POST['modelos'];
$familias = $_POST['familias'];
$problema = $_POST['problema'];
$solucao = $_POST['solucao'];
foreach ($_FILES["userfile"]["error"] as $key => $error)
{
if ($error == UPLOAD_ERR_OK)
{
$fileName = $_FILES['userfile']['name'][$key];
$tmpName = $_FILES['userfile']['tmp_name'][$key];
$fileSize = $_FILES['userfile']['size'][$key];
$fileType = $_FILES['userfile']['type'][$key];
$folderName = $_POST['arn'] . "/";
if (!file_exists ( $uploadDir . $folderName )) {
$arnFolder = mkdir($uploadDir . $folderName, 0777);
}
$filePath = $uploadDir . $folderName . basename($fileName);
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error adding Files";
exit;
}
include 'includes/connection.php';
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
if (!file_exists ($fileName)) {
$info = array ($fileName , $fileSize , $fileType , $filePath , $fileArn );
$sth = $dbh->prepare("INSERT INTO upload2 (name, size, type, path , id_arn ) VALUES ( ? , ? , ? , ? , ? )");
$sth->execute($info);
}
}
} //END OF LOOP
$parametros = array($arn,$modelos,$familias,$problema,$solucao);
$sth = $dbh->prepare("INSERT INTO arn_info (arn , modelos , familias , problema , solucao) VALUES ( ? , ? , ? , ? , ? )");
$sth->execute($parametros);
if($sth){
header("location: admin.php?inserted=1");
}
}
?>
IF I Attach One or More Files All Works Fine !
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不好的是,以上是很好的一般建议,但不是您问题的原因。该问题是由于您将连接包含在循环内而引起的,如果没有文件上传,则该连接将永远不会执行。这是修复方法:
My bad, the above is good general advice, but not the cause of your issue. The issue is caused by the fact that you include the connection inside the loop which will never be executed if there are no file uploads. Here's the fix:
看来你的 $sth 不是一个对象。如果您想查看更具描述性的消息,则必须告诉 PDO 您确实想查看错误。尝试以下操作(可能在“connection.php”中):
这将确保 PDO 输出一条消息。
It seems your $sth is not an object. If you want to see a more descriptive message, you'll have to tell PDO that you actually want to see errors. Try the following (probably in 'connection.php'):
That will make sure PDO will output a message.
在您的文件中找不到
$dbh
的定义。因此该变量不存在,您无法对其调用prepare()
方法。The definition of
$dbh
is nowhere to be found in your file. So the variable doesn't exist and you can't call theprepare()
method on it.