1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

New to OOP Php

Discussion in 'General PHP and MySQL Discussions' started by Colin Arenburg, Mar 4, 2012.

  1. Colin Arenburg

    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.

    class DataBase {
    $dbhost 'localhost';
    $dbuser 'ct';
    $dbpass 'XVXVXVXVXVX(not the password :P)';
    $db 'flockmanager';
    $fields = array();
    $col $_POST['column']
    $connect mysql_connect($this->dbhost,$this->dbuser$this->dbhost)
            if (!
    '<p class="dbError">We are sorry there was an error connecting to the database the error was:'mysql_error'</p>';
    insert() {
    'INSERT INTO'.$this->column.()
    I have a class that creates a forum based on code somthing like this:

    $myform = new Form("action","submitButtonText","submitButtonClass","formClass","meathod","Column");
    if that helps any, not sure it well
  2. chousho

    chousho Well-Known Member

    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

    $connect mysql_connect($this->dbhost,$this->dbuser$this->dbhost)
    dbhost 2 times. And you would also need
    $blah mysql_select_db("database"$connect)
    With mysqli, you can instead have:
    $mysqli = new mysqli("localhost""user""password""database");
    if (
    $mysqli->connect_errno) {
    "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.

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

    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 :)
    ne7 and Ingenious like this.
  3. Colin Arenburg

    Colin Arenburg New Member

    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)

Share This Page