2016-04-04 56 views
1

我試圖給相應的問題ID顯示相應的評論,但所有評論都顯示在每個帖子中。 實際上qusetion ID的值是不會期望的表,其中它假設是和該表如何顯示正確的值?

name: add_topic.php 
 

 
<body> 
 

 
<?php 
 

 
$con=mysqli_connect('localhost','root',''); 
 

 
if(!$con) 
 
{ 
 
die("could not connect to the server".mysqli_error()); 
 
} 
 
mysqli_select_db($con,'forum'); 
 

 

 
// Get value of id that sent from hidden field 
 
$id=$_POST['id']; 
 

 
// Find highest answer number. 
 
$q="SELECT MAX(a_id) AS Maxa_id FROM forum_ans WHERE que_id='$id'"; 
 
$result=mysqli_query($con,$q); 
 
$row=mysqli_fetch_assoc($result); 
 

 
// add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1 
 
if ($row) { 
 
$Max_id = $row['Maxa_id']+1; 
 
} 
 
else { 
 
$Max_id = 1; 
 
} 
 

 
// get values that sent from form 
 

 
$a_name=$_POST['a_name']; 
 
$a_email=$_POST['a_email']; 
 
$a_ans=$_POST['a_ans']; 
 

 
$datetime=date("d/m/y H:i:s"); // create date and time 
 

 
// Insert answer 
 
$q2="INSERT INTO forum_ans(que_id, a_id, a_name, a_email, a_ans, a_datetime)VALUES('$id', '$Max_id', '$a_name', '$a_email', '$a_ans', '$datetime')"; 
 
$result2=mysqli_query($con,$q2); 
 

 
if($result2){ 
 
echo "Successful<BR>"; 
 
echo "<a href='view_topic.php?id=".$id."'>View your answer</a>"; 
 

 
// If added new answer, add value +1 in reply column 
 

 
$q3="UPDATE forum_que SET reply='$Max_id' WHERE id='$id'"; 
 
$result3=mysqli_query($con,$q3); 
 
} 
 
else { 
 
echo "ERROR"; 
 
} 
 

 
// Close connection 
 
mysqli_close($con); 
 
?> 
 

 
</body>

塊命名爲que_id是示出0值(如我有設置0作爲它的默認值),這是它的主要原因,但我不能解決這個問題。 plz幫助...

name: view_topic.php 
 

 

 
<body> 
 
<?php 
 

 
$con=mysqli_connect('localhost','root',''); 
 
if(!$con) 
 
{ 
 
die("could not connect to the server".mysqli_error()); 
 
} 
 

 
mysqli_select_db($con,'forum'); 
 

 

 

 
// get value of id that sent from address bar 
 
$id=$_GET['id']; 
 
$q="SELECT * FROM forum_que WHERE id='$id'"; 
 

 
$result=mysqli_query($con,$q); 
 

 
$row=mysqli_fetch_assoc($result); 
 
?> 
 

 
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> 
 
<tr> 
 
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bordercolor="1" bgcolor="#FFFFFF"> 
 
<tr> 
 
<td bgcolor="#F8F7F1"><strong><?php echo $row['topic']; ?></strong></td> 
 
</tr> 
 

 
<tr> 
 
<td bgcolor="#F8F7F1"><?php echo $row['detail']; ?></td> 
 
</tr> 
 

 
<tr> 
 
<td bgcolor="#F8F7F1"><strong>By :</strong> <?php echo $row['name']; ?> <strong>Email : </strong><?php echo $row['email'];?></td> 
 
</tr> 
 

 
<tr> 
 
<td bgcolor="#F8F7F1"><strong>Date/time : </strong><?php echo $row['datetime']; ?></td> 
 
</tr> 
 
</table></td> 
 
</tr> 
 
</table> 
 
<BR> 
 

 
<?php 
 

 
// Switch to table "forum_answer" 
 
$q2="SELECT * FROM forum_ans WHERE que_id='$id'"; 
 
$result2=mysqli_query($con,$q2); 
 
