提交表单时,PHP函数回显失败

时间:2019-05-12 06:26:43

标签: php mysql pdo

我试图在PDO中使用更新命令,但是当我提交表单时,它在行上显示未定义的索引

我已将POST变量更改为一个静态数字,以查看问题是否出在post变量中,但是仍然没有插入数据,尽管现在它只是回显我设置的“失败”消息。

session_start();
/* DATABASE CONFIGURATION */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'dbf');
define("BASE_URL", "http://localhost/delivery/"); // Eg. http://yourwebsite.com



function getDB() 
{
$dbhost=DB_SERVER;
$dbuser=DB_USERNAME;
$dbpass=DB_PASSWORD;
$dbname=DB_DATABASE;
try {
$dbConnection = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); 
$dbConnection->exec("set names utf8");
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbConnection;
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}

}

class userClass
{

public function packageedit($floor,$fimg,$pimg)
{
try{
$db = getDB();
if($fimg==0 AND $pimg==0)
{}
elseif ($fimg==0) 
{
$stmt = $db->prepare("UPDATE package SET pimg=:pimg WHERE floor=:floor");
$stmt->bindParam("floor", $floor,PDO::PARAM_STR);
$stmt->bindParam("pimg", $pimg,PDO::PARAM_STR);
$stmt->execute();
$db = null;
return true;
}
elseif ($pimg==0) {
$stmt = $db->prepare("UPDATE package SET fimg=:fimg WHERE floor=:floor");
$stmt->bindParam("floor", $floor,PDO::PARAM_STR);
$stmt->bindParam("fimg", $fimg,PDO::PARAM_STR);
$stmt->execute();
$db = null;
return true;
}
else {}
}
catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}
}
}

$floor=$_POST['floor'];


    if(empty($_FILES['image']['name']))
    {
        $image=0;
    }
    else
    {
        $fileName = time().'_'.$_FILES['image']['name'];
        $sourcePath = $_FILES['image']['tmp_name'];
            $targetPath = "uploads/package/".$fileName;
            if(move_uploaded_file($sourcePath,$targetPath))
            {
                $image = $fileName;
            }
    }


    if(empty($_FILES['image2']['name']))
    {
        $image2=0;
    }
    else
    {
        $fileName = time().'_'.$_FILES['image2']['name'];
        $sourcePath = $_FILES['image2']['tmp_name'];
            $targetPath = "uploads/package/".$fileName;
            if(move_uploaded_file($sourcePath,$targetPath))
            {
                $image2 = $fileName;
            }
    }



    $uid=$userClass->packageedit($floor,$image,$image2);
    if($uid)
    {
        echo "Successful.";
    }
    else
    {
        echo "failed";
    }

预期结果是更新并保存文件。

0 个答案:

没有答案