Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > ช่วยดูให้หน่อยคับinsertข้อมูล3tableแล้วมันError ติดอยู่ตัวเดียว



 

ช่วยดูให้หน่อยคับinsertข้อมูล3tableแล้วมันError ติดอยู่ตัวเดียว

 



Topic : 136994



โพสกระทู้ ( 11 )
บทความ ( 0 )



สถานะออฟไลน์




โค้ดปกติมันเซฟได้เลยไม่มีปัญหาเลยคับ โดยผมเพิ่มคอลัม mix ใน tb orderitem1 ผมก็จะส่งตัว mix นี้บันทึกไปด้วย
แต่พอส่งข้อมูลขึ้น Error แบบนี้ครับ

Fatal error: Uncaught ArgumentCountError: The number of elements in the type definition string must match the number of bind variables in C:\xampp\htdocs\onlineShop\CartAction.php:120 Stack trace: #0 C:\xampp\htdocs\onlineShop\CartAction.php(120): mysqli_stmt->bind_param('ids', NULL, NULL, NULL, NULL) #1 {main} thrown in C:\xampp\htdocs\onlineShop\CartAction.php on line 120

ผมลองแก้ข้อมูลเอา $db_quantity ออก แล้วใส่ $db_mix ไปแทน แล้วส่งเข้า table orderitems1 คอลัม mix ค่ามันก็เข้าปกติคับ
แต่พอใส่ไปทั้งคู่ ก็เกิด error อย่างที่บอกไปด้านบนคับ ตอนแรกคิดว่าเป็นเพราะผมแก้โค้ดมันเลย Error แต่ผมลองเอาโค้ดเดิมๆ มาเพิ่ม
ตัว mix แล้วส่งไปก็ไม่ได้เหมือนเดิมคับ

Code (PHP)
CartAction.php
<?php 
// Include the database connection file 
require_once 'dbConnect.php'; 
 
// Initialize shopping cart class 
require_once 'Cart.class.php'; 
$cart = new Cart; 
 
// Default redirect page 
$redirectURL = 'index.php'; 
 
