gpt4 book ai didi

PHP 问题,不从数据库回显数据

转载 作者:行者123 更新时间:2023-11-29 07:23:13 25 4
gpt4 key购买 nike

使用以下 PHP 代码,我想回显从某些 MySQL 表中获取的数据:

<html>

<head>
<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Search Results</title>
</head>

<body bgcolor="#EFEFEF">
<table border="0" align=center width="95%" id="table1" bgcolor="#FFFFFF">
<tr>
<td>
<img border="0" src="index_files/text827854609.gif" width="260" height="67"></td>
</tr>
<tr>
<td><hr color="#C0C0C0" size="4"></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td>
<p align="left"><font face="Verdana" size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Search Student Name For Results</font></td>
</tr>
<tr>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
</tr>
<tr>
<td>
<form method="POST" name="Informationform" action="searchresults.php">
<!--webbot bot="SaveResults" U-File="fpweb:///_private/form_results.csv" S-Format="TEXT/CSV" S-Label-Fields="TRUE" B-Reverse-Chronology="FALSE" S-Builtin-Fields -->
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b><font face="Verdana" size="2">Enter Name in Full</font></b> :&nbsp;
<input type="text" name="fullname" size="50">
<input type="submit" value="Search" name="senda"></p>
</form>
<?php
if(isset($_POST['senda'])){
include 'mysqlconn.php';
$con = mysqli_connect($host, $dbuser, $pass, $db) or die('Cannot Connect');
$name = mysql_escape_string($con,$_POST['fullname']);

$sql = "SELECT * FROM scores WHERE fullname = '$name'";
$result = mysqli_query($con,$sql) or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
echo $tablez;
}mysqli_free_result($result);
}
?>
</td>
</tr>
<tr>
<td>
$tablez = "<table border=0 width=99% id=table2>
<tr>
<td width=299 align=center><b>
<font face=Verdana size=2 color=#808080>Full Name</font></b></td>
<td width=171 align=center><b>
<font face=Verdana size=2 color=#808080>studentNo</font></b></td>
<td width=276 align=center><b>
<font face=Verdana size=2 color=#808080>SubjectName</font></b></td>
<td width=106 align=center><b>
<font face=Verdana size=2 color=#808080>GPA</font></b></td>
<td width=176 align=center><b>
<font face=Verdana size=2 color=#808080>CGPA</font></b></td>
<td align=center><b>
<font face=Verdana size=2 color=#808080>SCORE</font></b></td>
</tr>
<tr>
<td width=299 align=center> . $row['Fullname'] . </td>
<td width=171 align=center>. $row['studentNo']. </td>
<td width=276 align=center>. $row['SubjectName'].</td>
<td width=106 align=center> .$row['GPA']. </td>
<td width=176 align=center>.$row['CGPA']. </td>
<td align=center> .$row['SCORE'].</td>
</tr>
</br></table>";
</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td><hr color="#808080" size="1">
<p>
<font face="arial" class="ws7" style="font-size: 9.3px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(212, 212, 212);">
EM Software Limited Nigeria<span class="Apple-converted-space">&nbsp;</span></font><font class="ws7" style="font-size: 9.3px; color: rgb(0, 0, 0); font-family: arial; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(212, 212, 212)">©</font><font face="arial" class="ws7" style="font-size: 9.3px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(212, 212, 212);"><span class="Apple-converted-space">&nbsp;</span>2016
All rights Reserved.</font></td>
</tr>
</table>
</body>

</html>

但是,它并没有按照我的预期工作。

这是我得到的输出:

enter image description here

如何从数据库中回显数据?

最佳答案

变量$tablez应该在循环内定义,因为它从记录集中获取内容。由于它是在循环之后声明的,因此您很可能会收到某种错误

为了避免重复表头,请在循环外部定义/echo,并仅在循环内部重复表行。

如果您使用样式表,则与使用内联样式相比,维护代码会容易得多,这样您就可以从各种 html 元素中删除所有样式,并使用一个外部文件来管理外观和布局。

$tableheader="
<table border='0' width='99%' id='table2'>
<tr>
<td width='299px' align='center'><b>
<font face='Verdana' size='2' color='#808080'>Full Name</font></b></td>
<td width='171px' align='center'><b>
<font face='Verdana' size='2' color='#808080'>studentNo</font></b></td>
<td width='276px' align='center'><b>
<font face='Verdana' size='2 'color='#808080'>SubjectName</font></b></td>
<td width='106px' align='center'><b>
<font face='Verdana' size='2' color='#808080'>GPA</font></b></td>
<td width='176px' align='center'><b>
<font face='Verdana' size='2' color='#808080'>CGPA</font></b></td>
<td align='center'><b>
<font face='Verdana' size='2' color='#808080'>SCORE</font></b></td>
</tr>";
$tablefooter='</table>';


echo $tableheader;

while( $row = mysqli_fetch_array( $result, MYSQLI_ASSOC ) ){

$tablerows = "
<tr>
<td width='299px' align='center'>" . $row['Fullname'] . "</td>
<td width='171px' align='center'>". $row['studentNo']. "</td>
<td width='276px' align='center'>". $row['SubjectName']."</td>
<td width='106px' align='center'>" .$row['GPA']. "</td>
<td width='176px' align='center'>".$row['CGPA']. "</td>
<td align='center'>" .$row['SCORE']."</td>
</tr>";
/* echo the table row */
echo $tablerows;
}

echo $tablefooter;/* close the table */
mysqli_free_result( $result );

关于PHP 问题,不从数据库回显数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35378500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com