0

I'm a beginner in PHP OOP. I know about basic OOP using PHP. Now I wanted to create a simple form submit code using OOP. I want to save a name in database. But I don't know what to write for first parameter in mysqli_query

Here's my code:

<?php
class dbConnect{
    private $host="localhost";
    private $user="root";
    private $pass="";
    private $db="oodb";
    public function Connect(){
        $conn=mysqli_connect($this->host, $this->user, $this->pass, $this->db);
        if($conn){
            echo "Connected";
        }
    }
}
$dbConn=new dbConnect();
$dbConn->Connect();
class Signup{
    public function Insert($value){
        if(isset($_REQUEST["sub"])){
            $query="INSERT INTO signup(name) VALUES('$value')";
            //FOLLING STATEMENT IS INCORRECT
            $queryrun=mysqli_query($dbConn->Connect(), $query);
            echo "Inserted";
        }
        else{
            echo "Error";
        }
    }
}
$sign=new Signup();
if(isset($_POST['id'])){
$sign->Insert($_POST['id']);
}
?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form name="form1" method="post">
    <input type="text" name="id">
    <input type="submit" name="sub">
</form>
</body>
</html>

Also, could you please tell me if I'm using OOP in a right way not? I think there are many mistakes.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

3 Answers3

2

In order to understand what the parameter should be like, you need to be aware of how prepared statements work. Please, read the linked article.

Here's a quick example:

$db = 'oodb';
$host = 'localhost';
$username = 'root';
$password = '';

$dsn = sprintf('mysql:dbname=%s;host=%s', $db, $host);
$pdo = new PDO($dsn, $username, $password);

$statement = $pdo->prepare('INSERT INTO signup(name) VALUES(:name)');

$statement->execute(array(
    'name' => $name,
));

Here's your code adapted to run using this simple example:

<?php

// configuration parameters
$db = 'oodb';
$host = 'localhost';
$username = 'root';
$password = '';
$dsn = sprintf('mysql:dbname=%s;host=%s', $db, $host);


// classes
class SignUp
{
    /**
     * @var PDO
     */
    protected $pdo;

    public function __construct(PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    public function insert($name)
    {
        $statement = $pdo->prepare('INSERT INTO signup(name) VALUES(:name)');

        $statement->execute(array(
            'name' => $name,
        ));
    }
}

// actual program flow
$pdo = new PDO($dsn, $username, $password);
$signUp = new SignUp($pdo);

if (array_key_exists('id', $_POST)) {
    $signUp->insert($_POST['id']);

    echo 'inserted';
    exit;
}
?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form name="form1" method="post">
    <input type="text" name="id">
    <input type="submit" name="sub">
</form>
</body>
</html>

As for your question whether you are doing PHP OOP the right way, my answer would be a solid NO.

OOP stands for Object-Oriented Programming. It is about different objects interacting with one another. The code you have done has one class that wraps a function. That's a class, not OOP.

Getting into OOP can get frustrating at first, so buckle up and be sure to read more on the topic. Remember that no one is born with experience.

Nikola Petkanski
  • 4,724
  • 1
  • 33
  • 41
  • 1
    Noble and all, but this has nothing to do with the question. This, as per your original post http://stackoverflow.com/revisions/39982962/1 – Funk Forty Niner Oct 11 '16 at 17:19
  • The question is `How to run MYSQL query using OOP in PHP?` and this is what I am answering to. – Nikola Petkanski Oct 11 '16 at 17:24
  • the error with the OP's code is undefined variable. Your answer does not fix their code or provide them with a solution. All this are really suggestions/comments, IMHO. – Funk Forty Niner Oct 11 '16 at 17:24
  • I understand wanna-be programmers might not be able to understand the simple example, so I have added full example. Hopefully that works for you. Don't forget to read about OOP and prepared statements and be sure to vote up all helpful answers. – Nikola Petkanski Oct 11 '16 at 17:32
  • Q) How to do OOP A) you need to be aware of how prepared statements work. huh wat? – PeeHaa Oct 11 '16 at 17:34
  • It's mine though :P I do agree with most of it, but the first paragraph and the code snippet don't relate to either OOP or the actual question. – PeeHaa Oct 11 '16 at 17:37
  • Maybe you two need to read the title and the question again. It is "How to run MySQL query using OOP in PHP". In my reply one can see I have outlined a simple example as well as adapted code the lazy user can copy and paste. The question is not "How to do OOP". It is more in the fashion of "How to use OOP to use MySQL query". And, yes - the answer is PDO with prepared statement. Not happy with it? Feel free to contribute a better solution. – Nikola Petkanski Oct 11 '16 at 17:44
  • 2
    Don't be nasty @NikolaPetkanski - no need for that. The OP did ask if they were doing OP "the right way". – Jay Blanchard Oct 11 '16 at 17:50
2

The problem is that you are not passing along the connection to the query. This is fixed by return-ing it in the Connect method and the passing it along to the Insert method.

This would change your code to

<?php
class dbConnect{
    private $host="localhost";
    private $user="root";
    private $pass="";
    private $db="oodb";
    public function Connect(){
        $conn=mysqli_connect($this->host, $this->user, $this->pass, $this->db);
        if($conn){
            echo "Connected";
        }

        return $conn;
    }
}
$dbConn=new dbConnect();
$conn = $dbConn->Connect();
class Signup{
    public function Insert($conn, $value){
        if(isset($_REQUEST["sub"])){
            $query="INSERT INTO signup(name) VALUES('$value')";
            //FOLLING STATEMENT IS NOW CORRECT
            $queryrun=mysqli_query($conn, $query);
            echo "Inserted";
        }
        else{
            echo "Error";
        }
    }
}
$sign=new Signup();
if(isset($_POST['id'])){
$sign->Insert($conn, $_POST['id']);
}
?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form name="form1" method="post">
    <input type="text" name="id">
    <input type="submit" name="sub">
</form>
</body>
</html>

But I agree with Nikola, that you should take a look at PDO. It's the object-oriented approach.

As for the quality - I suggest you read some introduction to OOP, because using classes is not writing OOP :)

EDIT: I updated to code to pass the connection along.

Cheers, Mike

Mike Chernev
  • 189
  • 6
-1

I see what the issue is, try making these changes:

$dbConn=new dbConnect();
$conn = $dbConn->Connect();

And then try again with this:

$queryrun = mysqli_query($conn, $query);

All you needed to do was use the right instance to the mysqli_query() method.

Also, please make sure you return $conn in the 'connect()' method.