PHP INSERT Error When I Don't Attach any File In Upload Input
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 = $_开发者_Go百科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
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:
<?php
include 'includes/connection.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;
}
$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");
}
}
?>
Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\arn \upload.php on line 60
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'):
<?php
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
That will make sure PDO will output a message.
The definition of $dbh
is nowhere to be found in your file. So the variable doesn't exist and you can't call the prepare()
method on it.
精彩评论