1

this is the error:

Warning: mysqli_query() expects at least 2 parameters, 1 given in >C:\xampp\htdocs\ajax_cart\dbcontroller.php on line 25

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null >given in C:\xampp\htdocs\ajax_cart\dbcontroller.php on line 26

and this is the code:

function construct() {
    $conn = $this->connectDB();
    if(!empty($conn)) {
        $this->selectDB($conn);
    }
}

function connectDB() {
    $conn = mysqli_connect($this->host,$this->user,$this->password);
    return $conn;
}

function selectDB($conn) {
    mysqli_select_db($this->database,$conn);
}

function runQuery($query) {
    $result = mysqli_query($query);
    while($row=mysqli_fetch_assoc($result)) {
        $resultset[] = $row;
    }       
    if(!empty($resultset))
        return $resultset;
}

function numRows($query) {
    $result  = mysqli_query($query);
    $rowcount = mysqli_num_rows($result);
    return $rowcount;   
}
}
?>

this is the index file:

<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
?>
<HTML>
<HEAD>
<TITLE>PHP Shopping Cart with jQuery AJAX</TITLE>
<link href="style.css" type="text/css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script>
function showEditBox(editobj,id) {
    $('#frmAdd').hide();
    $(editobj).prop('disabled','true');
    var currentMessage = $("#message_" + id + " .message-content").html();
    var editMarkUp = '<textarea rows="5" cols="80" id="txtmessage_'+id+'">'+currentMessage+'</textarea><button name="ok" onClick="callCrudAction(\'edit\','+id+')">Save</button><button name="cancel" onClick="cancelEdit(\''+currentMessage+'\','+id+')">Cancel</button>';
    $("#message_" + id + " .message-content").html(editMarkUp);
}
function cancelEdit(message,id) {
    $("#message_" + id + " .message-content").html(message);
    $('#frmAdd').show();
}
function cartAction(action,product_code) {
    var queryString = "";
    if(action != "") {
        switch(action) {
            case "add":
                queryString = 'action='+action+'&code='+ product_code+'&quantity='+$("#qty_"+product_code).val();
            break;
            case "remove":
                queryString = 'action='+action+'&code='+ product_code;
            break;
            case "empty":
                queryString = 'action='+action;
            break;
        }    
    }
    jQuery.ajax({
    url: "ajax_action.php",
    data:queryString,
    type: "POST",
    success:function(data){
        $("#cart-item").html(data);
        if(action != "") {
            switch(action) {
                case "add":
                    $("#add_"+product_code).hide();
                    $("#added_"+product_code).show();
                break;
                case "remove":
                    $("#add_"+product_code).show();
                    $("#added_"+product_code).hide();
                break;
                case "empty":
                    $(".btnAddAction").show();
                    $(".btnAdded").hide();
                break;
            }    
        }
    },
    error:function (){}
    });
}
</script>
</HEAD>
<BODY>
<div id="product-grid">
    <div class="txt-heading">Products</div>
    <?php
    $product_array = $db_handle->runQuery("SELECT * FROM tblproduct ORDER BY id ASC");
    if (!empty($product_array)) { 
        foreach($product_array as $key=>$value){
    ?>
        <div class="product-item">
            <form id="frmCart">
            <div class="product-image"><img src="<?php echo $product_array[$key]["image"]; ?>"></div>
            <div><strong><?php echo $product_array[$key]["name"]; ?></strong></div>
            <div class="product-price"><?php echo "$".$product_array[$key]["price"]; ?></div>
            <div><input type="text" id="qty_<?php echo $product_array[$key]["code"]; ?>" name="quantity" value="1" size="2" />
            <?php
                $in_session = "0";
                if(!empty($_SESSION["cart_item"])) {
                    $session_code_array = array_keys($_SESSION["cart_item"]);
                    if(in_array($product_array[$key]["code"],$session_code_array)) {
                        $in_session = "1";
                    }
                }
            ?>
            <input type="button" id="add_<?php echo $product_array[$key]["code"]; ?>" value="Add to cart" class="btnAddAction cart-action" onClick = "cartAction('add','<?php echo $product_array[$key]["code"]; ?>')" <?php if($in_session != "0") { ?>style="display:none" <?php } ?> />
            <input type="button" id="added_<?php echo $product_array[$key]["code"]; ?>" value="Added" class="btnAdded" <?php if($in_session != "1") { ?>style="display:none" <?php } ?> />
            </div>
            </form>
        </div>
    <?php
            }
    }
    ?>
