New to OOP Php

Colin Arenburg

New member
Hello Guys and Gals, i have just started to dive into OOP. I am stuck on how to insert into a database with OOP this is what i have so far. What i would like to do is have the fields taken from the form ( also not one hundred percent sure how to do that.

PHP:
class DataBase {
 
private $dbhost = 'localhost';
private $dbuser = 'ct';
private $dbpass = 'XVXVXVXVXVX(not the password :P)';
private $db = 'flockmanager';
private $fields = array();
private $col = $_POST['column']
 
    function connect()
    {
        $connect = mysql_connect($this->dbhost,$this->dbuser, $this->dbhost)
 
        if (!$connect)
        {
 
            print '<p class="dbError">We are sorry there was an error connecting to the database the error was:'mysql_error'</p>';
 
        }
    }
 
    function insert() {
 
        $connect
        $query = 'INSERT INTO'.$this->column.()
 
 
           
        )
 
 
 
    }
 
}
 
 
?>

I have a class that creates a forum based on code somthing like this:

PHP:
$myform = new Form("action","submitButtonText","submitButtonClass","formClass","meathod","Column");
$myform->addField("fieldName","label","type");

if that helps any, not sure it well
 
Hi there!
I'm not sure if you found your answer yet, as this thread is a little old. However, I would suggest using mysqli rather than mysql for your database connection. At the moment, the code you have above has

PHP:
$connect = mysql_connect($this->dbhost,$this->dbuser, $this->dbhost)
dbhost 2 times. And you would also need
PHP:
$blah = mysql_select_db("database", $connect)

With mysqli, you can instead have:
http://www.php.net/manual/en/mysqli.quickstart.connections.php
PHP:
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

This will establish a database resource that you can use to insert, delete, update, etc. data into your DB.

If you want to get user input from forms, and put that into your database, you'll need to use one of 2 methods in an HTML form.


HTML:
<form method="post" action="handle_form.php">
 
    <input type="text" name="username">...

Will create a variable called $_POST['username']. "handle_form.php" is the file that will be redirected to. In general, this is the file that will handle the input from the user, and make sure to check if $_POST['username'] is valid, is banned, etc.
http://myphpform.com/php-form-tutorial.php

I just found this post through DDG, it should explain a bit more about forms.

POST and GET are essentially the same. The big difference is that GET is used with the URL. So if you have http://site.com/index?id=3 you will now have a variable named $_GET['id'] that's set to 3. Or http://site.com/index.php?name=bob&role=user, where you now have $_GET['name'] set to "bob", and $_GET['role'] set to "user".

Before you put these in the database, you'll want to sanitize them. Because you are using 2 languages (PHP and SQL), you need to make sure that the user can't input SQL that will be executed. To check for this, you will have to pass the variable to an "escaping" function. There are a few that handle this. If you're using MySQL, you may want to look at this: http://us3.php.net/mysql_real_escape_string

So rather than checking for $search_words, you would check for mysql_real_escape_string($search_words).

Feel free to send me an Private Message if you need any help, too :)
 
Thanks for posting, i know all about forms and everything just getting used to OOP vs none OOP is hard for me. I have looked in to CodeIgnitor (sorry can't spell at all lol)
 
Top Bottom