duandeng1824 2013-08-05 06:22
浏览 97
已采纳

bind_param语句错误

Is there anything wrong with my code? I have been getting fatal error for the bind_param sentence. It states "Call to a member function bind_param() on a non-object in C:\xampp\htdocs\1102824H\Assignment2\copyspeech.php on line 35". Please help. Thanks.

<?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条回答 默认 最新

  • dongwupei7803 2013-08-05 06:27
    关注

    The syntax of your query is incorrect. That's why prepare() and subsequent call to bind_param() fail. Remove parentheses in SELECT clause of your query

    Change

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

    to

    SELECT id, subject, body, tags, image
    

    UPDATE Since id is an auto_increment column you also need to exclude it from columns list to let mysql generate a new id for a row being copied

    INSERT INTO assignment_speeches (subject, body, tags, image) 
    SELECT subject, body, tags, image 
      FROM assignment_speeches 
     WHERE id = ?
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大