我在php中编写了一个代码,用户可以提交数据,提交后数据将从数据库中显示但是不显示数据请帮忙
它在数据库中成功插入dta但是在提交后没有显示它请帮助哪里有我不知道的问题
这是代码
<html>
<body>
<?php
// This file provides the information for accessing the database.and connecting
//to MySQL. It also sets the language coding to utf-8
// First we define the constants:
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', '*****');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'test');
// Next we assign the database connection to a variable that we will call $dbcon:
$dbcon = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die ('Could not connect to MySQL: ' . mysqli_connect_error());
// Finally, we set the language encoding.as utf-8
$name=mysqli_real_escape_string($dbcon,$_POST["name"]);
$age=mysqli_real_escape_string($dbcon,$_POST["age"]);
$add="INSERT INTO demo (name,age) VALUES ('".$name."','".$age."')";
$result=@mysqli_query($dbcon,$add);
if($result==TRUE)
{
exit();
}
$query="SELECT * FROM demo";
$rst=mysql_query($query);
$num=mysql_num_rows($rst);
mysql_close();
?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td>
<font face="Arial, Helvetica, sans-serif">name</font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif">age</font>
</td>
</tr>
<?php$i=0;
while ($i < $num) {
$f1=mysql_result($rst,$i,name);
$f2=mysql_result($rst,$i,age);
$i++;
}
?>
<tr>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font>
</tr>
</body>
</html>
答案 0 :(得分:2)
mysqli_query
true
会返回INSERT
。所以在这种情况下:
$result=@mysqli_query($dbcon,$add);
if($result==TRUE)
{
exit();
}
如果查询成功,您告诉PHP退出脚本。由于您的数据已成功存储在数据库中,因此在此之后将停止执行脚本,因此不会显示您的数据。