bind_param语句错误

时间:2013-08-05 06:22:22

标签: php mysql

我的代码有什么问题吗?我一直在为bind_param句子犯下致命的错误。它声明“在第35行的C:\ xampp \ htdocs \ 1102824H \ Assignment2 \ copyspeech.php中的非对象上调用成员函数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');

&GT;

1 个答案:

答案 0 :(得分:1)

您的查询语法不正确。这就是为什么prepare()和随后对bind_param()的调用失败的原因。删除查询的SELECT子句中的括号

更改

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

SELECT id, subject, body, tags, image

更新由于idauto_increment列,因此您还需要将其从列列表中排除,以便让mysql为正在复制的行生成新ID

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