Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-在MySQL中添加多个下拉菜单选项时遇到问题_Php_Implode_Mysql Real Escape String - Fatal编程技术网

PHP-在MySQL中添加多个下拉菜单选项时遇到问题

PHP-在MySQL中添加多个下拉菜单选项时遇到问题,php,implode,mysql-real-escape-string,Php,Implode,Mysql Real Escape String,我在向MySQL数据库添加多个下拉菜单选项时遇到困难。我从中获得了以下代码 及 不幸的是,我没有使用这些函数的经验。有人能指出我这里出了什么问题吗?我正在使用WAMP(PHP5.3.8)和GoogleChrome(版本24.0.1312.52)正如巴特所说,mysql\u real\u escape\u字符串与字符串一起工作,而不是与数组一起工作。 你的$\u POST['game']之所以是一个数组,是因为你把它命名为game[]。如果将名称更改为game,则可以尝试使用一个值 尽管我们希望代

我在向MySQL数据库添加多个下拉菜单选项时遇到困难。我从中获得了以下代码


不幸的是,我没有使用这些函数的经验。有人能指出我这里出了什么问题吗?我正在使用
WAMP(PHP5.3.8)
GoogleChrome(版本24.0.1312.52)

正如巴特所说,mysql\u real\u escape\u字符串与字符串一起工作,而不是与数组一起工作。 你的
$\u POST['game']
之所以是一个数组,是因为你把它命名为
game[]
。如果将名称更改为
game
,则可以尝试使用一个值

尽管我们希望代码能够处理多种选择。您可以这样更改PHP代码:

<?php
if(isset($_POST['submit']))
{
    $query=mysql_connect('localhost','root','');
    mysql_select_db("freeze",$query);
    $choice = array();
    foreach($_POST['game'] as $game) {
        $choice[]=mysql_real_escape_string($game);
    }
    $choice1=implode(',',$choice);
    mysql_query("insert into tb values('','$choice1')");
}
?>

mysql\u real\u escape\u string
使用字符串,而不是字符串数组。谢谢Bart,你对我应该使用什么有什么建议吗?那是因为
name=“game[]”
创建了一个数组。非常感谢Kelu,非常感谢。它现在将数据插入我的数据库;创建表
freeze
tb
id
int(3)非空自动增量,
name
varchar(50)非空,主键(
id
)引擎=InnoDB自动增量=47默认字符集=latin1;
The error message are "Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in C:\WAMP\www\COSHH\test\index.php on line 8"
"Warning: implode() [function.implode]: Invalid arguments passed in C:\WAMP\www\COSHH\test\index.php on line 9" 
<?php
if(isset($_POST['submit']))
{
    $query=mysql_connect('localhost','root','');
    mysql_select_db("freeze",$query);
    $choice = array();
    foreach($_POST['game'] as $game) {
        $choice[]=mysql_real_escape_string($game);
    }
    $choice1=implode(',',$choice);
    mysql_query("insert into tb values('','$choice1')");
}
?>
<?php
if(isset($_POST['submit']))
{
    $query=mysql_connect('localhost','root','');
    mysql_select_db("freeze",$query);
    $choice=implode(',',$_POST['game']);
    $choice1=mysql_real_escape_string($choice);
    mysql_query("insert into tb values('','$choice1')");
}
?>