我是php的超新手,目前在練習寫留言板,程式碼有些是參考網路上的,但show.php在讀取資料的過程中都會出現Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in,送出留言也是失敗,不過讀取資料庫的時候是可以的,爬了很多文還是無法解決,於是想上來請這裡的大大。
這是我的程式碼:
p13-post.php
<?php
require("connect2.php");
$guestName=$_POST['guestName'];
$guestEmail=$_POST['guestEmail'];
$guestGender=$_POST['guestGender'];
$guestSubject=$_POST['guestSubject'];
$guestContent=$_POST['guestContent'];
$guestTime = date("Y:m:d H:i:s",time()+28800);
if(isset($guestName)){
mysql_query("insert into guest value('','$guestName','$guestEmail','$guestGender','$guestSubject','$guestTime','$guestContent','','')");
header("location:show.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>我要留言</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
</head>
<style>
.container{
margin:auto;
background-color:#f5f5f5;
width:800px;
padding-bottom: 20px;
}
.button{
text-align:center;
padding:20px 0;
}
.top h3{
font-family:微軟正黑體;
text-align:center;
padding:10px 0;
}
.form-group{
font-family:微軟正黑體;
font-size:16px;
}
</style>
<body>
<div class="container">
<div class="top">
<h3>新增留言</h3>
</div>
<form id="form1" name="form1" method="post" action="" class="form-horizontal">
<div class="form-group">
<label for="guestName" class="col-sm-4 control-label">暱稱:</label>
<div class="col-sm-6">
<input type="text" class="form-control" placeholder="您的暱稱" name="guestName" id="guestName" />
</div>
</div>
<div class="form-group">
<label for="guestEmail" class="col-sm-4 control-label">信箱:</label>
<div class="col-sm-6">
<input type="text" class="form-control" placeholder="您的信箱" name="guestEmail" id="guestEmail" />
</div>
</div>
<div class="form-group">
<label for="guestGender" class="col-sm-4 control-label">性別:</label>
<label class="radio-inline">
<input type="radio" name="guestGender" id="radio" value="男" /> 男
</label>
<label class="radio-inline">
<input type="radio" name="guestGender" id="radio2" value="女" />女
</label>
</div>
<div class="form-group">
<label for="guestSubject" class="col-sm-4 control-label">留言主旨:</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="guestSubject" id="guestSubject" />
</div>
</div>
<div class="form-group">
<label for="guestContent" class="col-sm-4 control-label">留言內容:</label>
<div class="col-sm-6">
<textarea name="guestContent" class="form-control" id="guestContent" rows="5"></textarea>
</div>
</div>
<div class="button">
<input type="submit" name="button" id="button" value="送出" class="btn"/>
</div>
</form>
</div>
</body>
</html>
connect2.php
<?php
function use_mysql($query)
{
$con=mysqli_connect("localhost","root","","guest");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
}
?>
show.php
<?php
require("connect2.php");
$data=mysql_query('select * from guest order by guestTime desc')//讓資料由最新呈現到最舊
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<title>自製留言板</title>
<style>
.top{
margin:auto;
width:60vw;
text-align:right;
padding:15vh 0 0 0;
font-family:微軟正黑體;
}
/*.nav{
background-color:#339;
padding: 10px 0px;
}*/
.nav a {
color: #5a5a5a;
font-size: 11px;
font-weight: bold;
text-transform: uppercase;
}
.nav li {
display: inline;
}
.CSSTableGenerator {
margin:auto;
padding:0px;
width:60vw;
}
.CSSTableGenerator table{
border-collapse: collapse;
border-spacing: 0;
width:100%;
height:100%;
margin:0px;padding:0px;
}.CSSTableGenerator tr:last-child td:last-child {
-moz-border-radius-bottomright:9px;
-webkit-border-bottom-right-radius:9px;
border-bottom-right-radius:9px;
}
.CSSTableGenerator table tr:first-child td:first-child {
-moz-border-radius-topleft:9px;
-webkit-border-top-left-radius:9px;
border-top-left-radius:9px;
}
.CSSTableGenerator table tr:first-child td:last-child {
-moz-border-radius-topright:9px;
-webkit-border-top-right-radius:9px;
border-top-right-radius:9px;
}.CSSTableGenerator tr:last-child td:first-child{
-moz-border-radius-bottomleft:9px;
-webkit-border-bottom-left-radius:9px;
border-bottom-left-radius:9px;
}.CSSTableGenerator tr:hover td{
background-color:#005fbf;
color:white;
}
.CSSTableGenerator td{
vertical-align:middle;
background-color:#e5e5e5;
border:1px solid #999999;
border-width:0px 1px 1px 0px;
text-align:left;
padding:8px;
font-size:16px;
font-family:Arial,微軟正黑體;
font-weight:normal;
color:#000000;
}.CSSTableGenerator tr:last-child td{
border-width:0px 1px 0px 0px;
}.CSSTableGenerator tr td:last-child{
border-width:0px 0px 1px 0px;
}.CSSTableGenerator tr:last-child td:last-child{
border-width:0px 0px 0px 0px;
}
.CSSTableGenerator tr:first-child td{
background:-o-linear-gradient(bottom, #005fbf 5%, #005fbf 100%);
background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #005fbf), color-stop(1, #005fbf) );
background:-moz-linear-gradient( center top, #005fbf 5%, #005fbf 100% );
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr="#005fbf", endColorstr="#005fbf");
background: -o-linear-gradient(top,#005fbf,005fbf);
background-color:#005fbf;
text-align:center;
font-size:20px;
font-family:Arial, 微軟正黑體;
font-weight:bold;
color:#ffffff;
}
.CSSTableGenerator tr:first-child:hover td{
background:-o-linear-gradient(bottom, #005fbf 5%, #005fbf 100%);
background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #005fbf), color-stop(1, #005fbf) );
background:-moz-linear-gradient( center top, #005fbf 5%, #005fbf 100% );
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr="#005fbf", endColorstr="#005fbf");
background: -o-linear-gradient(top,#005fbf,005fbf);
background-color:#005fbf;
}
</style>
</head>
<body>
<div class="top">
<a href="p13-post.php"><button type="button" class="btn btn-primary btn-lg">我要留言</button></a>
</div>
<p> </p>
<p> </p>
<?php
for($i=1;$i<=mysql_num_rows($data);$i++){
$rs=mysql_fetch_assoc($data);
?>
<div class="container">
<div class="CSSTableGenerator">
<table align="center">
<tr>
<td><?php echo $rs['guestSubject']?></td>
</tr>
<tr>
<td width="25%">暱稱</td>
<td width="75%"><?php echo $rs['guestName']?></td>
</tr>
<tr>
<td>信箱</td>
<td><?php echo $rs['guestEmail']?></td>
</tr>
<tr>
<td>性別</td>
<td><?php echo $rs['guestGender']?></td>
</tr>
<tr>
<td>留言內容</td>
<td><?php echo $rs['guestContent']?></td>
</tr>
</table>
</div>
</div>
<br />
<?php } ?>
</body>
</html>
拜託各位大大了
$sql = 'statement';
$con = mysql_connect('host', 'act', 'pwd');
$query = mysql_query($sql, $con);
$res = mysql_fetch_assoc($query);