0

When I enter data to mysql it shows as below, I want to take all the data to a one row. but since I'm entering data using an array I only have access to two elements at once. So how can I enter data to a single line? below is a snip of my code

$array = array("AAPL","GOOG","YHOO","FB","MSFT","NKE","SBUX");
foreach ($array as $s) {
    $sql = "INSERT INTO portfolio ({$s} , {$sss}) VALUES ('{$amount}', '{$value}')";
}

Snip from MYSQL table

user3378165
  • 6,546
  • 17
  • 62
  • 101
kasunjith
  • 21
  • 5
  • 1
    provide your table schema and code you use to insert data and code you are trying to get data from db – Alex Jun 15 '16 at 18:55
  • [Little Bobby](http://bobby-tables.com/) says [your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Jun 15 '16 at 19:26

1 Answers1

0

Your array is not of data values but column names. Your insert needs to be more like;

  $sql = "INSERT INTO portfolio ("
  foreach ($array as $s) {
       $sql = $sql + " $s, ";
  }
  $sql = $sql + ") values (";
  foreach ($amount as $s) {
       $sql = $sql + " $s, ";
  };
  $sql = $sql + ");"

However, you should be using mysqli, binding parameters not building strings as in this example. Also, your table as shown has only one value per stock, no value and amount, only room for one value.

john elemans
  • 2,578
  • 2
  • 15
  • 26