0

Right now, I have looped a form which in the end gives me a 2D Array.

Array 0D
User Arrays 1D
User Fields 2D

Array ( [1] => Array ( [fname] => qweqwe [lname] => qwewqe [email] => qwewqe [age] => wewqe ) [2] => Array ( [fname] => 123123 [lname] => 123123 [email] => 23123 [age] => 23123 ) )

This is an example of what I get when I type in rubbish into my fields. To check if I could get the values for each form, I used this:

$i = $_POST['number'];
$corporate = $_POST['corporate'];
$x = 1;
print_r($corporate);
while($x <= $i)
{
echo "The first name first name".$corporate[$x]["fname"].".";
}

Using this, I would attain the first name of the first user, followed by the second and so on. This proves that I can get the 2D values from my forms.

What I have to do now is to insert those values into my table. Keep in mind, my 1D contains values of each user. 2D is the values itself.

mysql_query("INSERT INTO students ('fname','lname','email', 'age') VALUES

I have no idea what to put after that. Any help would be appreciated.

0

2 Answers 2

1

You need to add a set of data values to insert. These would be in the form ("Robert","Brown","[email protected]","34")("Robert","Smith","[email protected]","33")

What version of PHP are you using?

for php5.3 you could try:

$values = array();
foreach($corporate as $line){
$values[] = "('".implode("','",array_map(function($x){ return addslashes($x); })) . "')";
}
$query = "INSERT INTO students ('fname','lname','email', 'age') VALUES";
$query .= implode($values);
$record = mysql_query($query);

Otherwise, try:

$values = array();
foreach($corporate as $line){

foreach($line as $i=>$item) $line[$i] = addslashes($item);
$values[] = "('".implode("','",$line) . "')";
}
$query = "INSERT INTO students ('fname','lname','email', 'age') VALUES";
$query .= implode($values);
$record = mysql_query($query);

To solve the second part of your problem, you need to edit the table definitions and remove the "NOT NULL" definition that sits on each field. Do you have php my admin on the server? you could do it through that by editing the table and fields, otherwise you could run the sql using ALTER TABLE. Let me know if you want more information on that.

Sign up to request clarification or add additional context in comments.

2 Comments

I am personally using PHP 5.3. However the system of which it would be installed on is using a version of 5.1. Thank you for the reply, but the code does not work. You missed 1 '{' and 2 '}'.
Thanks mate, must have missed that out in my copy and paste. (updated)
1

Well, using your query, understanding what it means in depth, I finally got it working.

This is the code that I used:

  foreach($corporate as $line)
        {
        $values[] = "('".implode("','",$line) . "')";
        echo "<br />";
        print_r($values);
        $a = implode(",", $values);
        echo $a;
        }

    $query = "INSERT into students (fname, lname,email,age) VALUES $a";
    $sql = mysql_query($query); 

This works fine on my database, works like a charm.
However, when I try this on my friends DB or an Online DB, I get an error which requires me to give every field a value. For some reason, it cannot enter NULL values.

I'm trying to figure out why, but the gist of my problem has been solved.
If you know what is causing my error, feel free to comment. Thanks.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.