php和mysqli代码不会将数据输入我的数据库..详细信息如下

时间:2015-05-22 13:08:06

标签: php database mysqli

完整代码分为两部分,但一个接一个地写在同一个php文件中

数据库信息如下:

  • 数据库名称:forumTutorial
  • 表名:threads
  • 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;
&#13;
&#13;

&GT;

1 个答案:

答案 0 :(得分:0)

有两个问题: -

  1. mysqli_ *与mysql _ *
  2. 的混合

    更改

    $con = mysqli_connect('localhost', 'root', '', 'forumTutorial') or die(mysql_error());
    

    $con = mysqli_connect('localhost', 'root', '', 'forumTutorial') or die(mysqli_error());
    
    1. INSERT查询不正确: -
    2. 更改

       $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));