I am trying to get the values from a php file.
I have a java file like this
ArrayList<NameValuePair> al = new ArrayList<NameValuePair>();
for (int i = 0; i < name.size(); i++)
{
al.add(new BasicNameValuePair("names["+i+"]",String.valueOf(name.get(i))));
System.out.println("arr is " +String.valueOf(name.get(i)+ "arrrr"+name.get(i)));
}
Here, name is an ArrayList.
Now I want that name ArrayList to be sent to php and loop that array and insert into database
this is my php file
$con = mysql_connect("localhost","aaa","aa");
mysql_select_db("aa", $con);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$arr[] = $_POST['names[]'];
foreach ($arr as $key)
{
$query="insert into Orders1(names) values('$key')";
mysql_query($query);
}
it’s inserting "" (empty) value in database please help me.
edit: and now you’ve deleted your php code, so the following won’t make much sense…
If you’re using array-based field names in your html form, e.g.
then the
[]do NOT appear in the $_POST array when the form’s submitted. Instead,$_POST['foo']becomes a sub-array of the various values entered into those form fields.That means
is almost certainly an undefined array key, returning a
NULL, which you then append to the$arrarray. You loop over $arr and insert the values, but insert only that null.Try, simply:
note the use of the escape function. Your code is highly vulnerable to SQL injection attacks and would leave your server open to a total remote compromise.