not_a_clue Posted May 24, 2010 Share Posted May 24, 2010 Total newbie so apologies if I've made any blunders. I have set up a form with a number of fields and PHP to go and fetch a unique number from a DB and insert it into one of the fields on a form. It then updates the the DB table to take that unique code out of circulation. The trouble is this is the first time I've written anything in php and the solution I have set up is both clumsy and slow to load. The issue I think is that it goes and gets all the values for the unique code then selects one from that list, whereas I think it should be possible to simply go get the next one. My form is roughly like this: First name <input type="text" id="FIRSTNAME_FIELD" name="FIRSTNAME_FIELD" value="" size="30" maxlength="64"> Last name <input type="text" id="LASTNAME_FIELD" name="FIRSTNAME_FIELD" value="" size="30" maxlength="64"> Email <input type="text" id="EMAIL_FIELD" name="FIRSTNAME_FIELD" value="" size="30" maxlength="64"> <input type="hidden" id="UNIQUE_CODE" name="UNIQUE_CODE_FIELD" value="<?php echo $yourcode; ?>" > <input type="hidden" id="AFFILIATE" name="AFFILIATE_FIELD" value="97" > NB: AFFILIATE is an integer used to define which UNIQUE_CODE is assigned to whom. I.e. codes 000001 thru 20,0000 belong to affiliate 1, 20,001 thru 50,000 to affiliate 2 etc. My PHP reads like this: <?php $con = mysql_connect("DB_LOCATION.com","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("DB_NAME", $con); $sql = "SELECT * from table_name WHERE IN_USE='0' AND AFFILIATE="97""; $result = mysql_query($sql,$con); $rows = mysql_num_rows($result); $counter = $rows-1; $yourcode = mysql_result($result,$counter); mysql_query("UPDATE table_name SET IN_USE='1' WHERE UNIQUE_CODE='$yourcode'"); mysql_close($con);?> As you can see, far too much mucking about, can anyone suggest a better method? Link to comment https://forums.phpfreaks.com/topic/202758-can-anyone-pimp-my-code/ Share on other sites More sharing options...
Bladescope Posted May 24, 2010 Share Posted May 24, 2010 <?php // Connections mysql_connect("DB_LOCATION.com","username","password") or die('Could not connect: ' . mysql_error()); mysql_select_db("DB_NAME", $con) or die('Could not select db: ' . mysql_error()); // Query. The 'limit 1' means it only returns with 1 row. $sql = "SELECT * from table_name WHERE IN_USE='0' AND AFFILIATE='97' ORDER BY UNIQUE_CODE ASC LIMIT 1"; $result = mysql_query($sql,$con); // Check if any rows were returned, at all. if(mysql_num_rows($result) > 0) { // Grab the data and put it back in the database. $yourcode = mysql_result($result,0); mysql_query("UPDATE table_name SET IN_USE='1' WHERE UNIQUE_CODE='$yourcode'") or die('Could not update: '. mysql_error()); } else { // No rows were left in the current affiliate. } // free up resources mysql_close($con); ?> Link to comment https://forums.phpfreaks.com/topic/202758-can-anyone-pimp-my-code/#findComment-1062735 Share on other sites More sharing options...
teamatomic Posted May 24, 2010 Share Posted May 24, 2010 function pimp_code($hat,$trim,$accessory) { echo "Leroy standin' on da corner with a $hat on his head sporting $trim and a $accessory!"; } pimp_code('big lime green floppy brimmed felt hat', 'six purple ostrich feathers trimmed out with a few peacock eyes','a double wrapped tiedyed boa' ); HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/202758-can-anyone-pimp-my-code/#findComment-1062792 Share on other sites More sharing options...
Pikachu2000 Posted May 25, 2010 Share Posted May 25, 2010 function pimp_code($hat,$trim,$accessory) { echo "Leroy standin' on da corner with a $hat on his head sporting $trim and a $accessory!"; } pimp_code('big lime green floppy brimmed felt hat', 'six purple ostrich feathers trimmed out with a few peacock eyes','a double wrapped tiedyed boa' ); HTH Teamatomic Now I has a lulz. Link to comment https://forums.phpfreaks.com/topic/202758-can-anyone-pimp-my-code/#findComment-1062808 Share on other sites More sharing options...
not_a_clue Posted May 25, 2010 Author Share Posted May 25, 2010 Yeah...I liked the first solution and it did seem to work but in the end I went for the Leroy method. Thanks guys, all working fine now. Link to comment https://forums.phpfreaks.com/topic/202758-can-anyone-pimp-my-code/#findComment-1062896 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.