how to display the right value? - php

Im trying to show the respective comments to the respective question ID but all the comments are shown in every post.
actually the value of qusetion ID is not going to the desired table where it suppose to be and that table
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>
block that named as que_id is showing the value of 0 (as i have set 0 as its default value) and that is the main reason of it but im not able to solve this problem. plz help...
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> </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>

You have two add_topic.php should one be add_ans.php
I don't see any exact problems with this code are you sure that the $id is being used correctly in the hidden field of your form?
Possibly try doing a print_r($_POST); on the add_ans.php page to see what data is actually being passed by the form

Related

Can't delete multiple rows from database with checkbox PHP

I'm learning php, now I'm trying to get use with databases and etc stuff. My code (that is below) seems that is not working, and I can't solve it out why. It looks good but still after pushing "delete" nothing happens. Maybe you will be able to help me. Thanks in advance.
<?php
$host="localhost";
$username="root";
$password="";
$db_name="zurnalas";
$tbl_name="zurnalas";
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<form name="form1" method="post" action="">
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Žurnalų trinimas iš duomenų bazės</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Pavadinimas</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Kiekis</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Leidykla</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Metai</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Kaina</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['pavadinimas']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['kiekis']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['leidykla']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['metai']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['kaina']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="delete"></td>
</tr>
<?php
// Check if delete button active, start this
if($_POST['delete']){
$checkbox[] = $_POST['checkbox[]'];
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=trinti.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
Your form's action field is empty. You have to get fields from form with php "$checkbox = $_POST['checkbox']". After you can do like you do, or you can write other mysql query " DELETE FROM YOUR_TABLE WHERE id in (1,2,3,4) ".

Remove user from table using checkbox

I know there are questions similar, but I am unable to determine my issue with my PHP code. I am trying to remove a student from an intermediary table between Student table and Class table, called Student_has_Class. The SQL code works, just not the page. It lists the students in the table successfully, but when I check the box by a student to remove them and click submit, it redirects to same page (basically a refresh) like it is supposed to (even though it doesn't reload the table for some reason), and the student is still in the Student_has_Class table. Please help, as posting online is a last resort for me.
Please take a look a the code:
Note: I removed some login and web address info for privacy. Indicated with { }.
<?php
// Connects to your Database
session_start();
$x = $_SESSION['user'];
$y = $_GET['id'];
mysql_connect("{removed site for privacy}", "{username removed}", "{password removed}") or die(mysql_error
());
mysql_select_db("test") or die(mysql_error());
if (isset($_POST['delete']))
{
if (isset($_POST['checkbox']))
{
$checkbox = $_POST['checkbox'];
if (is_array($checkbox)) {
foreach ($checkbox as $key => $x)
{
$mysql->query("DELETE FROM Student_has_Class WHERE User_idUser='$x'");
}
}
}
}
?>
<?php
$sql = "SELECT User.idUser, User.UserFirstName, User.UserLastName
FROM Student_has_Class
INNER JOIN User ON User.idUser = Student_has_Class.User_idUser AND User.Role = 'Student'
INNER JOIN Class ON Class.idClass = Student_has_Class.Class_idClass
WHERE Student_has_Class.Class_idClass = 2 AND Class.User_idUser = ".$x."
ORDER BY User.UserLastName";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Class Roster</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">Drop</td>
<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Last Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>First Name</strong></td>
</tr>
<?php
while ($rows = mysql_fetch_array($result))
{
?>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['idUser'];?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['idUser'];?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['UserLastName'];?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['UserFirstName'];?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Submit"></td>
</tr>
<?php
// Check if delete button active, start this
if ($_POST['delete'])
{
for ($i = 0; $i < $count; $i++)
{
$del_id = $checkbox[$i];
$sql = "DELETE FROM Student_has_Class WHERE User_idUser=".$del_id."";
$result = mysql_query($sql);
}
// if successful redirect to same page
if ($result)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL={address}/{page}.php\">";
}
}
mysql_close();
?>
Update:
I used Internet Explorer debugger to get some info that Chrome debugger doesn't provide. Here is the page, notice the values in the checkboxes. I'm not sure if that is correct.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>*HIDDEN*</title>
<style type="text/css">
body {
background-color: #D9D7D7;
}
h1 {
color: #FBF8F8;
}
</style>
<style>
table,th,td
{
border:1px solid black;
}
</style>
</head>
<body bgcolor="#D7D3D3">
<a href="main_login.php" >
<img src="banner2.jpg" width="1340" height="90">
</a></img>
<div id="shHeader">
<center>
<div class="div">
Welcome to *HIDDEN*!</div></center>
<table width ="1345" height="30" align="top" border=1">
<tr align= top>
<tr>
<td width="40"> Category </td>
<td width="40"> Homework Graph </td>
<td width="40"> Test Graph </td>
<td width="40"> Overview Graph </td>
<td width="40"> Progress Bar </td>
<td width="40"> Timeline </td>
</tr>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Class Roster</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">Drop</td>
<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Last Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>First Name</strong></td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name='checkbox[0]' type='checkbox' id='checkbox[]' value=''></td>
<td bgcolor="#FFFFFF">5</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name='checkbox[1]' type='checkbox' id='checkbox[]' value=''></td>
<td bgcolor="#FFFFFF">3</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name='checkbox[2]' type='checkbox' id='checkbox[]' value=''></td>
<td bgcolor="#FFFFFF">12</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name='checkbox[3]' type='checkbox' id='checkbox[]' value=''></td>
<td bgcolor="#FFFFFF">4</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name='checkbox[4]' type='checkbox' id='checkbox[]' value=''></td>
<td bgcolor="#FFFFFF">6</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
<td bgcolor="#FFFFFF">*HIDDEN*</td>
</tr>
<input type='hidden' name='hiddencounter' value='5'>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
Update:
The POST parameters are as follows when I try to delete the third person in the table by checking the checkbox to the left of their name, and clicking the Delete button (previously called Submit button). The person to delete has a User ID of 12.
Array ( [checkbox] => Array ( [2] => ) [hiddencounter] => 5 [delete] => Delete )
You can try this. But I will change your code from MySQL to MySQLi.
<?php
// Connects to your Database
session_start();
$x = $_SESSION['user'];
$y = $_GET['id'];
/* ESTABLISH CONNECTION */
$connect=mysqli_connect("YourHost","YourUsername","YourPassword","YourDatabaseName"); /* REPLACE THE NECESSARY HOST, USERNAME, PASSWORD, AND DATABASE */
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
if (isset($_POST['delete'])){ /* IF DELETE IS SET/CLICKED */
$counter=mysqli_real_escape_string($connect,$_POST['hiddencounter']);
$checkbox=$_POST['checkbox'];
for($x=0;$x<=$counter;$x++){
if(!empty($checkbox[$x])){ /* IF SELECTED ITEM */
$checked=mysqli_real_escape_string($connect,$checkbox[$x]);
mysqli_query($connect,"DELETE FROM Student_has_Class WHERE User_idUser='$checked'");
} /* END OF IF NOT EMPTY CHECKBOX SELECTED */
} /* END OF FOR LOOP */
} /* END OF ISSET DELETE */
?>
<?php
$sql = "SELECT User.idUser, User.UserFirstName, User.UserLastName
FROM Student_has_Class
INNER JOIN User ON User.idUser = Student_has_Class.User_idUser AND User.Role = 'Student'
INNER JOIN Class ON Class.idClass = Student_has_Class.Class_idClass
WHERE Student_has_Class.Class_idClass = 2 AND Class.User_idUser = ".$x."
ORDER BY User.UserLastName";
$result = mysqli_query($connect,$sql);
$count = mysqli_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Class Roster</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">Drop</td>
<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Last Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>First Name</strong></td>
</tr>
<?php
$counter=0; /* FOR COUNTING PURPOSES */
while ($rows = mysqli_fetch_array($result))
{
?>
<tr>
<td align="center" bgcolor="#FFFFFF">
<?php
$iduser=$rows['idUser'];
echo "<input name='checkbox[$counter]' type='checkbox' id='checkbox[]' value='$iduser'></td>";
/* I HAVE ASSIGNED THE COUNTER INSIDE THE CHECKBOX ARRAY ABOVE. AND DO YOU REALLY NEED [] IN YOUR ID? */ ?>
<td bgcolor="#FFFFFF"><?php echo $rows['idUser'];?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['UserLastName'];?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['UserFirstName'];?></td>
</tr>
<?php
$counter=$counter+1; /* INCREMENT COUNTER EVERY LOOP */
} /* END OF WHILE LOOP */
echo "<input type='hidden' name='hiddencounter' value='$counter'>"; /* SUBMIT A HIDDEN INPUT CONTAINING THE OVERALL COUNTER */
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Submit"></td>
</tr>
</table>
</form>

Unexpected $end in my forum script

So, I'm adding a forum section to my site. So far, everyone thing is good. You can create a topic, view a list of topics, but when you go to view a topic (which is view_topic.php?id=#) this keeps appearing:
Parse error: syntax error, unexpected $end in /home/papervip/public_html/forums/questions/view_topic.php on line 158
So, here's the full script of view_topic.php (without the database credentials included, so I can ensure my security :D ):
<?php
session_start();
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="forum_questions"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=$_GET['id'];
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../style/base.css">
<title>Paperviper - Questions - View Topic</title>
<meta name="keywords" content="games, free, indie, paperviper, pc, video games">
<meta name="description" content="Paperviper is a game developer group consisten of a team of indie-developers and freelancers. Working to bring you the best cheap and free games for addicting fun on your PC, Mac, and Linux!">
<style type="text/css">
</head>
<body>
<?php if(empty($_SESSION['user'])){
include("../../header.html");
}else{
include("../../user/user_header.html");
?>
<div id="page">
<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><? echo $rows['topic']; ?></strong></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><? echo $rows['detail']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>By :</strong> <? echo $rows['name']; ?> <strong>Email : </strong><? echo $rows['email'];?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Date/time : </strong><? echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>
<BR>
<?php
$tbl_name2="forum_questions_answer";
$sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id'";
$result2=mysql_query($sql2);
while($rows=mysql_fetch_array($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"><? echo $rows['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"><? echo $rows['a_name']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Email</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><? echo $rows['a_email']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Reply</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><? echo $rows['a_answer']; ?></td>
</tr>
<tr>
<td bgcolor="#F8F7F1"><strong>Date/Time</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><? echo $rows['a_datetime']; ?></td>
</tr>
</table></td>
</tr>
</table><br>
<?php
}
$sql3="SELECT view FROM $tbl_name WHERE id='$id'";
$result3=mysql_query($sql3);
$rows=mysql_fetch_array($result3);
$view=$rows['view'];
/
if(empty($view)){
$view=1;
$sql4="INSERT INTO $tbl_name(view) VALUES('$view') WHERE id='$id'";
$result4=mysql_query($sql4);
}
$addview=$view+1;
$sql5="update $tbl_name set view='$addview' WHERE id='$id'";
$result5=mysql_query($sql5);
mysql_close();
?>
<BR>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="add_answer.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" value="<?php echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); ?>" readonly></td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td>:</td>
<td><input name="a_email" type="text" id="a_email" size="45" value="<?php echo htmlentities($_SESSION['user']['email'], ENT_QUOTES, 'UTF-8'); ?>" readonly></td>
</tr>
<tr>
<td valign="top"><strong>Reply</strong></td>
<td valign="top">:</td>
<td><textarea name="a_answer" cols="38" rows="10" id="a_answer"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input name="id" type="hidden" value="<? echo $id; ?>"></td>
<td><input type="submit" name="Submit" value="Submit" class="button"> <input type="reset" name="Submit2" value="Reset" class="button"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php include("../../footer.html");?>
</div>
</body>
</html>
I've re-read the entire file about 10 times now, but it still keeps stating the final closing html tag as a unexpected $end. Am I overlooking something? Thanks in advance.
I believe you have an unmatched { here:
<body>
<?php if(empty($_SESSION['user'])){
include("../../header.html");
}else{
include("../../user/user_header.html");
} // It looks like this is missing
?>

update mysql table with list values

i have a form that extract data from mysql table into a form, each row has a menu to choose a value from and i want to update mysql with each value choosen for each row when the 'Apply To All' button is clicked but doesnt work at all.,here is my code.
<td><form id="main" name="main" method="post" action="setProjectStatus.php" onsubmit="return validateMain();">
<table width="100%" cellspacing="1" cellpadding="1">
<tr>
<td width="35%" rowspan="3"><img src="../img/project.jpg" alt="Comp Sci Stud" width="325" height="199" border="2" /></td>
<td width="65%" height="42" colspan="2"><table width="94%" cellpadding="1" cellspacing="1" class="main_table">
<tr class="table_title">
<td width="100%" class="table_title">Set Project Status. </td>
</tr>
<tr>
<td height="26"> </td>
</tr>
<tr>
<td height="26"><table width="100%" cellspacing="1" cellpadding="1">
<tr class="table_head">
<td width="2%" height="35"><div align="center"></div></td>
<td width="26%" height="35"><div align="center">Student Name</div></td>
<td colspan="2"><div align="center">Project</div></td>
<td width="19%"><div align="center">Status</div></td>
</tr>
<?php
session_start();
$username = $_SESSION['username'];
require_once("mysqlConnect.php");
//
$sql="SELECT * FROM spms_Student";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
echo "There are $count projects to be undertaken.";
while($rows=mysql_fetch_array($result)){
//
$query = "SELECT name FROM spms_systemUser WHERE userId = '".$rows[0]."'";
$result1 = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result1);
$name = $row[0];
?>
<tr>
<td height="25" align="center"> </td>
<td align="center"><?php echo $name; ?></td>
<td colspan="2" align="center"><?php echo $rows[1]; ?></td>
<td align="center"><label>
<select name="select" class="form_field_100px_select">
<option value="Pending" selected="selected">Pending</option>
<option value="Approved">Approved</option>
<option value="Disapproved">Disapproved</option>
</select>
</label></td>
</tr>
<?php
}
?>
<tr class="pager_bg">
<td height="35"> </td>
<td> </td>
<td width="37%" align="right"><input name="done" type="button" id="done" value="Done" onclick="window.location='../coordinatorMenu.html'" /></td>
<td width="16%"><label>
<input name="approveAll" type="submit" id="approveAll" value="Approve All" />
</label></td>
<td><input name="apply" type="submit" id="apply" value="Apply To All" /></td>
</tr>
<?php
mysql_close();
?>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><label></label> <label></label></td>
</tr>
</table>
</form></td>
You're trying to reuse your MySQL connection while still holding on to the resultset from the first query. You need to create a second connection for the inner loop queries. Even better would probably be to rewrite your query using a join, but I can't say for sure without knowing your schema.

Checkbox database deleting using phpmysql

Can any one tell what is happening with my code? I got this code from web but its not working; when I press delete its not deleting the database. Can you help me?
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="comment";
$tbl_name="members";
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr><td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"><tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td></tr>
<tr><td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
< td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td></tr>
<?php while($rows=mysql_fetch_array($result, MYSQL_ASSOC)){ ?>
<tr><td align="center" bgcolor="#FFFFFF">
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>">
</td><td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td><td bgcolor="#FFFFFF">
<?php echo $rows['firstname']; ?></td><td bgcolor="#FFFFFF"><?php echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['passwd']; ?>
</td></tr>
<?php } ?>
<tr><td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete"></td></tr>
</table></form></td></tr></table>
<?php
$checkbox = $_POST['checkbox'];
$delete = $_POST['delete'];
if(isset($delete)){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";}}
mysql_close();
?>
Please correct the form action to work the code
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="comment";
$tbl_name="members";
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<form name="form1" method="post" action="<? $_SERVER["PHP_SELF"]; ?>">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF">
<strong>Delete multiple rows in mysql</strong>
</td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF">
<strong>Id</strong>
</td>
<td align="center" bgcolor="#FFFFFF">
<strong>Name</strong>
</td>
<td align="center" bgcolor="#FFFFFF">
<strong>Lastname</strong>
</td>
<td align="center" bgcolor="#FFFFFF">
<strong>Email</strong>
</td>
</tr>
<?php while($rows=mysql_fetch_array($result, MYSQL_ASSOC)){ ?>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name="checkbox[<? echo $rows['id']; ?>]" type="checkbox" value="<? echo $rows['id']; ?>">
</td>
<td bgcolor="#FFFFFF">
<?php echo $rows['id']; ?>
</td>
<td bgcolor="#FFFFFF">
<?php echo $rows['firstname']; ?>
</td>
<td bgcolor="#FFFFFF">
<?php echo $rows['lastname']; ?>
</td>
<td bgcolor="#FFFFFF">
<?php echo $rows['passwd']; ?>
</td>
</tr>
<?php } ?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<?php
$delete = $_POST['delete'];
if(isset($delete)){
$t = $_POST;
foreach($t as $param){
$t1 = $param;
if(is_array($t1)){
foreach($t1 as $param2){
echo $param2;
echo "<br>";
//Here execute delete query
}
}
}
}
?>

Categories