Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

391
Visualizações
Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp64\www\OBJEPRO\dbcrud.php on line 10

Im creating a Simple CRUD using OOP, PHP but I get errors when I run add.php and the update and delete file. The same error in every file (add.php, update.php, delete.php)

Here are the errors:

Notice: Undefined index: Supplier_ID in C:\wamp64\www\OBJEPRO\add.php on line 12

Notice: Undefined index: Company_Name in C:\wamp64\www\OBJEPRO\add.php on line 12

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp64\www\OBJEPRO\dbcrud.php on line 10

Here's the code in add.php

<?php
require_once("header.php");
require_once("dbcrud.php");
if (isset($_POST['submit'])) {



}
    $dbcrud=new dbcrud;


    $result=$dbcrud->create($_POST['Supplier_ID'],$_POST['Company_Name']);
        if($result){
            echo "<br />";
            echo "Successfully added.";
        }else{
            echo "<br />";
            echo "Error!";
        }

?>
<p><h3>Add Supplier:</h3></p>
<form action="" method="POST">
<p>Supplier ID: <input type="text" name="Supplier_ID"></p>
<p>Company Name: <input type="text" name="Company_Name"></p>
<p><input type="submit" name="submit" value="Save"></p>
</form>

And here's the code in dbcrud.php:

<?php
require_once("dbconn.php");

class dbcrud {

        public function __construct(){
        $dbconn=new dbconn;
    }
    public function create($Supplier_ID, $Company_Name){                        
        $result=mysqli_query("INSERT INTO suppliers(Supplier_ID, Company_Name) VALUES('$Supplier_ID','$Company_Name')");
        return $result;
    }

    public function read(){
        $result=mysqli_query("SELECT * suppliers");
        return $result;
    }
    public function update($Supplier_ID, $Company_Name){
        $result=mysqli_query("UPDATE suppliers SET Company_name='{$Company_Name}' WHERE Supplier_ID={$Supplier_ID}'");
        return $result;

    }
    public function delete($Supplier_ID){
        $result=mysqli_query("DELETE FROM suppliers WHERE Supplier_ID={$Supplier_ID}");
        return $result;
    }


}

?>

Can anyone help me?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The documentation of mysqli_query() clearly states the two alternatives how use that method. You mix both, which won't work.

Ignoring quite a number of other issues I dare say this modification of your class will bring you closer to what you are looking for. It stores the database connection object inside a class property so that it can be reused for the actual queries.

<?php
require_once("dbconn.php");

class dbcrud {
    private $dbconn;

    public function __construct(){
        $this->dbconn = new dbconn;
    }
    public function create($Supplier_ID, $Company_Name){                        
        $result = mysqli_query(
            $this->dbconn, 
            "INSERT INTO suppliers(Supplier_ID, Company_Name) VALUES('$Supplier_ID','$Company_Name')"
        );
        return $result;
    }

    public function read(){
        $result = mysqli_query(
            $this->dbconn, 
            "SELECT * FROM suppliers"
        );
        return $result;
    }
    public function update($Supplier_ID, $Company_Name){
        $result = mysqli_query(
            $this->dbconn, 
            "UPDATE suppliers SET Company_name='{$Company_Name}' WHERE Supplier_ID={$Supplier_ID}'"
        );
        return $result;

    }
    public function delete($Supplier_ID){
        $result = mysqli_query(
            $this->dbconn, 
            "DELETE FROM suppliers WHERE Supplier_ID={$Supplier_ID}"
        );
        return $result;
    }


}

However this is only part of a solution. You really need to work through some tutorials to learn how to code database stuff. For one there are huge security implications in your code. You need to learn about the benefits of using "prepared statements" in combination with "parameter binding" to prevent sql injection vulnerability.

So take this as a starting point, lot's of other issues remain unanswered...

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda