Jump to content

Recommended Posts

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>";

Link to comment
https://forums.phpfreaks.com/topic/71820-get-highest-value-in-column-then-1-to-it/
Share on other sites

<?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?

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 :)

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>";
       
}

?>

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>";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.