Re: Problem Inserting Array of Rows from form
| From: | James Tan | Date: | Sun, 15 Jul 2001 15:46:56 +0000 |
| Subject: | Re: Problem Inserting Array of Rows from form | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-57947@lists.php.net to get a copy of this message | ||
dear david,
did u know u do not need to put single quote to enclose number type value??
notice ur field select syntax:
INSERT INTO songs VALUES (' 1 // --> 1 is not enclosed with end quote...
thus makes only 5 fields, not 6
, blah', ' ***', ' 45', ' 2', ' ')
id[0]=: 2
try
INSERT INTO songs VALUES ('1', 'blah', ' ***', ' 45', '
2', ' ')
id[0]=: 2
hope it helps,
regards,
James
David wrote:
> 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++;
> };