0

The code----

public function create(){
global $database;
//this code works perfectly to insert the new user into my database.
$sql = "INSERT INTO users (";
$sql .= "username, password, first_name, last_name";
$sql .= ") VALUES ('";
$sql .= $database->escape_value($this->username) ."', '";
$sql .= $database->escape_value($this->password) ."', '";
$sql .= $database->escape_value($this->first_name) ."', '";
$sql .= $database->escape_value($this->last_name) ."')";
if($database->query($sql)){
    $this->id = $database->insert_id();
return true;
}else{
return false;
}       

public function create()
{
    global $database;
       //this code is to be universal for other database types but it is not working.
    $attributes = $this->attributes();
    $sql        = "INSERT INTO ".self::$table_name." (";
    $sql .= join(", ", array_keys($attributes));
    $sql .= ") VALUES ('";
    $sql .= join("', '", array_values($attributes));
    $sql .= "')";
    if ($database->query($sql)) {
        $this->id = $database->insert_id();

        return true;
    } else {
        return false;
    }
}

the problem - suppose to be generalizing the script so it can be used in any database structure. Its not working in my wamp.

8
  • 3
    Show the query after your variables have been interpolated. Also show the actual error message. Commented Jun 18, 2014 at 13:47
  • Database query failed: Incorrect integer value: '' for column 'id' at row 1 Commented Jun 18, 2014 at 13:52
  • my sql code worked but the instructor is having us change this to work with late static binding in an extended class. Commented Jun 18, 2014 at 13:53
  • 1
    You're asking a question about something without providing sufficient information - that being the value of $attributes. That way, how can we know what the heck you're trying to do with the db? Commented Jun 18, 2014 at 14:58
  • here was the original code that worked....' Commented Jun 18, 2014 at 16:12

6 Answers 6

2

I think there may be an issue with magic quote . Kindly use addslash php function for array_values($attributes)

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

4 Comments

thank you, I tried it and it did not work. The problem is that I went from a working sql format if inserting my id, username, password, first and last name to this format as per my instructor but it is not working.
Could you print the sql and paste here. please also share the structure.
That is way too much data to put here, I could email it to you.
Yes, Lets have a chat [email protected]
1

This is not a programming error from your end the problem here is that MySQL is not interpreting this action as valid due to its SQL_MODE being in STRICT mode. You can either choose to edit your MySQL settings or add this code to your database class:

private function open_connection() {
    $this->connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASSWORD,DB_NAME);
    if (!$this->connection) {
      # code...
      die("Connection to database failed" . mysqli_errno($this->connection));
    }else{
      /// this is the part you should take note of i changed the sql mode here using this code 
      mysqli_query($this->connection, "SET GLOBAL sql_mode = ''");
    }

Comments

0

Your problem might be that you are using join("', '", array_values($attributes)) into the values you are trying to insert. But i guess, your column types aren't all strings, so you are creating something like :

INSERT INTO mytable(id,column1)
VALUES('id','mycolumn1')

Problem is probably that your id column's type is int, that might be why you have Incorrect integer value. So you should have something like :

INSERT INTO mytable(id,column1)
VALUES(1,'mycolumn1')

Just guessing here, hope that helps.

7 Comments

MySQL can convert '1' to 1 automatically, that's not a problem. But it cannot convert '' to a 0, which is the reason for this specific problem.
I guessed $attributes values were strings(not a '1', but more of a 'a'), and so trying to add a string in a int type column would fail. You might be right on this, guess we lack informations.
The quoted error message suggests that the value being inserted was exactly '', not some other string ;) (In the general case, the OP also needs to handle NULL values properly.)
Oho, missed this since i was sure '' could work. But this might be thx to AUTO_INCREMENT i always use. Thx for pointing this out.
The problem really came when using the join (", " . I am trying to create a new user for the db and have the user inserted into the DB.,
|
0

I run the same issue. I wanted to post reply if anyone goes through the same problem. I think you were following lynda.com tutorial. As DCoder mentioned, id is auto increment value, and there is no way knowing what the value is, and moreover, join function made each property values-column values strings, in your case you get empty ""string. But id field is an int. Your first create function, the one without join, worked because the id field is left out. Now to fix the second create function after the line $attributes = $this->attributes(); add the following code
if($attributes["id"]==""){ array_shift($attributes); } after the line $attributes = $this->attributes(); Your attributes holds a list of properties; I am also making assumption that your code is the same as the tutorial I come across. So you check if the id is empty. If it is empty, you do an array_shift. This remove the first attribute, that is id, and in the process the new attributes array will have no id, and then when you apply the join it will be the same as your first create function. This will fix the issue. But I still would like to know how the instructor pass the tutorial without any issue. Great instructor and great tutorial for OOP--I am talking about the lynda.com tutorial. You could elaborate your question more to get more response or the right answer. Since I run the same issue made it easier for me.

Comments

0

To let MySql generate sequence numbers for an AUTO_INCREMENT field you have some options:

explicitly assign NULL; explicitly assign 0

public function create() { global $database; // USE '0' for your auto_increment not null filed if your filed need to sanitize. $this->id = 0;

    $attributes = $this->sanitized_attributes();

  $sql = "INSERT INTO ".self::$table_name." (";
    $sql .= join(", ", array_keys($attributes));
  $sql .= ") VALUES ('";
    $sql .= join("', '", array_values($attributes));
    $sql .= "')";

  if($database->query($sql)) {
    $this->id = $database->insert_id();

    return true;
  } else {
    return false;
  }
}

Comments

0

This is how I figured to solve this problem

public function create(){
    global $database;

    $attributes = $this->sanitized_attributes();
    $keys = array_keys($attributes);
    $values = array_values($attributes);
    $id = array_shift($values);
    $id = (int) $id;
    $sql  = "INSERT INTO ".static::$table_name." (";
    $sql .= join(", ", $keys);
    $sql .= ") VALUES ($id,'";
    $sql .= join("', '", $values);
    $sql .= "')";
    if($database->query($sql)) {
        $this->id = $database->insert_id();
        return true;
    } else {
        return false;
    }
}

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.