0

If I get post multiple word data, I want to split them and save it into db with different rows.

 keyword = "apple,banana,orage,kiwi,mango"

so I wrote this code.

$keyword = $this->input->post('keyword');
$password = $this->input->post('pwd');
$password_confirm = 'password';

if($password == $password_confirm)
{
    $ExplodeKeywords = explode(",", $keyword);

    foreach($ExplodeKeywords as $val) 
    {
        $arrayshift = array_shift($val);
        if(count($arrayshift) > 0)
        {
            $query = 'INSERT INTO '.T_KEYWORD_POPULAR.' (kp_keyword, kp_time) VALUES ("'.$arrayshift.'", "'.date("Y-m-d H:i:s").'")';
            $this->db->query($query);               
        }
    }

    echo 'GOOD!';
}

It doesn't save the foreach data into db.

Can you see what caused the problem?

4
  • 3
    What's the array_shift doing there? $row is not an array. Commented May 23, 2012 at 8:49
  • 1
    the problem is, we don't know what the problem is if you don't tell us :( Commented May 23, 2012 at 8:49
  • Precisely $row is a word not an array. either you use while($row = array_shift($ExplodeKeywords)) {} or foreach($ExplodeKeywords AS $row) {} either way, $row contains a string with the value of the keyword. It actually should've been named $word (that's why careful naming is so important). Commented May 23, 2012 at 8:50
  • 1
    Why don't you var_dump literally every varable and see what's inside... straighten it out by yourself before thinking it's a problem that SO will fix for you? Commented May 23, 2012 at 8:51

1 Answer 1

4
$ExplodeKeywords = explode(",", $keyword);
foreach ($ExplodeKeywords as $row) {
    $query = 'INSERT INTO '.T_KEYWORD_POPULAR.' (kp_keyword, kp_time) VALUES ("'.$row.'", "'.date("Y-m-d H:i:s").'")';
    $this->db->query($query); 
}
Sign up to request clarification or add additional context in comments.

4 Comments

Would you mind changing the name of the variable from $row to $word?
MihaiStancu: He's learning PHP and stuck with a problem just like us years ago. Be kind, at least he showed what he had done.
@bsdnoobz I understand being stomped and not knowing where to start from. But that is no excuse for not being methodical about it. And in 8 years of work I have found that there was no need for me to ask questions (be it on SO or forums, you name it). Because being methodical and braking problems down and researching them individually would always yield results. If by chance googling a certain question didn't help it was already clear to me that I was asking the wrong question. Or that i didn't break it down enough (i.e. i didn't understand what I was doing).
I have been actively using SO for 2 weeks and have asked 1 question until now regarding RegEx catastrophic failures. I'm not saying SO is not useful (where did you think I found most of my answers when I was googling), I'm just saying there's a difference between asking only when your own wits/knowledge are at an end (and you RTFM-ed already), and writing 50 lines of code without testing and wondering why it doesn't work.

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.