Re: Re: Problem Inserting Array of Rows from form
| From: | David | Date: | Sun, 15 Jul 2001 16:17:05 +0000 |
| Subject: | Re: Re: Problem Inserting Array of Rows from form | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-57952@lists.php.net to get a copy of this message | ||
I will try without single quotes for the id and other fields which have numeric
values. For the original issue I was missing two single quotes. I was missing
a
single quote in the $val line after $id[$i] and before songname : I really
don't need the quote for id since it is a numeric value. Here is the
corrected code but there is still a problem,
$vals .=", ('$id[$i]', '$songname[$i]', '$rating[$i]',
'$video[$i]',
'$album_id[$i]',
'$movie[$i]')";
old code was:
$vals .=", ('$id[$i], $songname[$i]', '$rating[$i]',
'$video[$i]',
> '$album_id[$i]',
Now the problem is it only inserts one row into the table, when there should be
more than one row inserted.
Here is the updated code and a sample entry:
<?
if (isset($songname)&& isset($rating)){
mysql_connect("24.1.15.33", "webuser", "");
echo $id[0];
$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); // chop leading comma
$qry="INSERT INTO songs VALUES $vals";
echo $qry;
$result = mysql_db_query("movies", $qry);
// $res=mysql_query("movies", $qry);
$error_number = mysql_errno();
$error_msg = mysql_error();
echo "MySQL error $error_number: $error_msg";
Here is part of the form:
<TD align="right">Songname: </TD><TD><input type=text
name=songname[]
size=30><br></TD>
Results:
Add songs for Record Array
2INSERT INTO songs VALUES (' 2', ' test', ' ***', ' ',
' 1', ' ')MySQL error 0:
id[0]=: 3
ID[1]: 3
Songname[1]: test
Rating[1]: ***
Video[1]:
Album ID[1]: 1
test was added to the database
James Tan wrote:
> 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++;
> > };
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net