0

I have a form which dynamically creates new fields if the user wishes to. I am using post to retrieve all the values. However, I want to store and display all these values in a single column in a MySQL table.

What I did was create an array to store all these values. However I'm getting an error. Is this possible what I'm doing?

Storing post values in array:

$person1english[]=array("$english1","$english2","$english3","$english4","$english5","$english6","$english7","$english8","$english9","$english10");

$connect=mysql_connect('localhost','root','');
$db=mysql_select_db('conversationmaker');
if($connect)
{
    $sql="insert into relation(english_atom) values('$person1english');
    $query=mysql_query($sql);
    if($query)
    {
        $sql="SELECT * FROM relation";
        $query=mysql_query($sql);
        if($query)
        {

            echo "<table border=1>";
            echo "<tr><th>English</th><th>Sanskrit</th></tr>";
            while($row=mysql_fetch_array($query))
            {               
                echo "<tr>";
                    echo "<td>".$row[1]."</td>";
                    echo "<td>".$row[2]."</td>";
                echo "</tr>";
            }
            echo "</table>";

        }
    }
  }
}

I am only working with English atm so you can ignore the row 2 that you see.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • 1
    Your `insert` query is not closed. What error are you getting by the way? – AyB Mar 03 '14 at 13:26
  • 1
    "insert multiple rows into column" sounds like all kinds of wrong. http://en.wikipedia.org/wiki/Third_normal_form – Mike B Mar 03 '14 at 13:27
  • I know its not good practice from reading some other post. But what im trying to do, this is the only way i can do it. Im also new to php. – user3242593 Mar 03 '14 at 13:29
  • My error is: Parse error: syntax error, unexpected 'SELECT' (T_STRING) – user3242593 Mar 03 '14 at 13:29
  • very bad idea as Mike said, it will be hard and slow to process them later when needed. – Bojan Kovacevic Mar 03 '14 at 13:29
  • 1
    If you're new how come you know it can't be done? :) Also it seems you're confusing field with column. – Jakub Kania Mar 03 '14 at 13:30
  • I can told you, you do not have " in the end of the insert query. – Bojan Kovacevic Mar 03 '14 at 13:30
  • Ok i solved the previous error. Thanks for that. Now this is my error:Parse error: syntax error, unexpected ',' and i dont know how else to go about doing this. – user3242593 Mar 03 '14 at 13:32
  • That hardly tells us anything, does it mention the line number or near where? And you do realize `$person1english` is an array right? Inserting an array as a value would throw an error again. – AyB Mar 03 '14 at 13:35
  • first of all, in php array dont have to have [] in array declaration. If you want to use this bad design anyway,you need to use implode before insert. – Bojan Kovacevic Mar 03 '14 at 13:44
  • the error occurs on line 92. which is the $persone1english[]=("$english1","$english2","$english3","$english4","$english5","$english6","$english7","$english8","$english9","$english10") and can you tell me what other methods there are? – user3242593 Mar 03 '14 at 13:46
  • did you checked this line $sql="insert into relation(english_atom) values('$person1english'); – Rohit Choudhary Mar 03 '14 at 13:58

2 Answers2

0

use serialize() function

$person1english[]=array("$english1","$english2","$english3","$english4","$english5","$english6","$english7","$english8","$english9","$english10");

$connect=mysql_connect('localhost','root','');
$db=mysql_select_db('conversationmaker');
if($connect)
{
   $sql="insert into relation(english_atom) values('".serialize($person1english)."')";

use uunserialize() while fetching data from DB

Hope this help. Thanks

  • I keep getting this error : Parse error: syntax error, unexpected ',' in $person1english[]=("$english1","$english2","$english3","$english4","$english5","$english6","$english7","$english8","$english9","$english10"); – user3242593 Mar 03 '14 at 14:12
-1

Insert multiple rows with one query MySQL -

here is your answer

OR you can also iterate over your array, i.e. with foreach.

Community
  • 1
  • 1
socksnake
  • 44
  • 2