开发者

Inserting 2D Values of Array into SQL

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]开发者_如何学Go => 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.


You need to add a set of data values to insert. These would be in the form ("Robert","Brown","Robert.Brown@uni.com","34")("Robert","Smith","Robert.Smith@uni.com","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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