Jump to content


Photo

Grabbing post values, then modifiying them


  • Please log in to reply
3 replies to this topic

#1 artfuldrone

artfuldrone
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 03 October 2006 - 04:22 AM

Here's my situation: I am building a lyrics database for a project. I'm now working on a page where I can add albums. Firsty, I enter how many tracks are in the album, and from there I enter the songs.

What my problem is, is that I somehow need to grab all post values (eg. tracks) from the form, and modify them and insert the values into a MySQL query.

So if the album only had 4 songs, the query would look like this:

insert into albums_table (track1, track2, track3, track4) values('$_POST['track1']', '$_POST['track2']', '$_POST['track3']', '$_POST['track4']')

Of course if the next album had 12 songs the query would need to change.

I'd also like to add a few functions to the values like 'trim' and 'ucwords'.

As much help as possible will be greatly appreciated.

#2 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 03 October 2006 - 04:33 AM

There's always a better way, but this is how I would do it.

$query="INSERT INTO `albums_table` (";
do {
    $query.="`".key($_POST)."`,";
} while(next($_POST));
$query=substr($query,0,-1);
reset($_POST);
$query.=") VALUES (";
do {
    $query.="'".current($_POST)."',";
} while(next($_POST));
$query=substr($query,0,-1);
$query.=")";
mysql_query($query);


#3 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 03 October 2006 - 04:45 AM

Well I would do it sloppy but the easy way, and much easier to read and debug, though significantly SLOWER. Put each track into an array.
for($i = 0; $i < count($tracks_array); $i++)
{
    $sql = "INSERT into albums_table (track" . $i . ") VALUES ('$track_array[$i]')";
    mysql_query($sql) OR die("Fatal Error: " . mysql_error() . "");
}

Hopefully you see the slowdown here, I am doing a query for every track, which in your case probably should'nt matter, looping through this 15 or 20 times inst a big deal. Now a few hundred or thousand, I would'nt do the code I wrote above. For me though, the easy nature and easy reading makes the above my choice.

#4 JasonLewis

JasonLewis
  • Members
  • PipPipPip
  • Advanced Member
  • 3,351 posts
  • LocationVictoria, Australia

Posted 03 October 2006 - 12:04 PM

you could just make a seperate table in sql for the tracks. then have say id, album_id, trackname.
just another suggestion for you to ponder over...
Good luck with your coding.
Jason / ProjectFear / Jaysonic




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users