Problem Inserting Array of Rows from form
| From: | David | Date: | Sun, 15 Jul 2001 13:17:00 +0000 |
| Subject: | Problem Inserting Array of Rows from form | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-57935@lists.php.net to get a copy of this message | ||
I am trying to insert an array of rows or values from a PHP form into a
MySQL database. There are six columns in the table songs: id, songname,
rating, video, album_id, movie.
Here is what I get when I submit the form
Add songs for Record Array
INSERT INTO songs VALUES (' 1, blah', ' ***', ' 45', ' 2',
' ')
id[0]=: 2 this is debug code
INSERT Failed, check the code......... this is debug code
The problem seems to be with this part:
for ($i=0; $i<= $songsinalbum; $i++) {
$vals .=", ('$id[$i], $songname[$i]', '$rating[$i]',
'$video[$i]',
'$album_id[$i]',
'$movie[$i]')";
}
// $vals=preg_replace("^,", "", $vals);
$vals=preg_replace('/^,/', '', $vals); // chop leading comma
Complete code:
When the user presses submit on the form this part executes:
mysql_connect("192.168.0.1", "mysqluser", "mypassword");
$vals=' ';
for ($i=0; $i<= $songsinalbum; $i++) {
$vals .=", ('$id[$i], $songname[$i]', '$rating[$i]',
'$video[$i]',
'$album_id[$i]',
'$movie[$i]')";
}
// $vals=preg_replace("^,", "", $vals);
$vals=preg_replace('/^,/', '', $vals); // chop leading comma
$qry="INSERT INTO songs VALUES $vals";
echo $qry;
$res=mysql_query($qry);
Here is part of the form:
<?
$i = 1;
while ($i <= $songsinalbum) {
?>
<TD align="right">ID: </TD><TD><input type=text name=id[]
size=3><br></TD>
<TD align="right">Songname: </TD><TD><input type=text
name=songname[]
size=30><br></TD>
<?
$i++;
};