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.

3

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Noble and all, but this has nothing to do with the question. This, as per your original post stackoverflow.com/revisions/39982962/1
The question is How to run MYSQL query using OOP in PHP? and this is what I am answering to.
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.
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.
Don't be nasty @NikolaPetkanski - no need for that. The OP did ask if they were doing OP "the right way".
|
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

2 Comments

Did you test this? I did... got back Warning: mysqli_query() expects at least 2 parameters....
My bad, I updated to example so that it passes the connection along.
-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.

6 Comments

What is the point of assigning $conn when you are not using it?
Plus, the Connect method doesn't return the connection.
@MikeChernev: you're right, he didn't return the variable in the connect() function
Plus, you need to use $GLOBALS['conn'] in the method, because of the scope.
@MikeChernev: Not really but you can do that or you could just another variable name like $connect_db = $db->connect();. Note: I used $conn as an example.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.