Remove user from table using checkbox - php

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>

Related

how to display the right value?

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

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
?>

Pulling last years data from a table

Hello I'm trying to update this scholarship application box. However when the year changed to 2013 it only displays scholarship applicant info from 2013. I'd like it to display info from 2012. I tried messing around with the date but I cant seem to figure it out. Any help would be greatly appreciated!
<?php
$appYear = date("Y").'-'.(date("Y")+1);
$sql = 'select * from sApplication where studentID = "'.$database->iPrep($_SESSION['ID']).'" AND appYear = "'.$appYear.'" LIMIT 1';
$appID = Scholarship::iFindSQL($sql);
$total = count($appID);
if ($total > 0)
{
$app = array_shift($appID);
}
else
{
$app = 0;
}
?>
<li id="item-2">
<div id="appStatus">
<h3>Application Status</h3>
<blockquote>
<?php if ($app->submitted == ('0000-00-00') || !isset($app->submitted)) { ?>
<table style="border:1px solid #000;" width="100%" border="0" cellspacing="5" cellpadding="0">
<tr>
<td width="50%"><strong>Scholarship<br /> 2013-2014</strong></td>
<td width="50" align="right"> <a style="font-size:16px;" href="welcome.php? app=Scholar">Apply Now</a></td>
</tr>
<tr>
<td><strong>Date Submitted</strong></td>
<td align="right"> </td>
</tr>
<tr>
<td><strong>References</strong></td>
<td align="right"> </td>
</tr>
<tr>
<td><strong>Decision</strong></td>
<td> </td>
</tr>
<tr>
<td colspan="2"><hr /></td>
</tr>
<tr>
<td><strong>Scholarship 2012-2013</strong></td>
<td> </td>
</tr>
<tr>
<td><strong>Decision</strong></td>
<td> </td>
</tr>
</table>
<?php } else { ?>
<table style="border:1px solid #000;" width="100%" border="0" cellspacing="5" cellpadding="0">
<tr>
<td width="90%"><strong>Scholarship 2013-2014</strong></td>
<td width="10%" align="right"> </td>
</tr>
<tr>
<td><strong>Date Submitted</strong></td>
<td align="right"><?=dbOutDate($app->submitted)?></td>
</tr>
<tr>
<td><strong>References</strong> </td>
<td align="right"></td>
</tr>
<tr>
<td colspan="2">
<?php
$refs = Reference::iFindSQL("Select * from reference where appID = '".$app->ID."'");?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php foreach($refs as $ref) { ?>
<tr> <td> <small><?php if($ref->rType == 'Academic Reference'){ echo 'Academic/Artistic/Professional'; } else { echo 'Community Service'; } ?></small></td> <td align="right"><?=$ref->status?></td></tr>
<?php } ?>
</table>
</td>
</tr>
<tr>
<td><strong>Decision</strong></td>
<td align="right">
<?php
if ($app->complete == 'Approved') { echo 'Approved'; }
if ($app->complete == 'Declined') { echo 'Declined'; }
if ($app->complete == 'Pending') { echo 'Pending'; }
if ($app->complete == 'Incomplete') { echo 'Incomplete'; }
Remove the WHERE clause that limits the year. Use ORDER BY to sort by year, descending.
$sql = 'select * from sApplication
where studentID = "'.$database->iPrep($_SESSION['ID']).
'" ORDER BY appYear DESC LIMIT 1';

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.

order by date mysql query

I used the following code to display the event details fetched from the database.
<form name="event_form" id="event_form" action="" method="post" enctype="application/x-www-form-urlencoded">
<table width="765" border="0" align="left" cellpadding="0" cellspacing="0" >
<tr>
<td>
<table width="765" border="0" align="left" cellpadding="0" cellspacing="0" id="results" class="fronttbl">
<tr></tr>
<?php
$select = "SELECT * FROM `tbl_event`";
$select_event = mysql_query($select);
$select_num = mysql_num_rows($select_event);
if($select_num > 0)
{
while($fetch = mysql_fetch_array($select_event))
{
$feventid=$fetch['intEventid'];
$feventname=stripslashes(ucfirst($fetch['varEventname']));
$fDate=$fetch['varDate'];
$seperate=explode("-", $fDate );
$year=$seperate[0];
$month=$seperate[1];
$date=$seperate[2];
$fchiefguest=stripslashes(nl2br($fetch['varChiefguest']));
$fvenue=stripslashes($fetch['varVenue']);
$ftime=stripslashes($fetch['varTime']);
$feventdetails=stripslashes($fetch['varEventdetails']);
echo " ";
?>
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="30%" height="30" valign="top"><strong>Name of the event:</strong></td>
<td width="70%" height="30" valign="top"><?php echo $feventname; ?></td>
</tr>
<tr>
<td height="30" valign="top"><strong>Date of the event to be held:</strong></td>
<td height="30" valign="top"><?php echo $date.'-'.$month.'-'.$year; ?></td>
</tr>
<tr>
<td height="30" valign="top"><strong>Time of the Event:</strong></td>
<td height="30" valign="top"><?php echo $ftime; ?></td>
</tr>
<tr>
<td height="30" valign="top"><strong>Venue of the event:</strong></td>
<td height="30" valign="top"><?php echo $fvenue; ?></td>
</tr>
<tr>
<td height="30" valign="top"><strong>Name of the Chief Guest:</strong></td>
<td height="30" valign="top"><?php echo $fchiefguest; ?></td>
</tr>
<tr>
<td height="30" valign="top"><strong>Event Details:</strong></td>
<td height="30" valign="top"><?php echo $feventdetails; ?></td>
</tr>
</table>
<p style="border-bottom:1px dotted #CCCCCC;"></p>
</td></tr>
<?php }
}
?>
</table>
</td>
</tr>
</table>
<div id="pageNavPosition"></div>
</form>
I have date field in my database table and the input will be saved like the following format 2012-03-01.
I need my page displays the events order by recent date/month/year in the frontend. For example today's event should displayed first likewise. How can i do that?
Use ORDER BY (...is that what you meant)?
Change your query to:
SELECT *
FROM `tbl_event`
ORDER BY varDate DESC
as i understand use this function for front end:
function changeFormat( $date ){
$exp_date = explode('-', $date);
return $exp_date[2] . '-'.$exp_date[1].'-'.$exp_date[0];
}
example: echo changeFormat('2012-12-01'); will return 01-12-2012 and apply this in your while loop where you want to display your date

Categories