从数据库中随机获取名称
的index.php
<?php
//Database initialization
$connection = mysqli_connect("localhost", "APB", "12345678", "apb");
if (isset($_POST["button1"])) {
$number = mt_rand(1, 10);
$s = "SELECT * FROM student_list WHERE NUM='$number'";
$r = mysqli_query($connection, $s);
$row = mysqli_fetch_array($r);
$StudName = $row["NAME"];
$studClass = $row["CLASS"];
}
if (isset($_POST["b1"])) {
$number = mt_rand(1, 10);
$s = "SELECT * FROM choose WHERE sl='$number'";
$r = mysqli_query($connection, $s);
$row = mysqli_fetch_array($r);
$Name = $row["item"];
$StudName = "";
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form id="form" action="" name="form1" method="POST">
<table width="1057" border="1">
<tr>
<th scope="col">
<div align="left">MPTC FRESHER'S DAY CELEBRATION - 2017 </div>
</th>
</tr>
</table>
<p>This time the lucky one is ...</p>
<p>
<input name="button1" type="submit" id="button1" value="GENERATE" />
</div>
<div>
<input id="t1" name="textfield" type="text" value="<?php echo $StudName?>" size="35" maxlength="30" />
</div>
<p> </p>
<div>Choose One ..... </div>
<div>
<input name="Submit" type="submit" id="b1" value="1" />
<input id="b2" type="submit" name="Submit2" value="2" />
<input id="b3" type="submit" name="Submit3" value="3" />
<input id="b4" type="submit" name="Submit4" value="4" />
</div>
<div>
<input name="textfield2" value="<?php echo $Name?>" type="text" size="30" />
</div>
<p> </p>
<div>
<input type="submit" name="Submit5" value="COMPUTER" />
</div>
<div>
<input name="textfield3" type="text" size="60" />
</div>
<p> </p>
</form>
</body>
</html>
错误“注意:未定义的变量:C:\ xampp \ htdocs \ FDAY \ index.php中的名称 在第80行“
当我尝试为按钮button1和b1编写代码时,它显示错误。
答案 0 :(得分:1)
尝试更改此
<div>
<input name="Submit" type="submit" id="b1" value="1" />
...
</div>
到此
<div>
<input name="Submit" type="submit" id="b1" name="b1" value="1" />
...
</div>