所有人的好日子
我的php和mysql有问题。一切正常,直到我将新内容添加到我的数据库,现在我的图像在我点击创建时给了我错误。
以下是我得到的错误:
注意:未定义的变量:C:\ wamp \ www \ SA-Property中的conn 第140行的Finder \ create.php
致命错误:在非对象上调用成员函数lastInsertId() 在C:\ wamp \ www \ SA-Property Finder \ create.php第140行
这是我的代码:
//START IMAGES
// insert data
if ($valid) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO properties (title,description,area,map,status,type,bedrooms,bathrooms,parking,garage,garden,pool,price,pets,agentnum,agentemail) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$q = $pdo->prepare($sql);
$q->execute(array($title,$description,$area,$map,$status,$type,$bedrooms,$bathrooms,$parking,$garage,$garden,$pool,$price,$pets,$agentnum,$agentemail));
Database::disconnect();
}
**$last_id = $pdo->lastInsertId();**
$directory="uploads/";
$directoryagents="agents/";
$a=0;
foreach ($_FILES['agentfile']['name'] as $nameFile) {
$ext = pathinfo($nameFile, PATHINFO_EXTENSION);
if(is_uploaded_file($_FILES['agentfile']['tmp_name'][$a]))
{
move_uploaded_file($_FILES['agentfile']['tmp_name'][$a], $directoryagents.$last_id."_".$a.".".$ext);
}
$fileandpath = $directoryagents.$last_id."_".$a.".".$ext;
$pdo1 = Database::connect();
$pdo1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql1 = "UPDATE properties SET agentimageurl=? WHERE PropertyId=?";
$q1 = $pdo1->prepare($sql1);
$q1->execute(array($fileandpath,$last_id));
Database::disconnect();
}
foreach ($_FILES['file']['name'] as $nameFile) {
$ext = pathinfo($nameFile, PATHINFO_EXTENSION);
if(is_uploaded_file($_FILES['file']['tmp_name'][$a]))
{
move_uploaded_file($_FILES['file']['tmp_name'][$a], $directory.$last_id."_".$a.".".$ext);
}
$fileandpath = $directory.$last_id."_".$a.".".$ext;
$pdo1 = Database::connect();
$pdo1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql1 = "INSERT INTO images (propertyid,imageurl) values(?, ?)";
$q1 = $pdo1->prepare($sql1);
$q1->execute(array( $last_id,$fileandpath));
Database::disconnect();
$a++;
}
header("Location: admin_list.php");
}
这是第140行:
$last_id = $pdo->lastInsertId();
我只是不明白它运作良好,似乎无法看出我的问题所在。
答案 0 :(得分:0)
由于对象$ pdo在$ valid上下文之外不存在,我的建议是从is($ valid)检查中删除对象的创建。
$pdo = Database::connect();
// insert data
if ($valid) {
//$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
...
}
你应该用
做同样的事情Database::disconnect();
将它放在if检查之后。
希望它有所帮助。