0

I'm trying to insert two rows but the id of every row is in the same array, how can I insert correctly?

Because I tried by this way but only insert me the first id.

$sql = "
    INSERT INTO cars(car, price, createdDate, createdBy) 
    VALUES (".$this->db->escape($ids).",
            ".$this->db->escape($price).",
            NOW(),
            ".$this->session->userdata('admin_id').")";             

    mysql_query($sql);
    echo ($sql);

This is what I get:

INSERT INTO cars (car, price, createdDate, createdBy) 
VALUES ('217450,217449', '15', NOW(), 150) 

In car I want to insert the price, createdDate and createdBy on the two car Ids 217450,217449.

Thank you in advance.

5
  • 'ids' are 'csv' values ? Commented Aug 6, 2014 at 7:30
  • Yes they are comma separated values. Commented Aug 6, 2014 at 7:32
  • use explode function to make it as separate array and insert.... Commented Aug 6, 2014 at 7:32
  • @Torrezzzz yes I tried with explode but throw me error. Commented Aug 6, 2014 at 7:32
  • @devtreat: what error ? Commented Aug 6, 2014 at 7:34

3 Answers 3

1
$ids = "217450, 217449";
$id_explode = explode(",", $ids);

foreach($id_explode as $id)
{
    $sql = "
             INSERT INTO cars(car, price, createdDate, createdBy) 
             VALUES (".$this->db->escape($id).",
             ".$this->db->escape($price).",
             NOW(),
             ".$this->session->userdata('admin_id').")
           ";             

   mysql_query($sql);
   echo ($sql);
}


But I recommend you not to use raw SQL queries as it is vulnerable to SQL injection.
Hence, Use CI's active record:

$ids = "217450, 217449";
$id_explode = explode(",", $ids);

$insert_batch = array();
foreach($id_explode as $id)
{
    $arr = array(
              'car' => $id,
              'price' => $price,
              'createdDate' => NOW(),
              'createdBy' => $this->session->userdata('admin_id'),
             );
    $insert_batch[] = $arr;
}

$this->db->insert_batch('cars', $insert_batch); 


Documentation:

https://ellislab.com/codeigniter/user-guide/database/active_record.html

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

1 Comment

Happy to help. Also recommended a suggestion.
1

Use PHP function explode:

$ids= explode(',', $ids);
foreach($ids as $id)
{
    $sql = "INSERT INTO cars(car, price, createdDate, createdBy) 
        VALUES (" . $this->db->escape($id) . ", " . $this->db->escape($price)
        . ", NOW(), " . $this->session->userdata('admin_id') . ")";
    mysql_query($sql);
}

Comments

1

If you use ids as array like $ids = array('217450','217449');

$sql = "
INSERT INTO cars(car, price, createdDate, createdBy) 
VALUES ";
foreach($ids as $id){

$sql .=(".$this->db->escape($id).",
        ".$this->db->escape($price).",
        NOW(),
        ".$this->session->userdata('admin_id')."),";             
}
mysql_query($sql);
echo ($sql);

Now it will create query

 INSERT INTO cars (car, price, createdDate, createdBy)
VALUES ('217450', '15', NOW(), 150),('217450', '15', NOW(), 150);

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.