2013-08-05 101 views
-2

我的代碼有什麼問題嗎?我一直在爲bind_param語句發送致命錯誤。它聲明「調用第35行的C:\ xampp \ htdocs \ 1102824H \ Assignment2 \ copyspeech.php中的非對象的成員函數bind_param()」。請幫忙。謝謝。bind_param語句錯誤

<?php 

    session_start(); 

    // default user's name 
    $user = ''; 

    // if visitor is logged in 
    $loggedIn = (!empty($_SESSION['user'])); 

    // since user is logged in, let us retrieve user's name from $_SESSION 
    if ($loggedIn) { 
     $user = $_SESSION['user']; 
    } else { 
     // we only allow logged in user to see this page 
     // if visitor not logged in, redirect visitor to login page 
     header('Location: index.php'); 
     exit; 
    } 

    $speechID = $_GET['id']; 

    // the file that contains your database credentials like username and password 
    require_once('config/database.php'); 

    // see Lecture Webp_Week13_14_Using_PHPandMySQL(updating).pptx Slide 4 aka Step 1 
    $mysqli = new mysqli($database_hostname, $database_username, $database_password, $database_name) or exit("Error connecting to database"); 

    // Slide 5 aka Step 2 
    $stmt = $mysqli->prepare("INSERT INTO assignment_speeches (id, subject, body, tags, image) 
           SELECT id, subject, body, tags, image 
           FROM assignment_speeches 
           WHERE id = ?"); 

    // Slide 6 aka Step 3 the bind params must correspond to the ? 
    $stmt->bind_param("i", $speechID); // 1 ? so we use i. we use i because id is INT 

    // Slide 7 aka Step 4 
    $successfullyCopied = $stmt->execute(); 

    // Slide 8 aka Step 5 
    // we won't check the delete result here. 

    // Slide 9 aka Step 6 and 7 
    $stmt->close(); 

    $mysqli->close(); 

    // if we successfully delete this, we 
    if ($successfullyCopied) { 
     $_SESSION['message'] = 'Successfully copied'; 
    } else { 
     $_SESSION['message'] = 'Unable to copy'; 
    } 

    header('Location: homepage.php'); 

?>

+1

您的準備失敗,請檢查'$ mysqli-> error' – DevZer0

+0

重複**相關**部分 – Gordon

+0

此問題似乎是無關緊要的,因爲OP沒有麻煩檢查相關問題和數十個重複項我們已經有了這個相同的錯誤。 – Gordon

回答

1

查詢的語法不正確。這就是爲什麼prepare()和隨後致電bind_param()失敗。在查詢

變化

SELECT (id, subject, body, tags, image) 

SELECT id, subject, body, tags, image 

UPDATE的SELECT條款刪除括號由於idauto_increment列,您還需要從列排除列表,讓MySQL的產生被複制行的新ID

INSERT INTO assignment_speeches (subject, body, tags, image) 
SELECT subject, body, tags, image 
    FROM assignment_speeches 
WHERE id = ? 
+0

嗨,謝謝你的回覆。但是,表中沒有記錄信息(assignment_speeches_copy)。我可以知道出了什麼問題嗎? – user2646948

+0

@ user2646948唯一的原因是你的'SELECT'不返回任何東西。爲了進一步幫助您,您需要顯示代碼的其他相關部分(例如,如何執行查詢)和數據。你有沒有嘗試直接在mysql客戶端執行你的選擇? – peterm

+0

我已編輯帖子。希望能幫助到你。我是PHP和MySQL的新手。需要幫助。謝謝。 – user2646948