-2

私のコードに何か問題がありますか? bind_param 文で致命的なエラーが発生しています。「C:\xampp\htdocs\1102824H\Assignment2\copyspeech.php の 35 行目の非オブジェクトに対するメンバ関数 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');

?>

4

1 に答える 1

1

クエリの構文が正しくありません。そのためprepare()、その後の呼び出しはbind_param()失敗します。SELECTクエリの句で括弧を削除します

変化する

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

SELECT id, subject, body, tags, image

UPDATEidは列であるためauto_increment、mysql がコピーされる行の新しい ID を生成できるように、列リストから除外する必要もあります

INSERT INTO assignment_speeches (subject, body, tags, image) 
SELECT subject, body, tags, image 
  FROM assignment_speeches 
 WHERE id = ?
于 2013-08-05T06:27:57.137 に答える