[PHP] Problem Inserting Array of Rows from form

2001-07-15 Thread David

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=rightID:   /TDTDinput type=text name=id[]
size=3br/TD

TD align=rightSongname:  /TDTDinput type=text name=songname[]
size=30br/TD
?
$i++;
};




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Problem Inserting Array of Rows from form

2001-07-15 Thread Hank Marquardt

For one, as you've written it you have a mismatch of columns vs. fields --

You're combining id and name into one field for the insert -- thus you have
five fields trying to be inserted into a table with six elemets.

You should have a print of the mysql_error() in your debug code ... I bet if
you did that's what it would tell you:)

On Sun, Jul 15, 2001 at 07:17:00AM -0600, 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=rightID:   /TDTDinput type=text name=id[]
 size=3br/TD
 
 TD align=rightSongname:  /TDTDinput type=text name=songname[]
 size=30br/TD
 ?
 $i++;
 };
 
 
 
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 To contact the list administrators, e-mail: [EMAIL PROTECTED]

-- 
Hank Marquardt [EMAIL PROTECTED]
http://web.yerpso.net

Web  Database Development in PHP, MySQL/PostgreSQL
Small Office Networking Solutions - Debian GNU/Linux  FreeBSD
PHP Instructor - HTML Writers Guild http://www.hwg.org
*** PHP II The Cool Stuff starts July 16, 2001
*** http://www.hwg.org/services/classes/p181.1.html


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Problem Inserting Array of Rows from form

2001-07-15 Thread David

I did add mysql_error which helped me solve part of the problem: I was missing a
single quote in the $val line after $id[$i] and before songname :  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=rightSongname:  /TDTDinput type=text name=songname[]
size=30br/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

Hank Marquardt wrote:

 For one, as you've written it you have a mismatch of columns vs. fields --

 You're combining id and name into one field for the insert -- thus you have
 five fields trying to be inserted into a table with six elemets.

 You should have a print of the mysql_error() in your debug code ... I bet if
 you did that's what it would tell you:)

 On Sun, Jul 15, 2001 at 07:17:00AM -0600, 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=rightID:   /TDTDinput type=text name=id[]
  size=3br/TD
 
  TD align=rightSongname:  /TDTDinput type=text name=songname[]
  size=30br/TD
  ?
  $i++;
  };
 
 
 
 
  --
  PHP General Mailing List (http://www.php.net/)
  To unsubscribe, e-mail: [EMAIL PROTECTED]
  For additional commands, e-mail: [EMAIL PROTECTED]
  To contact the list administrators, e-mail: [EMAIL PROTECTED]

 --
 Hank Marquardt [EMAIL PROTECTED]
 http://web.yerpso.net

 Web  Database Development in PHP, MySQL/PostgreSQL
 Small Office Networking Solutions - Debian GNU/Linux  FreeBSD
 PHP Instructor - HTML Writers Guild http://www.hwg.org
 *** PHP II The Cool Stuff starts July 16, 2001
 *** http://www.hwg.org/services/classes/p181.1.html

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 To contact the list administrators, e-mail: [EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]