当前位置: 首页 > 知识库问答 >
问题:

带左联接表的PHP和MySQLi CRUD

岳浩
2023-03-14

有没有一种方法,我可以创建PHP和MySQLi CRUD与左联接表。目前,我有4个表-费用,付款,班级和学生。用户应该能够创建、编辑和删除付款表中的数据。但是,付款的行是有限的-(payments_id,student_id,payment_date,payment_amount和fee_id)。因此,我希望用户能够选择user firstname而不是student_id。

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}

共有1个答案

成浩漫
2023-03-14

我看不出查询有什么问题,所以我只是按照我会写的方式编写查询。也许可以试一试。

"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";     

使用双引号...祝你好运!:-)

 类似资料:
  • 问题内容: 这是我的namedquery: @NamedQuery(name =“ User.findOneWithLists”,查询=“从用户u中选择u” +“左联接FEACH u.aTemplates” +“左联接FE.bTemplates” +“左联接FE.bp” +“左JOIN FETCH u.aCredentials“ +” LEFT JOIN FETCH u.st(st.deleted

  • 问题内容: 我有三个表: 命令 OrderId,int PK CustomerId,与客户之间为int FK,允许为NULL 顾客 CustomerId,int PK CompanyId,将FK转换为Company,不允许为NULL 公司介绍 CompanyId,int PK 名称nvarchar(50) 我想选择所有订单,无论是否有客户,如果有客户,也要选择客户的公司名称。 如果我使用此查询…

  • 问题内容: 我已经搜索过,但没有找到明确的答案。以下哪一项对SQL Server的性能更好: 或者… 我尝试过同时运行这两个程序,但是很难确定。我会很高兴地解释一个为什么比另一个更快,或者这取决于情况。 问题答案: 您的两个查询不做相同的事情。特别是,如果两个表中的值重复,则第一个将返回重复的行。 如果要在其他两个表中的任何一个中查找行,我建议使用: 并且,在和中都创建索引。 您的原始查询哪个更快

  • 问题内容: 根据我对左外部联接的了解,结果表的行数永远不应超过左表的行数…如果这是错误的,请让我知道… 我的左表是192572行和8列。 我的右边表格是42160行和5列。 我的左表有一个名为“ id”的字段,该字段与我的右表中名为“键”的一列匹配。 因此,我将它们合并为: 但是组合的形状是236569。 我有什么误会? 问题答案: 如果键与另一个DataFrame中的多个行匹配,则可以预期这种情

  • 问题内容: 这个问题已经在这里有了答案 : SQL中左右联结与左右联结之间的区别[重复] (4个答案) 6年前关闭。 我看到过称为LEFT OUTER JOIN或RIGHT OUTER JOIN的联接。在某些地方,我见过LEFT JOIN或RIGHT JOIN。我对此感到困惑。 我两天前发布了一个问题,但我无法理解解决方案提供的链接。 这些连接类型是否相同,或者两者之间有区别? 问题答案: 两者之

  • 问题内容: 我的查询有问题,该查询显示商店列表以及与之关联的产品数量。我已经玩了很长时间的左联接等,但无济于事。这些表具有以下结构: 商店含有表列:, 产品含表列:,,, 查询如下: 我没有去买有0件商品的商店。请问我该如何实现? 基础数据库是MySQL。 谢谢!马耳他 问题答案: 您需要在左侧购物,因为右侧可能没有数据,在本例中为PRODUCT。 不仅如此,您还需要WHERE条件作为LEFT-J