0

在我的数据库中,我有:

行 ID - 驱动程序 ID - 日志 ID。

行 ID 是唯一的并且自动递增。我想要的是日志 ID 对于具有该驱动程序 ID 的每一行都是唯一的。

例如,插入驱动程序 ID 为 1 的行,我希望该行的日志 ID 为 1,但下次插入驱动程序 ID 为 1 的行时,我希望它的日志 ID 为 2。

我怎样才能做到这一点?

顺便说一句,我使用的是 PHPMyAdmin。

- - - - - - - - 编辑 - - - - - - - - - - -

这就是我现在在我的 PHP 中所拥有的,但它说:

在网页上:不正确的整数值:第 1 行的“FinesCost”列的“”

我转储变量并得到这个: string(2) "16" string(2) "16" string(2) "16" 所以我不明白为什么它说不正确的整数值以及为什么说它们是未定义的,因为它们的定义非常明确。

在 PHP 错误日志中:[19-Jul-2013 10:44:18 Europe/Minsk] PHP Notice: Undefined variable: FinesCostP‌ost2 in C:\inetpub\wwwroot\hosting\Dan\JWT\drivers-log-send.php on第 336 行 [2013 年 7 月 19 日 10:44:18 Europe/Minsk] PHP 通知:未定义变量:C:\inetpub\wwwroot\hosting\Dan\JWT\drivers-log-send.php 中的 TravelExpensesPo‌​st2 第 336 行

///PHP 将驾驶员银行详细信息插入银行数据库

session_start(); 

$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="jwtdriversbank"; // Table name

$un = "";
$usrname = "";
$usrpass = "";
$userID = "";

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

if(isset ($_SESSION['usrName']))
{
    $usrname = $_SESSION['usrName'];
}
else
{
    echo "4";
}

//var_dump ($usrname);

if(isset ($_SESSION['usrPass']))
{
    $usrpass = $_SESSION['usrPass'];
}
else
{
    echo "5";
}

$sql="SELECT * FROM jwtdrivers WHERE username='$usrname' and password='$usrpass'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);

$userID = $rows['id'];

//var_dump ($userID);

if($userID == "")
{
    echo "3";
}
else
{

            $TotalProfitPost = $TotalProfit;
    $LateFeePost = $LateFee;
    $FinesCostPost2 = $FinesCost;
    $TravelExpensesPost2 = $TravelExpenses;
    $FuelCostPost = $FuelCost;
    $CargoDamagePost = $CargoDamage;
    $TruckDamagePost = $TruckDamage;

    var_dump ($TotalProfitPost);
    var_dump($FinesCostPost2);
    var_dump($TravelExpensesPost2);

    $sql="INSERT INTO jwtdriversbank2 (DriverID, LogID, TotalProfit, LateFee, FinesCost, TravelExpenses, FuelCost, CargoDamage, TruckDamage) VALUES ('$userID', COALESCE((Select MAX(LogID) from jwtdriversbank2 tab2 where tab2.DriverID = '$userID'),0)+1,'$TotalProfitPost','$LateFeePost', '$FinesCostP‌ost2' , '$TravelExpensesPo‌​st2' ,'$FuelCostPost','$CargoDamagePost','$TruckDamagePost')";

    $result = mysql_query($sql);

    if($result)
    {
    }
    else
    {
        die(mysql_error());
    }

}
4

2 回答 2

1

A quick solution would be to use a subquery to find the maximum log (last log id) then increment it, something like this

Insert into <table_name>
values p_RowID, p_DriverID, COALESCE((Select MAX(Log_id) from <table_name> tab2 where tab2.Driver_id = p_DriverID),0)+1;

Here p_RowID and p_DriverID are the values you pass to insert into your table. The Coalesce function would check the given value and if it is NULL then it would replace it with the second parameter, in this case 0

于 2013-07-18T15:00:11.983 回答
1

Add a primary key for the two columns.

It should do the trick.

Look at this link for help

ALTER TABLE table_name
ADD CONSTRAINT pk_DriverID PRIMARY KEY (DriverID,LogID)

Do not forget to drop the first primary key because you will not need it no more.

EDIT : COMPLETE WITH THE OTHER ANSWER

Here is the code to insert your data.

Insert into <table_name>
values p_RowID, p_DriverID, COALESCE((Select MAX(Log_id) from <table_name> tab2 where tab2.Driver_id = p_DriverID),0)+1;

That should close the question.

You did not defined variable because PHP can't read them. I opened your program inside VIM editor and I found "<200c>" char inside $FineCostPost2 in the SQL query. You have to change it to make it work.

于 2013-07-18T15:00:54.450 回答