【问题标题】:PHP and MySQLi CRUD with LEFT JOIN tables带有 LEFT JOIN 表的 PHP 和 MySQLi CRUD
【发布时间】:2013-12-28 14:25:23
【问题描述】:

有没有一种方法可以使用 LEFT JOIN 表创建 PHP 和 MySQLi CRUD。目前,我有 4 张桌子——费用、付款、班级和学生。用户应该能够创建、编辑和删除支付表中的数据。但是,付款的行是有限的 - (payments_id、student_id、payment_date、payment_amount 和 fee_id)。因此,我希望用户能够选择用户名而不是 student_id。

目前我修改了php代码,遇到了几个错误。

var $query3 = 'SELECT students.student_id, fee.fee_id, fee.fee_description, fee.class_id, fee.fee_amount, 
                    students.firstname, students.lastname, payments.payment_id, payments.payment_date, 
                    class.class_description, payments.payment_amount, payments.payment_date
                    FROM students
                    LEFT JOIN class ON students.class_id = class.class_id
                    LEFT JOIN fee ON fee.class_id = class.class_id
                    LEFT JOIN payments ON payments.student_id = students.student_id
                    AND Payments.fee_id = fee.fee_id';

    public function createPayments($item) {

        $stmt = mysqli_prepare($this->connection,  "INSERT INTO $this->query3 (
                                                    student_id, fee_id, fee_description, class_id, fee_amount,
                                                    firstname, lastname, payment_id, payment_date,
                                                    class_description, payment_amount, payment_date)
                                                    VALUES (?, ?, ?, ?, ?, ?,?,?,?,?,?,?)");
        $this->throwExceptionOnError();

        mysqli_stmt_bind_param($stmt, 'issi', $item->student_id, $item->fee_id, $item-> fee_description, $item->class_id, $item->fee_amount,
                                $item->firstname, $item->lastname, $item->payment_id, $item->payment_date->toString('YYYY-MM-dd HH:mm:ss'), 
                                $item->class_description, $item->payment_amount, $item->payment_date);
        $this->throwExceptionOnError();

        mysqli_stmt_execute($stmt);     
        $this->throwExceptionOnError();

        $autoid = mysqli_stmt_insert_id($stmt);

        mysqli_stmt_free_result($stmt);     
        mysqli_close($this->connection);

        return $autoid;
    }

    /**
     * Updates the passed item in the table.
     *
     * Add authorization or any logical checks for secure access to your data 
     *
     * @param stdClass $item
     * @return void
     */
    public function updatePayments($item) {

        $stmt = mysqli_prepare($this->connection, "UPDATE $this->query3 SET student_id=?, fee_id=?, fee_description=?, class_id=?, fee_amount=?,
                                                    firstname=?, lastname=?, payment_date=?, class_description=?, payment_amount=?,
                                                    payment_date=? where payment_id=?");        
        $this->throwExceptionOnError();

        mysqli_stmt_bind_param($stmt, 'issii',  $item->student_id, $item->fee_id, $item-> fee_description, $item->class_id, $item->fee_amount,
                                $item->firstname, $item->lastname, $item->payment_id, $item->payment_date->toString('YYYY-MM-dd HH:mm:ss'), 
                                $item->class_description, $item->payment_amount, $item->payment_date);
        $this->throwExceptionOnError();

        mysqli_stmt_execute($stmt);     
        $this->throwExceptionOnError();

        mysqli_stmt_free_result($stmt);     
        mysqli_close($this->connection);
    }

    /**
     * Deletes the item corresponding to the passed primary key value from 
     * the table.
     *
     * Add authorization or any logical checks for secure access to your data 
     *
     * 
     * @return void
     */
    public function deletePayments($itemID) {

        $stmt = mysqli_prepare($this->connection, "DELETE FROM $this->query3 WHERE payment_id = ?");
        $this->throwExceptionOnError();

        mysqli_stmt_bind_param($stmt, 'i', $itemID);
        mysqli_stmt_execute($stmt);
        $this->throwExceptionOnError();

        mysqli_stmt_free_result($stmt);     
        mysqli_close($this->connection);
    }

错误代码

There was an error while invoking the operation. Check your server settings and try invoking the operation again. 

Reason: Server error MySQL Error - 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT students.student_id, fee.fee_id, fee.fee_description, fee.class_id, fee.f' at line 1 #0 C:\wamp\www\feez3\services\PaymentsService2.php(220): PaymentsService2->throwExceptionOnError() #1 [internal function]: PaymentsService2->deletePayments('Enter Value') #2 [internal function]: ReflectionMethod->invokeArgs(Object(PaymentsService2), Array) #3 C:\wamp\www\ZendFramework\library\Zend\Server\Reflection\Function\Abstract.php(380): call_user_func_array(Array, Array) #4 C:\wamp\www\ZendFramework\library\Zend\Amf\Server.php(359): Zend_Server_Reflection_Function_Abstract->__call('invokeArgs', Array) #5 C:\wamp\www\ZendFramework\library\Zend\Amf\Server.php(359): Zend_Server_Reflection_Method->invokeArgs(Object(PaymentsService2), Array) #6 C:\wamp\www\ZendFramework\library\Zend\Amf\Server.php(553): Zend_Amf_Server->_dispatch('deletePayments', Array, 'PaymentsService...') #7 C:\wamp\www\ZendFramework\library\Zend\Amf\Server.php(629): Zend_Amf_Server->_handle(Object(Zend_Amf_Request_Http)) #8 C:\wamp\www\feez3\gateway.php(69): Zend_Amf_Server->handle() #9 {main}

谁能指导我完成这个?先感谢您。

【问题讨论】:

  • AND Payments.fee_id = fee.fee_id 不应该是付款。
  • 数据库和表名在 Windows 中不区分大小写,在大多数 Unix 中区分大小写。无论如何,我喜欢复杂查询的简短而有意义的别名。

标签: php mysqli crud


【解决方案1】:

我没有看到查询有什么问题,所以我只是按照我的编写方式编写了查询。不妨试试这个。

"SELECT   s.student_id, 
          s.firstname,
          s.lastname,
          f.fee_id, 
          f.fee_description, 
          f.class_id, 
          f.fee_amount, 
          p.payment_id, 
          p.payment_date, 
          p.payment_amount, 
          p.payment_date,
          c.class_description
FROM      students s 
LEFT JOIN class c ON (s.class_id = c.class_id)
LEFT JOIN fee f ON (c.class_id = f.class_id)
LEFT JOIN payments p ON (s.student_id = p.student_id)  
AND       p.fee_id = f.fee_id";     

用双引号括起来... 祝你好运! :-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    • 2011-05-26
    • 2015-08-26
    • 2012-07-28
    • 2016-05-01
    • 2013-03-13
    • 1970-01-01
    相关资源
    最近更新 更多