// Process request based on the specified action 
if(isset($_REQUEST['action']) && !empty($_REQUEST['action'])){ 
    if($_REQUEST['action'] == 'addToCart' && !empty($_REQUEST['id'])){ 
        $product_id = $_REQUEST['id']; 
 
        // Fetch product details from the database 
        $sqlQ = "SELECT * FROM products1 WHERE id=?"; 
        $stmt = $db->prepare($sqlQ); 
        $stmt->bind_param("i", $db_id); 
        $db_id = $product_id; 
        $stmt->execute(); 
        $result = $stmt->get_result(); 
        $productRow = $result->fetch_assoc(); 
 
        $itemData = array( 
            'id' => $productRow['id'], 
            'image' => $productRow['image'], 
            'name' => $productRow['name'], 
            'price' => $productRow['price'], 
            'qty' => 1,
            'mix' => 1 
        ); 
         
        // Insert item to cart 
        $insertItem = $cart->insert($itemData); 
         
        // Redirect to cart page 
        $redirectURL = $insertItem?'viewCart.php':'index.php'; 
    }elseif($_REQUEST['action'] == 'updateCartItem' && !empty($_REQUEST['id'])){ 
        // Update item data in cart 
        $itemData = array( 
            'rowid' => $_REQUEST['id'], 
            'qty' => $_REQUEST['qty'] 
        ); 
        $updateItem = $cart->update($itemData); 
         
        // Return status 
        echo $updateItem?'ok':'err';die; 
    }elseif($_REQUEST['action'] == 'removeCartItem' && !empty($_REQUEST['id'])){ 
        // Remove item from cart 
        $deleteItem = $cart->remove($_REQUEST['id']); 
         
        // Redirect to cart page 
        $redirectURL = 'viewCart.php'; 
    }elseif($_REQUEST['action'] == 'placeOrder' && $cart->total_items() > 0){ 
        $redirectURL = 'checkout.php'; 
         
        // Store post data 
        $_SESSION['postData'] = $_POST; 
     
        $first_name = strip_tags($_POST['first_name']); 
        $last_name = strip_tags($_POST['last_name']); 
        $email = strip_tags($_POST['email']); 
        $phone = strip_tags($_POST['phone']); 
        $address = strip_tags($_POST['address']); 
         
        $errorMsg = ''; 
        if(empty($first_name)){ 
            $errorMsg .= 'Please enter your first name.<br/>'; 
        } 
        if(empty($last_name)){ 
            $errorMsg .= 'Please enter your last name.<br/>'; 
        } 
        if(empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)){ 
            $errorMsg .= 'Please enter a valid email.<br/>'; 
        } 
        if(empty($phone)){ 
            $errorMsg .= 'Please enter your contact number.<br/>'; 
        } 
        if(empty($address)){ 
            $errorMsg .= 'Please enter your address.<br/>'; 
        } 
         
        if(empty($errorMsg)){ 
            // Insert customer data into the database 
            $sqlQ = "INSERT INTO customers1 (first_name,last_name,email,phone,address,created,modified) VALUES (?,?,?,?,?,NOW(),NOW())"; 
            $stmt = $db->prepare($sqlQ); 
            $stmt->bind_param("sssss", $db_first_name, $db_last_name, $db_email, $db_phone, $db_address); 
            $db_first_name = $first_name; 
            $db_last_name = $last_name; 
            $db_email = $email; 
            $db_phone = $phone; 
            $db_address = $address; 
            $insertCust = $stmt->execute(); 
             
            if($insertCust){ 
                $custID = $stmt->insert_id; 
                 
                // Insert order info in the database 
                $sqlQ = "INSERT INTO orders1 (customer_id,grand_total,created,status) VALUES (?,?,NOW(),?)"; 
                $stmt = $db->prepare($sqlQ); 
                $stmt->bind_param("ids", $db_customer_id, $db_grand_total, $db_status); 
                $db_customer_id = $custID; 
                $db_grand_total = $cart->total(); 
                $db_status = 'Pending'; 
                $insertOrder = $stmt->execute(); 
             
                if($insertOrder){ 
                    $orderID = $stmt->insert_id; 
                     
                    // Retrieve cart items 
                    $cartItems = $cart->contents(); 
                     
                     // Insert order items in the database 
                    if(!empty($cartItems)){ 
                        $sqlQ = "INSERT INTO order_items1 (order_id, product_id, quantity, mix) VALUES (?,?,?,?)"; 
                        $stmt = $db->prepare($sqlQ); 
                        foreach($cartItems as $item){ 
                            $stmt->bind_param("ids", $db_order_id, $db_product_id, $db_quantity, $db_mix); //มัน Error บรรทัดนี้คับ
                            $db_order_id = $orderID; 
                            $db_product_id = $item['id']; 
                            $db_quantity = $item['quantity'];
                            $db_mix = $item['mix']; // <- ตัวนี้ที่เพิ่มใหม่ ที่มีปัญหาครับ ส่งไม่ได้
                            $stmt->execute(); 
                        } 
                        // Remove all items from cart 
                        $cart->destroy(); 
                         
                        // Redirect to the status page 
                        $redirectURL = 'orderSuccess.php?id='.base64_encode($orderID); 
                    }else{ 
                        $sessData['status']['type'] = 'error'; 
                        $sessData['status']['msg'] = 'Something went wrong, please try again.'; 
                    } 
                }else{ 
                    $sessData['status']['type'] = 'error'; 
                    $sessData['status']['msg'] = 'Something went wrong, please try again.'; 
                } 
            }else{ 
                $sessData['status']['type'] = 'error'; 
                $sessData['status']['msg'] = 'Something went wrong, please try again.'; 
            } 
        }else{ 
            $sessData['status']['type'] = 'error'; 
            $sessData['status']['msg'] = '<p>Please fill all the mandatory fields.</p>'.$errorMsg;  
        } 
         
        // Store status in session 
        $_SESSION['sessData'] = $sessData; 
    } 
} 
 
// Redirect to the specific page 
header("Location: $redirectURL"); 
exit();


Cart.class.php
Code (PHP)
<?php 
// Start session 
if(!session_id()){ 
    session_start(); 
} 
 

class Cart { 
    protected $cart_contents = array(); 
     
    public function __construct(){ 
        // get the shopping cart array from the session 
        $this->cart_contents = !empty($_SESSION['cart_contents'])?$_SESSION['cart_contents']:NULL; 
        if ($this->cart_contents === NULL){ 
            // set some base values 
            $this->cart_contents = array('cart_total' => 0, 'total_items' => 0); 
        } 
    } 
     
    /** 
     * Cart Contents: Returns the entire cart array 
     * @param    bool 
     * @return    array 
     */ 
    public function contents(){ 
        // rearrange the newest first 
        $cart = array_reverse($this->cart_contents); 
 
        // remove these so they don't create a problem when showing the cart table 
        unset($cart['total_items']); 
        unset($cart['cart_total']); 
 
        return $cart; 
    } 
     
    /** 
     * Get cart item: Returns a specific cart item details 
     * @param    string    $row_id 
     * @return    array 
     */ 
    public function get_item($row_id){ 
        return (in_array($row_id, array('total_items', 'cart_total'), TRUE) OR ! isset($this->cart_contents[$row_id])) 
            ? FALSE 
            : $this->cart_contents[$row_id]; 
    } 
     
    /** 
     * Total Items: Returns the total item count 
     * @return    int 
     */ 
    public function total_items(){ 
        return $this->cart_contents['total_items']; 
    } 
     
    /** 
     * Cart Total: Returns the total price 
     * @return    int 
     */ 
    public function total(){ 
        return $this->cart_contents['cart_total']; 
    } 
     
