-1

i want to add and display list of medicines we use php oop but it gives me 5 errors even the code works fine.

Notice: Undefined variable: array in D:\wamp\www\poocrud\action.php on line 41

Warning: array_keys() expects parameter 1 to be array, null given in D:\wamp\www\poocrud\action.php on line 9

Warning: implode(): Invalid arguments passed in D:\wamp\www\poocrud\action.php on line

Warning: array_values() expects parameter 1 to be array, null given in D:\wamp\www\poocrud\action.php on line 10

Warning: implode(): Invalid arguments passed in D:\wamp\www\poocrud\action.php on line 10 INSERT INTO medicines() VALUES ('') .

<?php

include "db.php";

class dataoperation extends Database
{
   public function insert($table,$fildes){
    //"INSERT INTO $table ($m_name,$qty) VALUES ('name','qty')";
    $sql = "INSERT INTO ".$table."(".implode(",", array_keys($fildes)).") VALUES
     ('".implode("','",array_values($fildes))."')";
     echo $sql;
     $query = mysqli_query($this->con,$sql);
      if($query){
     return true;
     }
   }

   public function getAll($table){
    $sql = "SELECT * FROM ".$table;
    $array = array();
    $query = mysqli_query($this->con,$sql);
    while($row = mysqli_fetch_assoc($query)){
        $array[] = $row ; 
    }
    return $array;

   }

}

$obj = new dataoperation;

if(isset($_POST["submit"])){
    $array = array(
        "m_name"=> $_POST["name"],
        "qty"=> $_POST["qty"],

    );

    }
    if($obj->insert("medicines",$array)){
    header("location:index.php?msg=inserted");
 }
?>
  • I thought what was posted here looked familiar to me. [You asked this already and under another account](https://stackoverflow.com/q/59591768/1415724). This should be flagged to @moderators as a sock puppet. [Other account](https://stackoverflow.com/users/12215944/hajar-lamine). I would have flagged it myself, but I'm still banned from doing that for now. – Funk Forty Niner Jan 04 '20 at 17:12
  • @FunkFortyNiner you win what ultimately, nothing, I don't know how can I use stackoverflow because I'm new here – Hajar Lamine Jan 04 '20 at 17:26

1 Answers1

-1

All of those errors would occur if there was no submit input in your post, making this line fail:

if(isset($_POST["submit"]))

In that scenario, the $array variable isn't defined, but you still try to use it in your insert. And then it causes all the rest of those errors.

jhilgeman
  • 1,543
  • 10
  • 27