php中的主键和外键

时间:2012-03-15 08:31:57

标签: php mysql

大家好我真的需要一些关于这些主键和外键的帮助。我试过寻找潜在客户,但我无法弄明白。请在这里帮助我,如果在

之前提出这个问题,我很抱歉

我有2张桌子和他们的专栏

客户端 id(pk)|组织|描述|令牌

交易 trx_id | c_id(fk)| trx_description | trx_date |行动| no_of_token

在事务表中,其中的所有值都由我通过php myadmin手动键入,甚至选择c_id即外键。所以我的问题是例如我创建一个名为ccc的新组织,我想要它在事务表中自动分配给3的c_id。每次更新标记时,它应该在trasaction表中显示为trx_id(我已将其设置为自动增量)  但是c_id应该与我选择的组织相匹配并相应地增加行.........希望你们能快速帮助我,如果我让你困惑,我很抱歉......如果你们想要,我可以发布代码:)

下面的脚本添加了客户端

<?php 

include 'connection.php';

// Get values from form
$organisation=$_POST['organisation'];
$description=$_POST['description'];
$token=$_POST['token'];


// Insert data into mysql
$sql="INSERT INTO client(organisation, description, token)VALUES('$organisation', '$description', '$token')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='login_success.php'>Back to main page</a>";
}

else {
echo "ERROR";
}

// close connection
mysql_close();
?>

交易表脚本

    <?php 

include 'connection.php';

?>  
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
</head>
<body>
<form action="add.php?id=<?=$_GET["id"];?>" name="frmEdit" method="POST">
<?
$strSQL = "SELECT * FROM client WHERE id = '".$_GET["id"]."' ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
if(!$objResult)
{
echo "Not found id=".$_GET["id"];
}
else
{
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">id </div></th>
<th width="160"> <div align="center">organisation </div></th>
<th width="198"> <div align="center">current token </div></th>
</tr>
<tr>
<td><?=$objResult["id"];?></div></td>
<td><?=$objResult["organisation"];?></td>
<td><?=$objResult["token"];?></td>
</tr>
</table>

How many tokens do you want to add to this organisation: <input type="text" id="newToken" name="newToken" /><br/>
Please fill up the reason for adding the tokens : <input type="text" id="reason" name="reason" /><br/>
<input type="submit" name="submit" value="submit">

<? 

$newToken = isset($_POST['newToken']) ? $_POST["newToken"] : "";
$reason = isset($_POST['reason']) ? $_POST["reason"] : "";


$sql="UPDATE client SET token = token + '$newToken' WHERE id = '".$_GET["id"]."' ";

$result=mysql_query($sql) or die ("Error Query [".$sql."]");
?>

<?

if ($newToken == true)
{

mysql_query("START TRANSACTION");
$date = date("Y-m-d H:i:s");

$query_string_count = "SELECT count(*) AS num FROM transaction WHERE c_id =" . $_GET["id"]; 
  $query = mysql_query($query_string_count);
  $result = mysql_fetch_array($objQuery);

  $num = $objResult["num"];

  INSERT INTO `transaction` (`trx_id`, `c_id`, `trx_description`, `trx_date`, `action`, `no_of_token`) VALUES ($num + 1, $_GET["id"], '$reason', '".$date."', 'Add token', '$newToken')";

  mysql_query("COMMIT TRANSACTION");





?>

<?
header("Location:login_success.php"); 
}
else {



}
?>

<?

}
mysql_close();

?>
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

问题是您将trx_id设置为auotoincrement。 如果你需要保持每个客户端的transcation数量,那么trx_id不应该是自动增量。

例如,每次在事务表上插入时:

1:开始新的交易 2:number =计算具有c_id感兴趣的客户端的id的数字事务 3:插入到事务中并设置trx_id = number + 1 4:提交交易

注意:在这种情况下,由于您拥有更多客户端,因此trx_id无法唯一。如果需要,可以在事务表中插入一个新列,即primary_key,在本例中为autoincrement

这里是我所描述的代码快照:

    //new db transaction
  mysql_query("START TRANSACTION");

  // count number of transaction for clients $_GET["id"]
  $query_string_count = "SELECT count(*) AS num FROM transactions WHERE c_id =" . $_GET["id"]; 
  $query = mysql_query($query_string_count);
  $result = mysql_fetch_array($objQuery);

  $num = $objResult["num"];

  //insert new transaction for client $_GET["id"] with id $num + 1
  INSERT INTO `transaction` (`trx_id`, `c_id`, `trx_description`, `trx_date`, `action`, `no_of_token`) VALUES ($num + 1, $_GET["id"], '$reason', '".$date."', 'Add token', '$newToken')";

   //COMMIT -> persist on db
   mysql_query("COMMIT TRANSACTION");