Why doesn't it want to add a category?

2019-09-20 03:25发布

问题:

i am making a cms for a school assiment but when i try to create a new category it keeps giving me errors: google chrome gives me this:

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in Warning: Missing argument 3 for addCat(), called in C:\xampp\htdocs\portfoliojbehrens\admin\doAddc.php on line 6 and defined in C:\xampp\htdocs\portfoliojbehrens\includes\functions.php on line 254

Notice: Undefined variable: cDesc in C:\xampp\htdocs\portfoliojbehrens\includes\functions.php on line 255

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\portfoliojbehrens\includes\functions.php on line 256

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\portfoliojbehrens\includes\functions.php:254) in C:\xampp\htdocs\portfoliojbehrens\admin\doAddc.php on line 7

here is my code: from the function page:

function addCat($mysqli, $cName, $cDesc){
    $query = "INSERT INTO categories (title, description) VALUES ('$cName', '$cDesc')";
    mysqli_query($mysqli, $query);
}

from the page that shout check the inputs:

<?php
include '../includes/functions.php';
sec_session_start();
if(isset($_POST['submit'])){
    if(isset($_POST['CatName'])){
        addCat($_POST['CatName'],$_POST['CatDesc']);
        header("Location: categories.php");
    } else{
        echo"please set a category name!";
        include('addCat.php');
    }
}else{
    header("Location: addCat.php");
}
 ?>

and finally the input:

<form action="doAddc.php" method="post">
   <label for="CatName">Name</label>
   <input type="text" name="CatName" id="CatName"/>
   <label for="CatDesc">Description</label>
   <input type="text" name="CatDesc" id="CatDesc"/>
   <input type="submit" name="submit" value="submit"/>
</form>

hope some of u know what i am doing wrong here

回答1:

You need to pass through your mysql database connection to the addCat function as the first parameter.



标签: php mysqli