简体   繁体   中英

how to insert arraylist into single column in mysql database using php

Hello Sir i am pass array list through url android to php my arraylistvalues[x,yz,....] 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

mysql_query("INSERT INTO `test` (`menuname`) VALUES ('".mysql_real_escape_string( $value )."')") or die ('unable'.mysql_error());

$a might not be an array, and as such, foreach throws error as it expects its argument to be an array.

You can do a var_dump to see what $_POST['menuname'] really is.

Also, use mysql_real_escape_string to escape things before you insert to db

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

http://sg2.php.net/mysql_real_escape_string

Or better yet, use pdo http://sg.php.net/pdo

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