    /** 
     * Insert items into the cart and save it to the session 
     * @param    array 
     * @return    bool 
     */ 
    public function insert($item = array()){ 
        if(!is_array($item) OR count($item) === 0){ 
            return FALSE; 
        }else{ 
            if(!isset($item['id'], $item['name'], $item['price'], $item['qty'])){ 
                return FALSE; 
            }else{ 
                /* 
                 * Insert Item 
                 */ 
                // prep the quantity 
                $item['qty'] = (float) $item['qty']; 
                if($item['qty'] == 0){ 
                    return FALSE; 
                } 
                // prep the price 
                $item['price'] = (float) $item['price']; 
                // create a unique identifier for the item being inserted into the cart 
                $rowid = md5($item['id']); 
                // get quantity if it's already there and add it on 
                $old_qty = isset($this->cart_contents[$rowid]['qty']) ? (int) $this->cart_contents[$rowid]['qty'] : 0; 
                // re-create the entry with unique identifier and updated quantity 
                $item['rowid'] = $rowid; 
                $item['qty'] += $old_qty; 
                $this->cart_contents[$rowid] = $item; 
                 
                // save Cart Item 
                if($this->save_cart()){ 
                    return isset($rowid) ? $rowid : TRUE; 
                }else{ 
                    return FALSE; 
                } 
            } 
        } 
    } 
     
    /** 
     * Update the cart 
     * @param    array 
     * @return    bool 
     */ 
    public function update($item = array()){ 
        if (!is_array($item) OR count($item) === 0){ 
            return FALSE; 
        }else{ 
            if (!isset($item['rowid'], $this->cart_contents[$item['rowid']])){ 
                return FALSE; 
            }else{ 
                // prep the quantity 
                if(isset($item['qty'])){ 
                    $item['qty'] = (float) $item['qty']; 
                    // remove the item from the cart, if quantity is zero 
                    if ($item['qty'] == 0){ 
                        unset($this->cart_contents[$item['rowid']]); 
                        return TRUE; 
                    } 
                } 
                 
                // find updatable keys 
                $keys = array_intersect(array_keys($this->cart_contents[$item['rowid']]), array_keys($item)); 
                // prep the price 
                if(isset($item['price'])){ 
                    $item['price'] = (float) $item['price']; 
                } 
                // product id & name shouldn't be changed 
                foreach(array_diff($keys, array('id', 'name')) as $key){ 
                    $this->cart_contents[$item['rowid']][$key] = $item[$key]; 
                } 
                // save cart data 
                $this->save_cart(); 
                return TRUE; 
            } 
        } 
    } 
     
    /** 
     * Save the cart array to the session 
     * @return    bool 
     */ 
    protected function save_cart(){ 
        $this->cart_contents['total_items'] = $this->cart_contents['cart_total'] = 0; 
        foreach ($this->cart_contents as $key => $val){ 
            // make sure the array contains the proper indexes 
            if(!is_array($val) OR !isset($val['price'], $val['qty'])){ 
                continue; 
            } 
      
            $this->cart_contents['cart_total'] += ($val['price'] * $val['qty']); 
            $this->cart_contents['total_items'] += $val['qty']; 
            $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']); 
        } 
         
        // if cart empty, delete it from the session 
        if(count($this->cart_contents) <= 2){ 
            unset($_SESSION['cart_contents']); 
            return FALSE; 
        }else{ 
            $_SESSION['cart_contents'] = $this->cart_contents; 
            return TRUE; 
        } 
    } 
     
    /** 
     * Remove Item: Removes an item from the cart 
     * @param    int 
     * @return    bool 
     */ 
     public function remove($row_id){ 
        // unset & save 
        unset($this->cart_contents[$row_id]); 
        $this->save_cart(); 
        return TRUE; 
     } 
      
    /** 
     * Destroy the cart: Empties the cart and destroy the session 
     * @return    void 
     */ 
    public function destroy(){ 
        $this->cart_contents = array('cart_total' => 0, 'total_items' => 0); 
        unset($_SESSION['cart_contents']); 
    } 
}




Tag : PHP, MySQL









ประวัติการแก้ไข
2023-04-14 14:29:13
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2023-04-14 14:28:24 By : tomtourism3 View : 301 Reply : 2
 

 

No. 1



โพสกระทู้ ( 3,844 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

สถานะออฟไลน์
Twitter Facebook

มันก็บอกตรงตัวนะครับ ว่า type ผิด
"Uncaught ArgumentCountError: The number of elements in the type definition string must match the number of bind variables"






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-04-18 16:49:28 By : mongkon.k
 


 

No. 2



โพสกระทู้ ( 4,720 )
บทความ ( 8 )



สถานะออฟไลน์


https://www.php.net/manual/en/mysqli-stmt.bind-param.php
ค่าแรกเป็น types ต้องใส่ให้ครบตามจำนวน vars และต้องถูกประเภทด้วย
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-04-19 15:00:49 By : mr.v
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ช่วยดูให้หน่อยคับinsertข้อมูล3tableแล้วมันError ติดอยู่ตัวเดียว
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่