jjmusicpro Posted October 4, 2007 Share Posted October 4, 2007 Ok, i wanted to know how i can query a DB, get the highest value, or better yet what number comes next, +1 to it, and add put it in a field in my form. Here is what i have. require_once('connect.php'); require_once('opendb.php'); $groupid_sent = $_GET['groupid']; $query = "SELECT groupid FROM metrogroups"; $result = @mysql_query($query); $count = mysql_num_rows($result); $row = mysql_fetch_array($result, MYSQL_ASSOC); echo "<form action='creategroup.php?actname=$groupid_sent' method='post'>"; echo "Group ID: <input type=text name=groupid size=10> <br>"; echo "Group Name: <input type=text name=groupname size=75 > <br>"; echo "Group Area: <input type=text name=grouparea size=75 > <br>"; echo "<input type=submit name=submit value=update>"; echo "</form>"; Quote Link to comment https://forums.phpfreaks.com/topic/71820-get-highest-value-in-column-then-1-to-it/ Share on other sites More sharing options...
trq Posted October 4, 2007 Share Posted October 4, 2007 <?php require_once('connect.php'); require_once('opendb.php'); $groupid_sent = $_GET['groupid']; $query = "SELECT MAX(groupid) AS mxid FROM metrogroups LIMIT 1"; if ($result = mysql_query($query)) { if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); $mxid = $row['mxid']+1; echo "<form action='creategroup.php?actname=$groupid_sent' method='post'>"; echo "Group ID: <input type='text' name='groupid' size='10' value='$mxid'> <br>"; // I assume this is where you want the value? echo "Group Name: <input type='text' name='groupname' size='75'> <br>"; echo "Group Area: <input type='text' name='grouparea' size='75'> <br>"; echo "<input type='submit' name='submit' value='update'>"; echo "</form>"; } } ?> This seems a bad idea to me though. What exactly are you trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/71820-get-highest-value-in-column-then-1-to-it/#findComment-361733 Share on other sites More sharing options...
jjmusicpro Posted October 4, 2007 Author Share Posted October 4, 2007 I am trying to make a "Add new Client" function, i wanted to make it so they dont have to pick an ID number, this way they dont pick an ID number that already exists. this way, since my id numbers go in seq. order, it will get the latest ones say 7 that is already taken, and add 1 to it Quote Link to comment https://forums.phpfreaks.com/topic/71820-get-highest-value-in-column-then-1-to-it/#findComment-361736 Share on other sites More sharing options...
trq Posted October 4, 2007 Share Posted October 4, 2007 You should make your groupid field an auto incrementing primary key instead. There is nothing stopping a client placing any number they please in your form and this will kill your data's integrity. Quote Link to comment https://forums.phpfreaks.com/topic/71820-get-highest-value-in-column-then-1-to-it/#findComment-361737 Share on other sites More sharing options...
jjmusicpro Posted October 4, 2007 Author Share Posted October 4, 2007 ok for some reason, i cant get my query to run and display the data when i pass the ID to it, the fields are blank, it should go intot he database and get the groupname and grouparea from the id i passed <?php if (isset($_POST['submit'])) { require_once ('connect.php'); require_once ('opendb.php'); // define all your post variables here // These are passed in the URL String $groupid = mysql_real_escape_string($_POST['groupid']); $groupname = mysql_real_escape_string($_POST['groupname']); $grouparea = mysql_real_escape_string($_POST['grouparea']); // Run Update Function // account_name='$account_name' $query = "UPDATE metrogroups SET groupname='$groupname', grouparea='$grouparea' WHERE account_name=''"; $result = mysql_query($query)or die(mysql_error()); // Confirmation that updated worked echo "Group Account Edited"; echo "$query"; } else { require_once ('connect.php'); require_once ('opendb.php'); $groupid = $_GET['groupid']; $query = "SELECT * FROM metrogroups WHERE groupid='$groupid'"; $result = @mysql_query($query); $count = mysql_num_rows($result); echo "<form action='editgroup.php' method='post'>"; echo "Group Name: <input type=text name=groupname size=75 > <br>"; echo "Group Area: <input type=text name=grouparea size=75 > <br>"; echo "<input type=submit name=submit value=update>"; echo "</form>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71820-get-highest-value-in-column-then-1-to-it/#findComment-361797 Share on other sites More sharing options...
trq Posted October 4, 2007 Share Posted October 4, 2007 Which query are you talking about? The update or select? Your question / problem is pretty unclear. Quote Link to comment https://forums.phpfreaks.com/topic/71820-get-highest-value-in-column-then-1-to-it/#findComment-361801 Share on other sites More sharing options...
jjmusicpro Posted October 4, 2007 Author Share Posted October 4, 2007 not the post the select, i need to get the values into those text fields Quote Link to comment https://forums.phpfreaks.com/topic/71820-get-highest-value-in-column-then-1-to-it/#findComment-361806 Share on other sites More sharing options...
BlueSkyIS Posted October 4, 2007 Share Posted October 4, 2007 UPDATE WHERE account_name='' ?? Quote Link to comment https://forums.phpfreaks.com/topic/71820-get-highest-value-in-column-then-1-to-it/#findComment-361807 Share on other sites More sharing options...
trq Posted October 4, 2007 Share Posted October 4, 2007 Didn't I just show you how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/71820-get-highest-value-in-column-then-1-to-it/#findComment-361810 Share on other sites More sharing options...
jjmusicpro Posted October 4, 2007 Author Share Posted October 4, 2007 I think i need to somehow get the values from the selects into the text box fields: $groupid = $_GET['groupid']; $query = "SELECT * FROM metrogroups WHERE groupid=$groupid"; $result = @mysql_query($query); $count = mysql_num_rows($result); echo "$query"; // viewing query echo "<form action='editgroup.php' method='post'>"; echo "Group Name: <input type=text name=groupname value?? size=75 > <br>"; echo "Group Area: <input type=text name=grouparea size=75 > <br>"; echo "<input type=submit name=submit value=update>"; echo "</form>"; Quote Link to comment https://forums.phpfreaks.com/topic/71820-get-highest-value-in-column-then-1-to-it/#findComment-361811 Share on other sites More sharing options...
trq Posted October 4, 2007 Share Posted October 4, 2007 I showed you already. If your going to ask a question, at least take the time to view the replies!!! Quote Link to comment https://forums.phpfreaks.com/topic/71820-get-highest-value-in-column-then-1-to-it/#findComment-361815 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.