0

我的查询有问题,没有获取最大记录,也没有增加。你能帮我弄清楚我的代码有什么问题吗?

 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) FROM `ballpark_details`");
    //Getting the max ref_id
    $values = mysql_fetch_assoc($query);    
    $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['ballpark_details_booking_ref']), $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($value){ //($value) {
    //$value = $this->bookingRef;
    $html = "";
    //$value = 10307;
    $value++;
    $html .= '<label for="ref">Booking Ref: </label>';
    $html .= HTML::inputText("ref", 20, $value) . PHP_EOL;
    return $html;
}

在这里,我想增加预订参考中的价值。文本框。但是当我尝试运行程序时,它给出的数字是 1 而不是最后一个最大值。

4

2 回答 2

0

您必须更改$values['ballpark_details_booking_ref']$values['max(ballpark_details_booking_ref)'].

您还可以为查询添加别名并将其用作结果的索引

SELECT MAX(ballpark_details_booking_ref) as max_booking_ref FROM `ballpark_details`

进而

$html .= $this->wrapLabelTextbox($this->inputBookingRef($values['max_booking_ref']), $rowClass);
于 2012-11-19T07:19:05.580 回答
0
$values = mysql_fetch_assoc($query); 

$values不包含 MAX 的值。

所以你将不得不使用。

$query = "SELECT MAX(ballpark_details_booking_ref) AS MAXValue FROM `ballpark_details`";

$result = mysql_query($query, $conn) or die ("The Following query is not properly executed: ".$query."<br>");

//Getting the max ref_id

while ($result !=-1 && $row = mysql_fetch_assoc($result)) {
$max = $row['MAXValue'];
}

然后

$html .= $this->wrapLabelTextbox($this->inputBookingRef($max), $rowClass);
于 2012-11-19T07:26:00.547 回答