2015-02-07 64 views
0

中的非對象我有以下錯誤,這與我用於註冊用戶的表單處理文件完全相同,但是我已將其更改爲相應的表和列。雖然每次都能正常工作。 這裏就是錯誤所在的代碼:調用成員函數execute()對

$sql = "INSERT INTO events1 (eventname,about,website) VALUES (:yas,:yasas,:yasasha)"; 
 
$q = $conn->prepare($sql); 
 
$q->execute(array(':yas'=>$eventname,':yasas'=>$about,':yasasha'=>$website)); 
 
下面是完整的代碼:

<?php 
 
$servername = "localhost"; 
 
$username = "root"; 
 
$password = "Af2vaz93j68"; 
 
$dbname = "pdo_ret"; 
 

 
// Create connection 
 
$conn = new mysqli($servername, $username, $password, $dbname); 
 
// Check connection 
 
if ($conn->connect_error) { 
 
    die("Connection failed: " . $conn->connect_error); 
 
} 
 
$eventname = $_POST['eventname']; 
 
$about = $_POST['about']; 
 
$website = $_POST['website']; 
 

 

 
if($eventname == '') { 
 
    $errmsg_arr[] = 'You must enter your Email'; 
 
    $errflag = true; 
 
} 
 
if($about == '') { 
 
    $errmsg_arr[] = 'You must enter your Password'; 
 
    $errflag = true; 
 
} 
 
if($website == '') { 
 
    $errmsg_arr[] = 'You must enter First Name'; 
 
    $errflag = true; 
 
} 
 

 
$sql = "INSERT INTO events1 (eventname,about,website) VALUES (:yas,:yasas,:yasasha)"; 
 
$q = $conn->prepare($sql); 
 
$q->execute(array(':yas'=>$eventname,':yasas'=>$about,':yasasha'=>$website)); 
 
    
 

 

 
if ($conn->query($sql) === TRUE) { 
 
    echo "New record created successfully"; 
 
} else { 
 
    echo "Error: " . $sql . "<br>" . $conn->error; 
 
} 
 

 
$conn->close(); 
 
?>

+0

您是否檢查過連接是否失敗?另請向我們展示您的**全**代碼。所以如果有其他的錯誤,我們馬上看到它。 (在你的文件(S)的頂部添加錯誤報告:'',並告訴我們,如果你得到任何更多的錯誤 – Rizier123 2015-02-07 15:15:51

+0

是的,連接沒問題,我會更新代碼 – SARUAV 2015-02-07 15:16:49

+0

什麼是錯誤信息?可能是某一列是必需的,但是你正試圖保存一個空字符串? – 2015-02-07 15:20:25

回答

2

特殊照顧混淆PDO和mysqli的。 mysqli不支持命名參數,所以你stmt不編譯,Mysqli::prepare返回false。此外,mysqli不支持通過mysqli_stmt::execute傳遞參數,因此即使切換到位置佔位符,執行也會失敗。

這是你需要什麼的mysqli:

$sql = "INSERT INTO events1 (eventname,about,website) VALUES (?,?,?)"; 
$stmt = $conn->prepare($sql); 

// check to make sure the statement was prepared without error 
if ($stmt) { 
    // the statement is good - proceed 
    $stmt->bind_param('sss', $eventname, $about, $website); 
    $stmt->execute(); 
} 

此外,這沒有意義的:

if ($conn->query($sql) === TRUE) { 
    echo "New record created successfully"; 
} else { 
    echo "Error: " . $sql . "<br>" . $conn->error; 
} 

這將只運行相同的查詢再次要麼插入的第二行完全相同的數據,或者可能根據您的模式創建重複鍵錯誤。

如果你想測試之前的查詢成功了,你會做這樣的事情:

$sql = "INSERT INTO events1 (eventname,about,website) VALUES (?,?,?)"; 
$stmt = $conn->prepare($sql); 

if ($stmt) { 
    $stmt->bind_param('sss', $eventname, $about, $website); 
    $success = $stmt->execute(); 
} else { 
    $success = false; 
} 

if ($success === true) { 
    echo "New record created successfully"; 
} else { 
    echo "Error: " . $sql . "<br>" . $conn->error; 
} 

如果你想使用PDO(我更喜歡和通常建議),您的代碼會是這個樣子:

$conn = PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); 
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 


try { 
    $sql = "INSERT INTO events1 (eventname,about,website) VALUES (:yas,:yasas,:yasasha)"; 
    $stmt = $conn->prepare($sql); 
    $stmt->execute(array(':yas'=>$eventname,':yasas'=>$about,':yasasha'=>$website)); 
    echo "New record created successfully"; 
} catch (PDOException $e) { 
    echo "Error: " . $sql . "<br>" . $e->getMessage(); 
} 
+0

我該如何解決這個問題?我的朋友告訴我,我仍然對PDO和mysqli感到困惑。 $ conn = new PDO(「mysql:host = $ dbhost; dbname = $ dbname」,$ dbuser,$ dbpass); – SARUAV 2015-02-07 15:27:01

+0

@SARUAV如果您想切換,我添加了PDO示例代碼。真的,你只需要選擇一個。我更喜歡PDO,但只要你一致,它應該沒問題。 – prodigitalson 2015-02-07 15:35:36

+1

謝謝,我會注意到這一點,我想我現在將與PDO一起:) – SARUAV 2015-02-07 15:39:27

相關問題