1

Hello Sir i am pass array list through url android to php my arraylistvalues[x,y.z,....] this all values inserted into single column(menuname) in test table to mysql using php

i tried this

$a=$_POST['menuname'];
mysql_connect("localhost","root","MobixMySQL");
mysql_select_db("test");
foreach($a as $value)
{
  mysql_query("INSERT INTO test (menuname) VALUES $value)")or
  die ('unable'.mysql_error());
  echo "Inserted";
}

i try above php it shows error valild argument in foreach please tell me how to solve ..please help me

4

2 回答 2

1
mysql_query("INSERT INTO `test` (`menuname`) VALUES ('".mysql_real_escape_string( $value )."')") or die ('unable'.mysql_error());
于 2012-04-07T01:35:17.330 回答
0

$a可能不是一个数组,因此foreach会抛出错误,因为它期望它的参数是一个数组。

你可以做一个var_dump看看到底是什么$_POST['menuname']

另外,mysql_real_escape_string在插入到数据库之前用于转义

mysql_query("INSERT INTO test (menuname) VALUES ('" . mysql_real_escape_string($value) . "')");

http://sg2.php.net/mysql_real_escape_string

或者更好的是,使用pdo http://sg.php.net/pdo

于 2012-04-07T01:38:51.477 回答