0

I am encountering an error when inserting to the database, here is the code i am using

class DB_Functions 
{

private $db;

//put your code here
// constructor
function __construct() {
    require_once 'DB_Connect.php';
    // connecting to database
    $this->db = new DB_Connect();
    $this->db->connect();
}

// destructor
function __destruct() {

}

/**
 * Storing new user
 * returns user details
 */
public function storeUnit($email, $unit, $maint, $attent, $done) {
    $con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die (mysql_error());
    mysql_select_db(DB_DATABASE, $con);
    $var = mysql_query('select 1 from `table_name`');
    if ($var !== FALSE){
        $format = 'Y-m-d G:i:s';
        $date = date($format);
        mysql_query("CREATE TABLE '$email'( Col1 VARCHAR, Col2 VARCHAR,Col3 VARCHAR, Col4 VARCHAR, Col5 VARCHAR),$con");
        $result =  mysql_query("INSERT INTO '$email'(Col1, Col2 ,Col3 , Col4 , Col5) VALUES('$unit', '$done', '$attent', '$maint', '$date')");
    } else {
        $result = mysql_query("INSERT INTO '$email'(Col1, Col2 ,Col3 , Col4 , Col5) VALUES('$var2', '$var3', '$var4', '$var5', '$date')");
    }

    // check for successful store
    if ($result) {
        // get unit details 
        $uid = mysql_insert_id(); // last inserted id
        $result = mysql_query("SELECT * FROM users WHERE Col1 = $var2");
        // return unit details
        return mysql_fetch_array($result);
    } else {
        return false;
    }
}
1
  • And the error you're encountering is ....? Commented Oct 19, 2012 at 7:05

1 Answer 1

3

the error is here

mysql_query("CREATE TABLE '$email'( Col1 VARCHAR, Col2 VARCHAR,Col3 VARCHAR, 
                 Col4 VARCHAR, Col5 VARCHAR),$con");
                                            ^ this one

the variable $con should not be included on the string

mysql_query("CREATE TABLE '$email'( Col1 VARCHAR, Col2 VARCHAR,Col3 VARCHAR,
                 Col4 VARCHAR, Col5 VARCHAR)",$con);

Another thing is that you create a column which data type is varchar but you did not specify its capacity. It should be

CREATE TABLE '$email'( Col1 VARCHAR(50), ....

Additional Info about Preventing from SQL INJECTION:

Best way to prevent SQL injection in PHP

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.