我有一個應用程序,您可以查看here。當你打開應用程序時,你會看到一個提交按鈕,只需點擊按鈕,你會看到它會在下面生成字符串。如果您不斷點擊按鈕,它會在字母「A」和「B」之間始終生成2個字母的字符串。如果數據庫中的字符串包含數字結尾,它仍然不應該生成字符串在PHP
如果在數據庫下的「SessionId」列中,我有一行字符串「AB」。然後,當我繼續點擊提交按鈕時,它不會生成字符串「AB」。我已經測試過這個,這工作正常。所以如果字符串最後不包含數字,這不是問題。
我有這個問題。如果我想創建多個考試。假設字符串是「AA」,並且我有3個考試,它不會在數據庫中插入字符串「AA」,因爲有多個「AA」,所以它會在數據庫中的每個「AA」之後連接一個數字,以便它顯示爲「AA1」,「AA2」和「AA3」。
但問題是,如果這樣做,那麼當你再次使用該應用程序,並保持點擊「提交」按鈕,你會發現它仍然會生成字符串「AA」時,它不應該作爲字符串「AA 「在數據庫中用作」AA1「,」AA2「和」AA3「。我目前在數據庫中有這三個值,但它仍然在應用程序中生成字符串。
所以我的問題是如何在應用程序中生成字符串時如何阻止它生成字符串,如果字符串在數據庫中以數字連接在數據庫的末尾?
下面是應用程序的代碼:
<?php
// connect to the database
include('connect.php');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
function id_generator(){
$id = "";
$a = array("A" , "B", "C");
for($i = 0 ; $i < 2 ; $i++){ $r = rand(0 , 1);
$id .= $a[ $r ];
}
return $id;
};
$is_there = true;
while($is_there){
$id = id_generator(); // your function to generate id;
$result = "SELECT SessionId FROM Session WHERE SessionId LIKE CONCAT(?, '%')";
$stmt=$mysqli->prepare($result);
// You only need to call bind_param once
$stmt->bind_param("1",$id);
// execute query
$stmt->execute();
// get result and assign variables (prefix with db)
$stmt->bind_result($dbSessionId);
$stmt->store_result();
$stmtnum = $stmt->num_rows();
if($stmtnum == 0) {
$is_there = false;
}
}
?>
<h1>CREATING A NEW ASSESSMENT</h1>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>
<input id="courseSubmit" type="submit" value="Submit" name="submit" />
</p>
</form>
<?php
if (isset($_POST['submit'])) {
?>
<br/>
<form action="QandATable.php" method="post" id="sessionForm">
<p><strong>1: Your Assessment ID: </strong><span id="idFont"><?php echo $id; ?></span></p>
<input type='hidden' name='id' value='<?php echo $id; ?>' />
</form>
<?php
}
?>
</body>
</html>
您可以在SQL語句後放置「echo $ result」並顯示輸出(應該是SQL語句)。我的猜測是這將告訴故事。 – Lock