<html> <head lang="en"> <meta charset="UTF-8"> <title>Online Dictionary</title> </head> <body> <img src="icon.jpg" style="margin-left: 300px"/> <h1>查询英文</h1> <form action="word.php" method="post" ;> 请输入英文:<input type="text" name="enword"/> <input type="hidden" name="type" value="search1"/> <input type="submit" value="查询"/> </form> <h1>查询中文</h1> <form action="word.php" method="post" ;> 请输入中文:<input type="text" name="chword"/> <input type="hidden" name="type" value="search2"/> <input type="submit" value="查询"/> </form> </body> </html>
<?php header("content-type:text/html;charset=utf-8"); require_once "F:/WAMP/www/Hanshunping/SqlTool.class.php"; if (isset($_POST['type'])) { echo "TYPE<br>"; $type = $_POST['type']; } else { echo "NO TYPE EXISTS<br>"; } if ($type == 'search1') { if (isset($_POST['enword'])) { $en_word = $_POST['enword']; } else { echo "BLANK input"; } //$en_word需要引号;精确查询; $sql = "select * from words where enword='" . $en_word . "' limit 0,1"; $database = "test003"; $sqlTool = new SqlTool($database); $res = $sqlTool->execute_dql($sql); if ($row = mysqli_fetch_assoc($res)) { echo $en_word . "----中文意思:" . $row['chword']; } else { echo "No WORD"; } } if ($type == 'search2') { if (isset($_POST['chword'])) { $ch_word = $_POST['chword']; } else { echo "BLANK input"; } //模糊查询;like语句; $sql = "select * from words where chword like '%" . $ch_word . "%' "; echo $sql; $database = "test003"; $sqlTool = new SqlTool($database); $res = $sqlTool->execute_dql($sql); //判断是否取出数据; if (mysqli_affected_rows($sqlTool->conn) > 0) { //因为要查询多个单词,这里用while循环;并且不再用limit; while ($row = mysqli_fetch_assoc($res)) { echo "<br>" . $ch_word . "----英文意思:" . $row['enword']; } } else { echo "nonononono"; } echo "<br>" . $ch_word; } mysqli_free_result($res);