简体   繁体   English

Mysql INSERT INTO和UPDATE无法正常工作

[英]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. 我知道我应该使用mysqli函数,但这不是重点。

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. 现在,如果需要,您不必使用array_push,而必须创建新的数组。

in your code you are changing $name with $name1 so it doesnt work, actually works.. 在您的代码中,您使用$ name1更改了$ name,所以它不起作用,实际上起作用了。

You use array_push to add $name1 to $name. 您可以使用array_push将$ name1添加到$ 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. 您现在在$ name数组中有了一个数组。 but when you call it you only call the second value of this $name array. 但是,当您调用它时,您只会调用此$ name数组的第二个值。 after that you need to call the first value of the $name1 array within the $name array. 之后,您需要调用$ name数组中$ name1数组的第一个值。

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 见我的PHPFiddle http://phpfiddle.org/main/code/zsw-jdj

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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