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?
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...