完整代码分为两部分,但一个接一个地写在同一个php文件中
数据库信息如下:
5列:
id - INT - 5长度 - 主键 - 自动增量(AI / A_I)。
title - VARCHAR - 255长度。
内容 - VARCHAR - 2000长度。
作者 - VARCHAR - 255长度。
标签 - VARCHAR - 255长度。
$q = mysqli_query($con, "INSERT INTO `threads` VALUES ('', '$title', '$content', '$user', '0', '0', '$tags')") or die(mysql_error());
if ($q) {
echo 'Thread created.';
}else
echo 'Failed to create thread.';
}
}
$ news ='&#39 ;;
$ nq = mysqli_query($ con," SELECT * FROM(SELECT * FROM` threads` ORDER BY`id`DESC LIMIT 3)t ORDER BY`id` ASC");
if(mysqli_num_rows($ nq)> 0){
while($ row = mysqli_fetch_array($ nq)){
$ news。=''。$ row [" title"]。''。$ row [" content"]。&# 39;&#39 ;;
}
}> PHP文件在代码段中如下:
<html>
<head></head>
<body>
<h1>Search:</h1>
<form action='searchThreads.php' method='POST'>
<table border='0'>
<tbody>
<tr>
<td><input type='text' name='searchQuery' /></td>
</tr>
<tr>
<td><input type='submit' value='Search' name='searchSent' /></td>
</tr>
</tbody>
</table>
</form>
<h1>Create Thread:</h1>
<form action='forumTutorial.php' method='POST'>
<table>
<tbody>
<tr>
<td>Title: </td><td><input type='text' name='title' /></td>
</tr>
<tr>
<td>Description: </td><td><input type='text' name='content' /></td>
</tr>
<tr>
<td>Tags: </td><td><input type='text' name='tags' /></td><td>Tags must be separated by a comma (,)</td>
</tr>
<tr>
<td></td><td><input type='submit' value='Create Thread' name='createThread' /></td>
</tr>
</tbody>
</table>
</form>
<br/>
<h1>Threads:</h1>
<table>
<tbody>
<?php
$qu = mysqli_query($con, "SELECT * FROM `threads`");
if (mysqli_num_rows($qu) > 0) {
while ($row = mysqli_fetch_array($qu)) {
$content = $row['content'];
if (strlen($content) > 100) {
$a = $content;
$content = '';
for($i=0;$i<100;$i++) {
$content .= $a[$i];
}
}
echo '<tr><td><a href="threadPage.php?tid='.$row["id"].'">'.$row["title"].'</td><td>'.$content.'...</td></tr>';
}
}else{
echo '<tr><td>No threads found :(</tr></td>';
}
?>
</tbody>
</table>
<br/>
<table>
<tbody>
<?php echo $news; ?>
</tbody>
</table>
</body>
</html>
&#13;
&GT;
答案 0 :(得分:0)
有两个问题: -
更改
$con = mysqli_connect('localhost', 'root', '', 'forumTutorial') or die(mysql_error());
到
$con = mysqli_connect('localhost', 'root', '', 'forumTutorial') or die(mysqli_error());
INSERT
查询不正确: - 更改
$q = mysqli_query($con, "INSERT INTO `threads` VALUES ('', '$title', '$content', '$user', '0', '0', '$tags')") or die(mysql_error());
到
$q = mysqli_query($con, "INSERT INTO threads (put your column names ) VALUES ('', '$title', '$content', '$user', '0', '0', '$tags')") or die(mysqli_error($con));