</div>
<div class="clear-float"></div>
<div id="shopping-cart">
<div class="txt-heading">Shopping Cart <a id="btnEmpty" class="cart-action" onClick="cartAction('empty','');">Empty Cart</a></div>
<div id="cart-item"></div>
</div>
<script>
$(document).ready(function () {
    cartAction('','');
})
</script>


</BODY>
</HTML>

i don't know what is the other parameter..what could i be missing?

silver01
  • 63
  • 2
  • 11
  • When in doubt check the manual... http://php.net/manual/en/mysqli.query.php `runQuery` also seems like it'll be susceptible to SQL injections, without actual SQL can't say for sure. Parameterized queries you should be using. – chris85 Jan 12 '17 at 04:59
  • Pass the `$conn` connection variable. – Atul Sharma Jan 12 '17 at 05:03
  • when i add $conn, mysqli_query($conn, $query) in line 25 it says: Notice: Undefined variable: conn in C:\xampp\htdocs\ajax_cart\dbcontroller.php on line 25 – silver01 Jan 12 '17 at 05:10
  • It seems these are the function of a class. then you should use $this->conn instead of $conn. I think it will solve your issue. Right now its not considering $conn connection variable. – Naveed Ramzan Jan 12 '17 at 05:14
  • $result = mysqli_query($this->conn, $query); it says: Notice: Undefined property: DBController::$conn in C:\xampp\htdocs\ajax_cart\dbcontroller.php on line 25 – silver01 Jan 12 '17 at 05:19

2 Answers2

1

You need to pass $conn variable to mysqli_query($query); Following is the example that you need to follow.

mysqli_query($conn,"SELECT * FROM table");

Following that method your new code should be(for two function only, rest of the code will remain same)

function runQuery($query) {
    $conn = connectDB(); // if you are using within class then $this->connectDB();
    $result = mysqli_query($conn,$query);
    if($result)
    {
    if(mysqli_num_rows($result) > 0)
    {
    while($row=mysqli_fetch_assoc($result)) {
        $resultset[] = $row;
    }       
    if(!empty($resultset))
       {
        return $resultset;
       }
   }
   }
}

function numRows($query) {
    $conn = connectDB(); // if you are using within class then $this->connectDB();
    $result  = mysqli_query($query);
    $rowcount = mysqli_num_rows($result);
    return $rowcount;   
}
  • i tried what you've said but... there's warning: Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\ajax_cart\dbcontroller.php on line 27 – silver01 Jan 12 '17 at 05:33
  • Actually your query is executed, but comes with 0 number of records, that's why you are getting this warning solution is if(mysqli_num_rows($result) > 0) { while($row=mysqli_fetch_assoc($result)) { $resultset[] = $row; } if(!empty($resultset)) return $resultset; } – Mudasir Nazir Jan 12 '17 at 05:36
  • function runQuery($query) { $conn = $this->connectDB(); // if you are using within class then $this->connectDB(); $result = mysqli_query($conn,$query); if(mysqli_num_rows($result) > 0) { while($row=mysqli_fetch_assoc($result)) { $resultset[] = $row; } if(!empty($resultset)) return $resultset; } } ..............not working, it says: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\ajax_cart\dbcontroller.php on line 27 – silver01 Jan 12 '17 at 05:49
  • can you please let me see the $query string. There might be issue with you mysql Query. – Mudasir Nazir Jan 12 '17 at 05:51
  • this is the query in the index file "SELECT * FROM tblproduct ORDER BY id ASC" ........look in main post for full code of index – silver01 Jan 12 '17 at 05:56
  • I have applied some changes in my code, can you please do have look again. There is a check that will check whether query is executed successfully or not. – Mudasir Nazir Jan 12 '17 at 05:57
  • no errors now, but the items in my tblproduct doesn't show up..there is 3 items in the table – silver01 Jan 12 '17 at 06:04
  • Please look at the code again. i have applied another change in connectDB() function, add database as last parameter. $conn = mysqli_connect($this->host,$this->user,$this->password,$this->database); – Mudasir Nazir Jan 12 '17 at 06:09
  • oh my god, it works now :) super thanks man – silver01 Jan 12 '17 at 06:16
0

When you do procedural style, you need to provide a db and the query. If you use Object Oriented approach, you only need the query.

So either do:

$conn->query;

or

mysqli_query($conn, $query);

Link to documentation

BizzyBob
  • 12,309
  • 4
  • 27
  • 51