简体   繁体   中英

Mysql INSERT INTO and UPDATE won't work properly

$name = array('maxgebruikers' => $_POST['maxgebruikers']);
$name1 = array('maxplaylist' => $_POST['maxplaylist']);
array_push($name,$name1);

$result = mysql_query("SELECT * FROM organisation_settings WHERE organisation_id = '{$organisatieID}'");
if(mysql_fetch_assoc($result) == 0)
{
    foreach($name as $type=>$value) {
    mysql_query("INSERT INTO organisation_settings (type, value, organisation_id)
    VALUES('{$type}', '{$value}' ,'{$organisatieID}')");
    print_r($name);
    }
}
else
{
    foreach($name as $type=>$value) {
    mysql_query("UPDATE organisation_settings 
    WHERE organisation_id = '{$organisatieID}'(type, value, organisation_id)
    SET VALUES('{$type}', '{$value}' ,'{$organisatieID}')");
    print_r($name);
    }
}

As you can see at the picture, I want where type is

like my array;

It won't insert correctly, where and what is the error in the code?

I know I should use mysqli function, but it is not the point now.

you need to use array like this,

$name = array('maxgebruikers' => $_POST['maxgebruikers'],
              'maxplaylist' => $_POST['maxplaylist']
);

now you dont have to use array_push if you need you must create new array.

in your code you are changing $name with $name1 so it doesnt work, actually works..

You use array_push to add $name1 to $name. so your name array now looks like this:

$name = array('maxgebruikers' => $_POST['maxgebruikers'], 
               array('maxplaylist' => $_POST['maxplaylist']));

you have now got an array in your $name array. but when you call it you only call the second value of this $name array. after that you need to call the first value of the $name1 array within the $name array.

I hope you get what i mean.

This is because you put an array in an array. If you want to make it work you'll need te redifne your variables. See my PHPFiddle http://phpfiddle.org/main/code/zsw-jdj

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM