我有一个应用程序,您可以查看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