修复后的代码如下:

<?php
$chengyu = $_GET['chengyu'];

$con = mysqli_connect("localhost", "s0811030", "xwDO18fsYS");
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con, "s0811030");
mysqli_set_charset($con, "utf8");

$result = mysqli_query($con, "SELECT * FROM chengyudaquan WHERE chengyu = '$chengyu'");
$row = mysqli_fetch_assoc($result);

mysqli_query($con, "UPDATE chengyudaquan SET view = view + 1 WHERE chengyu = '$chengyu'");

?>

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>成语大全</title>
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
		<style>
			body {
				font-family: Arial, sans-serif;
				margin: 0;
				padding: 20px;
				max-width: 500px;
				display: block;
				margin-left: auto;
				margin-right: auto;
			}
			
			form {
				margin-bottom: 20px;
			}
			
			input[type="text"] {
				padding: 5px;
				width: 200px;
			}
			
			input[type="submit"] {
				padding: 5px 10px;
			}
			
			.container {
				padding: 20px;
				border: 1px solid #ccc;
				border-radius: 5px;
			}
			
			h1 {
				margin-top: 0;
			}
			
			.idiom {
				margin-bottom: 20px;
				border: 1px solid #ccc;
				border-radius: 5px;
				padding: 10px;
				background-color: #f7f7f7;
			}
			
			.idiom p {
				margin: 0;
			}
			
			.idiom p:first-child {
				font-weight: bold;
				font-size: 18px;
			}
			
			.idiom p:last-child {
				margin-top: 10px;
			}
		</style>
	</head>
	<body>
		<div align="center">
			<form method="get" action="index.php">
				<input name="chengyu" type="text"/>
				<input type="submit"/>
			</form>
		</div>
		<div class="container">
			<?php if ($row) { ?>
				<div class="idiom">
					<p><?php echo $row['chengyu']; ?>(<?php echo $row['pinyin']; ?>)</p>
					<p>解释:<?php echo $row['jieshi']; ?></p>
					<p>来源:<?php echo $row['laiyuan']; ?></p>
					<p>举例:<?php echo $row['shili']; ?></p>
				</div>
			<?php } else { ?>
				<p>没有找到相关成语。</p>
			<?php } ?>
		</div>
	</body>
</html>

修复的部分主要包括:

  1. 将数据库连接的用户名和密码替换为正确的值。
  2. 在查询数据库之前设置字符集为utf8,避免中文乱码。
  3. 添加对查询结果是否为空的判断,如果没有找到相关成语,则显示一条提示信息
中文设置修复php$chengyu = $_GETchengyu;$con = mysqli_connectlocalhost s0811030 xwDO18fsYS;if !$con dieCould not connect mysqli_error$con;mysqli_select_db$con s0811030;mysqli_set_charset$con utf8;$resu

原文地址: https://www.cveoy.top/t/topic/iN0I 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录