while($row2=mysqli_fetch_assoc($result2)){ 
 
?> 
 

 
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> 
 
<tr> 
 
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> 
 
<tr> 
 
<td bgcolor="#F8F7F1"><strong>ID</strong></td> 
 
<td bgcolor="#F8F7F1">:</td> 
 
<td bgcolor="#F8F7F1"><?php echo $row2['a_id']; ?></td> 
 
</tr> 
 
<tr> 
 
<td width="18%" bgcolor="#F8F7F1"><strong>Name</strong></td> 
 
<td width="5%" bgcolor="#F8F7F1">:</td> 
 
<td width="77%" bgcolor="#F8F7F1"><?php echo $row2['a_name']; ?></td> 
 
</tr> 
 
<tr> 
 
<td bgcolor="#F8F7F1"><strong>Email</strong></td> 
 
<td bgcolor="#F8F7F1">:</td> 
 
<td bgcolor="#F8F7F1"><?php echo $row2['a_email']; ?></td> 
 
</tr> 
 
<tr> 
 
<td bgcolor="#F8F7F1"><strong>Answer</strong></td> 
 
<td bgcolor="#F8F7F1">:</td> 
 
<td bgcolor="#F8F7F1"><?php echo $row2['a_ans']; ?></td> 
 
</tr> 
 
<tr> 
 
<td bgcolor="#F8F7F1"><strong>Date/Time</strong></td> 
 
<td bgcolor="#F8F7F1">:</td> 
 
<td bgcolor="#F8F7F1"><?php echo $row2['a_datetime']; ?></td> 
 
</tr> 
 
</table></td> 
 
</tr> 
 
</table><br> 
 
    
 
<?php 
 
} 
 

 
$q3="SELECT view FROM forum_que WHERE id='$id'"; 
 
$result3=mysqli_query($con,$q3); 
 
$row3=mysqli_fetch_assoc($result3); 
 
$view=$row3['view']; 
 
    
 
// if have no counter value set counter = 1 
 
if(empty($view)){ 
 
$view=1; 
 
$q4="INSERT INTO forum_que(view) VALUES('$view') WHERE id='$id'"; 
 
$result4=mysqli_query($con,$q4); 
 
} 
 
    
 
// count more value 
 
$addview=$view+1; 
 
$q5="update forum_que set view='$addview' WHERE id='$id'"; 
 
$result5=mysqli_query($con,$q5); 
 
mysqli_close($con); 
 
?> 
 

 
<BR> 
 
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> 
 
<tr> 
 
<form name="form1" method="post" action="add_ans.php"> 
 
<td> 
 
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> 
 
<tr> 
 
<td width="18%"><strong>Name</strong></td> 
 
<td width="3%">:</td> 
 
<td width="79%"><input name="a_name" type="text" id="a_name" size="45"></td> 
 
</tr> 
 
<tr> 
 
<td><strong>Email</strong></td> 
 
<td>:</td> 
 
<td><input name="a_email" type="text" id="a_email" size="45"></td> 
 
</tr> 
 
<tr> 
 
<td valign="top"><strong>Answer</strong></td> 
 
<td valign="top">:</td> 
 
<td><textarea name="a_ans" cols="45" rows="3" id="a_ans"></textarea></td> 
 
</tr> 
 
<tr> 
 
<td>&nbsp;</td> 
 
<td><input name="id" type="hidden" value="<? echo $id; ?>"></td> 
 
<td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td> 
 
</tr> 
 
</table> 
 
</td> 
 
</form> 
 
</tr> 
 
</table> 
 
</body>

name: add_topic.php 
 

 
<body> 
 

 
<?php 
 

 

 

 
// get data that sent from form 
 
$topic=$_REQUEST['topic']; 
 
$detail=$_REQUEST['detail']; 
 
$name=$_REQUEST['name']; 
 
$email=$_REQUEST['email']; 
 

 
$datetime=date("d/m/y h:i:s"); //create date time 
 

 
$con=mysqli_connect('localhost','root',''); 
 

 
if(!$con) 
 
{ 
 
die("could not connect to the server".mysqli_error()); 
 
} 
 
mysqli_select_db($con,'forum'); 
 

 

 
$q="INSERT INTO`forum`.`forum_que`(topic, detail, name, email, datetime)VALUES('$topic', '$detail', '$name', '$email', '$datetime')"; 
 

 
if(mysqli_query($con,$q)) 
 
{ 
 
echo '<script>alert("Data Inserted Now redirecting to login page");</script>'; 
 
header('location:main_forum.php'); 
 
} 
 
else 
 
{ 
 
echo "error inserting record" . mysqli_error($con,$query); 
 
echo '<script>alert("Error while inserting data");</script>'; 
 
} 
 
mysqli_close($con); 
 

 
?> 
 

 
</body>

回答

0

你有兩個add_topic.php一個應該是add_ans.php 我沒有看到這個代碼的任何確切的問題,你確定$id在窗體的隱藏字段中正確使用? 可能試着在add_ans.php頁面上執行print_r($_POST);,以查看

表格實際傳遞的數據