0

I am trying to insert data from an Array to MySQL Database. The Database contains two columns (string). Here is the code I wrote. What is wrong?

<?php
$time = date("H:i:s");
$a = array("foo", "bar", "hallo", "world");
//convert values of the array to string
$a = array_map('strval', $a);
//get array size
$arraySize = count($a);

//connection to database
$link = mysql_connect('', '', '') or die('connection lost');
mysql_select_db() or die('DB not found');

//try to insert data from the array to the database
for ($i = 0; $i < $arraySize; $i++) {
    mysql_query("INSERT INTO `` (`time`, `text`) VALUES ('$time','$a[i]'))");
}


mysql_close($link);

?>

I cleared some personal data in the code intentionally.

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • 3
    [**Do not use `mysql_*` functions in new code**](http://stackoverflow.com/q/12859942). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://php.net/mysql-connect)? Learn about [*prepared statements*](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) can help you decide which. – Qirel Sep 26 '17 at 19:27
  • `INSERT INTO `` ...` so where should it go..? There's no table specified. `$a[i]` that would also not be the right index, you don't have a variable identifier on the `$i` variable there – Qirel Sep 26 '17 at 19:27
  • Qirel, I cleared this field intentionally. – Google user Sep 26 '17 at 19:29
  • Why would you remove the table-name? I understand the DB credentials, but why the tablename...? And as a suggestion, you can also use the `CURTIME()` MySQL function instead of using the `$time` variable. – Qirel Sep 26 '17 at 19:29
  • Also, what's happening? You say it's not working, but you don't describe how. Not inserting anything? Inserting wrong values? Does PHP error-reporting yield anything (Yes, it does, if you're looking for it, you would have some warnings here), does `mysql_error()` give anything? – Qirel Sep 26 '17 at 19:33
  • Oh, yes, you are right. The database doesnot receive values from the array. The database just filled out with empty values. Also, if I try `echo $a;` afrer `$a = array_map('strval', $a);` , it just says "`Array`". – Google user Sep 26 '17 at 19:36
  • As it would, it's an array! Like I tried to say in my second comment, `$a[i]` should be `$a[$i]`. Or better yet, use a `foreach` loop instead of a `for` loop. – Qirel Sep 26 '17 at 19:37
  • Qirel, Oh it seems I got the issue. I lost `$` in this statement `$a[$i]`. Thank you a lot, you are the best! – Google user Sep 26 '17 at 19:41

3 Answers3

0

Try this

remove the $a = array_map('strval',$a)

for(){ inside the for loop

$value = (string)$a[i];

$sql = "INSERT INTO $tablename (time,text) VALUES ('$time', '{$value}') ";

mysql_query($sql);

$value = "";

}

0

The problem is with this part of code:

VALUES ('$time','$a[i]')

I missed $ before i.

Also this part of code may be droped without any problems: $a = array_map('strval', $a);

0

Here is your answer..! Hope it works for you

   $contentArray = [];
   $rows = array();

   $time = date("H:i:s");
   $rows[] = array( "foo", "bar", "hallo", "world");
   $fields = json_encode($rows);
   array_push($contentArray,$fields,$time);

   $values[] = '(' . placeholders('?', 2) . ')';

   $field_names = "text,time";

   $sql = "INSERT INTO table_name (" . $field_names . ") VALUES " . implode(',', $values);
Gss Venkatesh
  • 168
  • 12