I have a textbox which pulls the maximum value from DB. But it gives wrong value.

Can you help figure out what went wrong here?

basicfunction:

private function basicInformation() {
            // initialize variables
    $host = "localhost";
    $db_name = "test";
    $tbl_name = "ballpark_details";
    $username = "root";
    $password = "";

    // connect to database
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB"); # ORDER BY ballpark_details_id DESC
    $query = mysql_query("SELECT MAX(ballpark_details_booking_ref) as max_booking_ref FROM `ballpark_details`");
    //Getting the max ref_id
    $values = mysql_fetch_assoc($query);    
    while ($query !=-1 && $row = mysql_fetch_assoc($query)) {
    $max = $row['max_booking_ref'];
    }
    echo print_r($values, true);
    $html = "";
    $html .= '<fieldset id="basic-information" class="ui-widget">' . PHP_EOL;
    $html .= '<legend>Basic Ballpark Information</legend>' . PHP_EOL;
    $rowClass = "input-row";

    //$html .= $this->wrapLabelTextbox($this->inputBookingRef($values['max_booking_ref)']), $rowClass);
    $html .= $this->wrapLabelTextbox($this->inputBookingRef($max), $rowClass);
    //$html .= $this->wrapLabelTextbox($this->inputBookingRef(), $rowClass);
    $html .= $this->wrapLabelTextbox($this->inputBank(), $rowClass);
    $html .= $this->wrapLabelTextbox($this->inputRegion(), $rowClass);
    $html .= $this->wrapLabelTextbox($this->inputDescription(), $rowClass);
    $html .= $this->wrapLabelTextbox($this->inputNotes(), $rowClass);
    $html .= $this->wrapLabelTextbox($this->inputStartDate(), $rowClass);
    $html .= $this->wrapLabelTextbox($this->inputRequestedDeliveryDate(), $rowClass); #$this->inputEndDate()
    $html .= $this->wrapLabelTextbox($this->inputExpiryDate(), $rowClass);
    $html .= $this->wrapLabelTextbox($this->inputURL(), $rowClass);
    $html .= $this->wrapLabelTextbox($this->inputCAR(), $rowClass);
    (strcmp($_GET["page"], "createballpark"))?
        $html .= $this->wrapLabelTextbox($this->inputProjectStatusEdit(), $rowClass) :
        $html .= $this->wrapLabelTextbox($this->inputProjectStatusCreate(), $rowClass);         
    $html .= '</fieldset>';
    return $html;
}

and my inputBookingRef function:

private function inputBookingRef($values){ 
    //$values = $this->bookingRef;
    $html = "";
    $values++;
    $html .= '<label for="ref">Booking Ref: </label>';
    $html .= HTML::inputText("ref", 20, $values) . PHP_EOL;
    return $html;
}

In this line:

echo print_r($values, true);

It's displaying the last max value. But in my textbox it's just giving value number 1. I don't know what's wrong with my codings. Any help is greatly appreciated so I could go on with my program. Thanks.

Your database section looks a little mamlformed, specifically this line:
while ($query !=-1 && $row = mysql_fetch_assoc($query)) {
I would guess that the previous line:
$values = mysql_fetch_assoc($query);
already collected the necessary information, so if this line:
echo print_r($values, true);
prints the correct information, I would try changing your code at line 12(ish) to:

if(!$res = mysql_query("SELECT MAX(ballpark_details_booking_ref) as max_booking_ref FROM `ballpark_details`")){//error check the db interaction.
    die(mysql_error());
} else {//we are selecting 'MAX', there will only be one row
    $row = mysql_fetch_assoc($res);//no need for a while loop to collect one line.
    $max = $row['max_booking_ref'];
}

...does it work as intended?

@adam yes it's echo print_r($values, true) gives the correct value. I tried to change my code to yours but it's still the same. I already deleted my while loop. The value that's always displaying is number 1. And when I would comment out the line $values++ in inputBookingRef function, the textbox is empty.

Okay, I'm lost now :)
Can you repost your updated code and as clearly and simply as possible tell me what exactly what you want to achieve.
You are collecting data in one place and hoping to show it in another place, you need to check each place where the data is transferred. It's like a relay race, we need to look at each changeover and see who is dropping the baton.

Hi,

this is the FYI,

    private function basicInformation() {
    // initialize variables
    $host = "localhost";
    $db_name = "test";
    $tbl_name = "ballpark_details";
    $username = "root";
    $password = "";
    // connect to database
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB"); # ORDER BY ballpark_details_id DESC
    //$query = mysql_query("SELECT MAX(ballpark_details_booking_ref) as max_booking_ref FROM `ballpark_details`");
    //Getting the max ref_id
    if(!$res = mysql_query("SELECT MAX(ballpark_details_booking_ref) as max_booking_ref FROM `ballpark_details`"))
    {//error check the db interaction.
    die(mysql_error());
    } else {//we are selecting 'MAX', there will only be one row
    $row = mysql_fetch_assoc($res);//no need for a while loop to collect one line.
    $max = $row['max_booking_ref'];
    }
    $values = $max;
    echo print_r($values, true);
    $html = "";
    $html .= '<fieldset id="basic-information" class="ui-widget">' . PHP_EOL;
    $html .= '<legend>Basic Ballpark Information</legend>' . PHP_EOL;
    $rowClass = "input-row";

    $html .= $this->wrapLabelTextbox($this->inputBookingRef($values, true), $rowClass); //['max_booking_ref)']
    //$html .= $this->wrapLabelTextbox($this->inputBookingRef($max), $rowClass);
    $html .= $this->wrapLabelTextbox($this->inputBank(), $rowClass);
    $html .= $this->wrapLabelTextbox($this->inputRegion(), $rowClass);
    $html .= $this->wrapLabelTextbox($this->inputDescription(), $rowClass);
    $html .= $this->wrapLabelTextbox($this->inputNotes(), $rowClass);
    $html .= $this->wrapLabelTextbox($this->inputStartDate(), $rowClass);
    $html .= $this->wrapLabelTextbox($this->inputRequestedDeliveryDate(), $rowClass); #$this->inputEndDate()
    $html .= $this->wrapLabelTextbox($this->inputExpiryDate(), $rowClass);
    $html .= $this->wrapLabelTextbox($this->inputURL(), $rowClass);
    $html .= $this->wrapLabelTextbox($this->inputCAR(), $rowClass);
    (strcmp($_GET["page"], "createballpark"))?
        $html .= $this->wrapLabelTextbox($this->inputProjectStatusEdit(), $rowClass) :
        $html .= $this->wrapLabelTextbox($this->inputProjectStatusCreate(), $rowClass);         
    $html .= '</fieldset>';
    return $html;
}


private function inputBookingRef($values){ 
    $values = $this->bookingRef;
    $html = "";
    $values= "";
    $html .= '<label for="ref">Booking Ref: </label>';
    $html .= HTML::inputText("ref", 20, $values) . PHP_EOL;
    return $html;
}

Ah I see, line 20, change:
$values = $max; to $values = $row;.
Also, I think line 27 will give a problem:
$this->inputBookingRef($values, true), because the
inputBookingRef() function takes only one parameter,
as it is in the commented out line 28:
$this->inputBookingRef($max)
Hope this helps, happy coding :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.