0

I have this php code, no error is thrown but the data is not inserted to the table and no table is created, can you please tell me where i am going wrong?

Cheers in advance :)

DB_Functions.php

private $db;

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

// destructor
function __destruct() {

}

 public function storeUnit($email, $units, $maint, $attent, $done) {
    $con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die (mysql_error());
    mysql_select_db(DB_DATABASE, $con);

        $format = 'Y-m-d G:i:s';
        $date = date($format);
        $result = mysql_query("CREATE TABLE IF NOT EXISTS $email( Units VARCHAR(10), Finish VARCHAR(20), Attent VARCHAR(200), Maint VARCHAR(200), created_at VARCHAR(30))",$con);
        $result = mysql_query("INSERT INTO '$email'(Units, Finish, Attent, Maint, created_at) VALUES('$units', '$done', '$attent', '$maint', '$date')");
        return true;
}
6
  • 1
    what the error are you getting? Commented Oct 19, 2012 at 9:14
  • im not getting one, plainly the information is not being incerted into the table and the table is not being created Commented Oct 19, 2012 at 9:15
  • 1
    @Nicholas remove the single quote around the name of the table CREATE TABLE IF NOT EXISTS $email ... click this link for example. as you can see the query was successfully built. try to modify it by adding single quote ion tableName and it won't build. Commented Oct 19, 2012 at 9:17
  • Thanks, it still doesn't work tho :( Commented Oct 19, 2012 at 9:19
  • @Nicholas you mean the table was not created even if you remove the single quote? Commented Oct 19, 2012 at 9:20

2 Answers 2

3

this is my first post, but I think you've bad architecture of code or bad OOP design for your application, but the problem is you use wrong quotes to create table and insert row. You need to use these symbols to quote names "`" for example:

CREATE TABLE `table name` ....
INSERT INTO `table name` ....

So I think that you have to think for your security of code, use mysql_real_escape_string

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

1 Comment

Sorry I am quite new to php and mysql so excuse my bad sytax
2

try using ` instead of ' for tablename

$result = mysql_query("INSERT INTO `$email`(Units, Finish, Attent, Maint, created_at) VALUES('$units', '$done', '$attent', '$maint', '$date')");

Edit : Try looking if your database uses InnoDB, you may use transactional mode... so you must commit changes if autocommit is disabled

2 Comments

Your AWESOME!!!!!!!!! It works now!!! Thank you heaps!!!! I will accept your answer when i'm allowed to :)
You can accept the answer still just click on tick(Accept Sign) below rank of this answer

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.