Jump to content

Bhaal

Members
  • Posts

    60
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

Bhaal's Achievements

Member

Member (2/5)

0

Reputation

  1. Brilliant! I'd never have figured this out. Thank you SO MUCH!
  2. Double '&&' has the same results: it skips the first value pair. I checked the names of the text fields just to make sure - all of them are as follows: <li>Artist: <input type="text" size="25" name="artist[]" class="enter" > Song: <input type="text" size="45" name="song[]" class="enter" ></li> So why would the first set be skipped? Very strange.
  3. Thanks for the help! The query quoted above has the exact same results as the original: 25 records inserted with many blanks (when there should be only as many records as text boxes with values). Playing around with it yielded different results...sort of. This version inserts records for all the values filled in (posted) EXCEPT for the first set: if (count($_POST['artist']) > 0) { foreach ($_POST['artist'] as $key => $val) { $song = $_POST['song'][$key]; $artist = $val; if(!empty($val) & !empty($key)) { $q = "insert into comp_items (compitem_artist, compitem_song, CompID ) values ('$artist', '$song', '$lastID')"; mysql_query($q); } } } So if the user enters three sets of 'artist name', 'song name' only the last two are saved. If they enter 24 sets, only the last 23 are saved. But no blank records, so it's much closer. But having 'or' (as in " if(!empty($val) || !empty($key)) { " ) enters blank records. Darn - so close...kinda frustrating (nope: ignorance is not bliss).
  4. There are two tables: comp_main and comp_items. comp_main has an auto increment ID field: 'comp_id'; comp_items has a 'foreign key' field: 'CompID'. One-to-many: comp_main to comp_items based on that ID. I'm attempting to build a data entry form that allows the user to enter the comp_main info AND the comp_items info. Getting the info into comp_main is the easy part: $qa = "insert into comp_main set comp_name = '$_POST[comp_name]', comp_desc = '$_POST[comp_desc]' "; mysql_query($qa); The difficult part (for me) is the comp_items info. There are only four fields in the comp_items table: - an auto increment ID field; - artist name; - song name; - CompID (the 'foreign key' from comp_main). The form has 25 text boxes for 'artist name' and 25 text boxes for 'song name' (this is the way the client wants it, so they can enter 'artist name', 'song name' then tab to the next set and enter 'artist name', 'song name' on down the form - up to 25 times). I know the comp_items foreign key field (CompID) will be the last inserted ID from the first query: $lastID = mysql_insert_id(); So I'm trying to loop through the 25 sets of artist and song text boxes and only insert those that are filled in: if (count($_POST['artist']) > 0) { foreach ($_POST['artist'] as $key => $val) { $song = $_POST['song'][$key]; $artist = $val; $q = "insert into comp_items (compitem_artist, compitem_song, CompID ) values ('$artist', '$song', '$lastID')"; mysql_query($q); } } That loop does insert the artist name, song name and CompID correctly, but it also inserts multiple 'blank' record with $lastID as CompID. For instance, if the user enters two artist names and song names, there are 25 records entered into comp_items (instead of just 2) with 23 of those records having artist name and song name blank but with CompID as $lastID. It should only enter as many records as there are posts for $artist and $song. (Man I hope I'm explaining this accurately enough!) So, how can I loop through the form, grab the entered values and insert them into comp_items w/o any 'blank' records?
  5. Mark it done! Thank you Jay. Now how do I mark this 'solved'?
  6. Would you mind explaining further? Put 'it' in an array 'first'. Is 'it' $artName? Is 'first' before the print statement - but still inside the while? Or is 'first' before the eregi function? ('Cause I've tried all those: $artName = array(); )
  7. Scenario: table with artist names (and other fields); some names have "The" (as in 'The Dumb Koders'), others have "A" ('A Wilhelm Scream') but most are just a name ('Joe Smith'). I'm using eregi to take the 'The' and place it after the actual name - so it changes 'The Beatles' to 'Beatles, The' - for the purpose of sorting. Except now I don't know how to sort these results - since they are strings. So the sort() function doesn't work. The entire code: <? $sql = "select distinct artistName, song_file_name1, song_file_name2, song_file_name3, song_title1, song_title2, song_title3, desc, sync from indies where ((artistName like '%$srch%')) order by indies.artistName ASC"; $result_artist = mysql_query($sql) or die ($sql . mysql_error()); while ($row = mysql_fetch_array($result_artist)) { $artName = eregi ("^(The|An|A)+ (.*)$", $row['artistName'], $regs) ? $regs[2].", ".$regs[1] : $row['artistName']; print $artName . "<br /> "; } mysql_close(); ?> How can I 're-sort' $artName after running an eregi on it? As I understand it, $artName is not an array therefore sort() doesn't work. Is there a 'simple' way of resorting the eregi results before printing to the page? (Otherwise, the eregi is pretty much useless.) Thanks in advance.
  8. Got it. But of course now there's another problem: re-sorting the results. I'll ask about this in another thread.
  9. For me, it's more of a hate-hate relationship.
  10. This is just dumb 'cause it should be "easy". Got a field holding 'artist name' (in an array) so when looped it prints: Joe Smith The Nube Koders Henry Lee Lisa Jones The Idiots ... (etc.) When sorting, the record "The Nube Koders" gets put in the "T" section obviously. So I need to take the string "The" and place it after the actual name (with a comma) so it prints: Nube Koders, The Pretty sure eregi will work for this (eregi not ereg since some of the names are all caps). But nothing I try is working. After much consternation: $artName = eregi ("(.+),( the)", $row['artistName'], $regs) ? $regs[1].", ".$regs[2] : $row['artistName']; Doesn't work at all though. No surprise there. Any hints? Thanks...
  11. Thanks redarrow. I made one other change that seems to work - instead of: $newQty = $qty++; I'm using: $newQty = $qty + 1; ...which works in a testing environment... THANKS ALL! I truly appreciate it! (Mark this one: resolved)
  12. Code hasn't changed since I last posted it: if($_POST["submit_x"]) { $sql = "select * from cart where OrderID = '$PHPSESSID' "; $rql = mysql_query($sql) or die(mysql_error()); $rows = mysql_num_rows($rql); if($rows == '0') { $MyTotal = $_POST[itemPrice]; $q1 = "insert into cart set OrderID = '$PHPSESSID', ItemID = '$_POST[itemID]', ItemName = '$_POST[itemName]', ItemPrice = '$_POST[itemPrice]', ItemQty = '1', ItemType = '$_POST[itemType]', ItemTotal = '$MyTotal' "; mysql_query($q1) or die(mysql_error()); } else { while($aql = mysql_fetch_array($rql)) { $myItem = $aql[itemID]; $qty = $aql[itemQty]; $newQty = $qty++; $ItemID = $_POST[itemID]; if($myItem == $ItemID) { $q1 = "update cart set ItemQty = $newQty where OrderID = '$PHPSESSID' and ItemID = '$ItemID' "; $uq = mysql_query($q1); } } if(!$uq) { $MyTotal = $_POST[itemPrice]; $q1 = "insert into cart set OrderID = '$PHPSESSID', ItemID = '$_POST[itemID]', ItemName = '$_POST[itemName]', ItemPrice = '$_POST[itemPrice]', ItemQty = '1', ItemType = '$_POST[itemType]', ItemTotal = '$MyTotal' "; mysql_query($q1) or die(mysql_error()); } } }
  13. Well, I still don't understand why the update query portion of this code isn't firing. Any ideas about that?
  14. Alright sspoke - good advice, but much easier said than done, for a newbie like me at least. "...make a function or something in MySQL for that not PHP..." I don't think I've ever attempted anything like that - is that like a stored procedure in SQLServer? Does MySQL support functions on the database level??
  15. This is kind of working (thanks, roopurt18!): if($_POST["submit_x"]) { $sql = "select * from cart where OrderID = '$PHPSESSID' "; $rql = mysql_query($sql) or die(mysql_error()); $rows = mysql_num_rows($rql); if($rows == '0') { $MyTotal = $_POST[itemPrice]; $q1 = "insert into cart set OrderID = '$PHPSESSID', ItemID = '$_POST[itemID]', ItemName = '$_POST[itemName]', ItemPrice = '$_POST[itemPrice]', ItemQty = '1', ItemType = '$_POST[itemType]', ItemTotal = '$MyTotal' "; mysql_query($q1) or die(mysql_error()); } else { while($aql = mysql_fetch_array($rql)) { $myItem = $aql[itemID]; $qty = $aql[itemQty]; $newQty = $qty++; $ItemID = $_POST[itemID]; if($myItem == $ItemID) { $q1 = "update cart set ItemQty = $newQty where OrderID = '$PHPSESSID' and ItemID = '$ItemID' "; $uq = mysql_query($q1); } } if(!$uq) { $MyTotal = $_POST[itemPrice]; $q1 = "insert into cart set OrderID = '$PHPSESSID', ItemID = '$_POST[itemID]', ItemName = '$_POST[itemName]', ItemPrice = '$_POST[itemPrice]', ItemQty = '1', ItemType = '$_POST[itemType]', ItemTotal = '$MyTotal' "; mysql_query($q1) or die(mysql_error()); } } } It IS preventing duplicates (YEAH!!!). However - it's not updating the Quantity ($qty). The first part sees if there are records in the cart - if not, do a simple insert. If there are records, it loops through and tries to find dupes. If it finds dupes, it's supposed to do an update. If it doesn't find dupes it's supposed to do the insert. If the item exists, it's supposed to increment quantity by 1, hence the line: $qty = $aql[itemQty]; $newQty = $qty++; My guess is that it's not firing the update portion of the query at all. I can't see the flaw in the logic. Any clues? Thanks again - this is already a huge leap forward.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.