This question already has answers here:
Posting both checked and unchecked checkboxes
(3 answers)
Closed 1 year ago.
I am working on an attendance reporting page. Here I am creating a dynamic checkbox with content from db.
I can store the checked student(present) students detail, but I also want to store the absent student. In order to do that I want a hidden field or something to get the unchecked student details please anyone help me to do that.
<form name="myform" action="" method="post">
<div class="checkbox">
<table border="1" cellspacing="2" cellpadding="5" summary="">
<?php while ($row = mysql_fetch_assoc($res)){?>
<tr>
<td>
<input type="checkbox" class="input" id="input<?php echo $row['st_id']; ?>" name="student[]" value="<?php echo $row['st_id']; ?>" checked="checked">
<?php echo $row['st_name'] ; ?>
<label for="input<?php echo $row['st_id']; ?>"></label>
<input type="text" name="absent" value="0"/>
</td>
</tr>
<?php }?>
</table>
<input type="submit" name="submit" value="submit"/>
</div>
PHP code
<?php
$res = mysql_query("SELECT * FROM `student_info` WHERE `sem`='$selsem'");
if(isset($_POST["submit"]))
{
//Here goes array
for($i=0;$i<count($_POST['student']);$i++)
{
$id=$_POST['student'][$i];
echo $id;
$check=1;
mysql_query("insert into manage_attendance(st_id,date,sem,period,subject,status) values('$id','$seldate','$selsem','$selperiod','$selsub','$check')");
}
}
?>
As #EhsanT said i have solved the problem with sql command...Thankyou EhsanT....
if(isset($_POST["submit"]))
{
$ans=array();
$ans=$_POST['student'];
$re=mysql_query("SELECT `st_id` FROM `student_info`
WHERE `sem` ='$selsem'
AND `st_id` NOT IN (".implode(',',array_map('intval',$ans)).")");
while ($ro = mysql_fetch_row($re)){
mysql_query("insert into manage_attendance(st_id,date,sem,period,subject,status) values('$ro[0]','$seldate','$selsem','$selperiod','$selsub','0')"); }
//Here goes array
for($i=0;$i<count($_POST['student']);$i++)
{
$id=$_POST['student'][$i];
//echo $id;
$check=1;
mysql_query("insert into manage_attendance(st_id,date,sem,period,subject,status) values('$id','$seldate','$selsem','$selperiod','$selsub','$check')");
//print_r ($_POST['student']);
}
}
Related
I have outputted the results of a MySQL table to an HTML table. In the last column, I want to add a delete option which calls another form and deletes the user from the MySQL table. I can't seem to get it to work though.
This is my code for the results page:
<?php
$contacts = mysql_query("
SELECT * FROM contacts ORDER BY ID ASC") or die( mysql_error() );
// If results
if( mysql_num_rows( $contacts ) > 0 )
?>
<table id="contact-list">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Telephone</th>
<th>Address</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php while( $contact = mysql_fetch_array( $contacts ) ) : ?>
<tr>
<td class="contact-name"><?php echo $contact['name']; ?></td>
<td class="contact-email"><?php echo $contact['email']; ?></td>
<td class="contact-telephone"><?php echo $contact['telephone']; ?></td>
<td class="contact-address"><?php echo $contact['address']; ?></td>
<td class="contact-delete"><form action='delete.php' method="post">
<input type="hidden" name="name" value="">
<input type="submit" name="submit" value="Delete">
</form></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
and, this is my delete.php script
<?php
//Define the query
$query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1";
//sends the query to delete the entry
mysql_query ($query);
if (mysql_affected_rows() == 1) {
//if it updated
?>
<strong>Contact Has Been Deleted</strong><br /><br />
<?php
} else {
//if it failed
?>
<strong>Deletion Failed</strong><br /><br />
<?php
}
?>
I cannot figure out why this is not working.
You have to pass a variable in the delete link. You have to pass <?php echo $contact['name']; ?> (the name value) in a hidden field or pass this value in URL:
Replace
<td class="contact-delete">
<form action='delete.php' method="post">
<input type="hidden" name="name" value="">
<input type="submit" name="submit" value="Delete">
</form>
</td>
With
<td class="contact-delete">
<form action='delete.php?name="<?php echo $contact['name']; ?>"' method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
</td>
USe javascript
<input name="Submit2" type="button" class="button" onclick="javascript:location.href='delete.php?id=<?php echo $your_id;?>';" value="« Back" />
and in delet.php
$id=$_GET['id'];
and put $id in your sql statement.
You are missing to pass name in this line:
<input type="hidden" name="name" value="">
You need to have something (<?php echo $contact['name']; ?>) in the value attribute.
BTW, do not use deprecated mysql_* functions, use PDO or mysqli_* instead.
<input type="hidden" name="name" value="">
You are missing a value which wil be picked up by this line in your delete file.
$query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1";
Right now it isn't receiving anything, which is why it will not work.
So add a value to it and it will work. Example:
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
First, you should not write the code in that way; the code has no protection against SQL injection.
1. Try to use primary IDs instead of using a name (what happens if 2 people has the same name?).
So, you can create a hidden field to know which 'person' you are dealing with.
<input type="hidden" name="contact_id" value="<?php $contact['contact_id']; ?>">
2. Sanitize variables to avoid attacks:
<?php $contact_id = isset($_POST['contact_id'])?intval($_POST['contact_id']):0;
// proceed with the query
if($contact_id>0) { $query = "DELETE FROM contacts WHERE contact_id = '$contact_id'";
}
// redirect to the main table with header("location: main.php");
?>
I have outputted the results of a MySQL table to an HTML table. In the last column, I want to add a delete option which calls another form and deletes the user from the MySQL table. I can't seem to get it to work though.
This is my code for the results page:
<?php
$contacts = mysql_query("
SELECT * FROM contacts ORDER BY ID ASC") or die( mysql_error() );
// If results
if( mysql_num_rows( $contacts ) > 0 )
?>
<table id="contact-list">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Telephone</th>
<th>Address</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php while( $contact = mysql_fetch_array( $contacts ) ) : ?>
<tr>
<td class="contact-name"><?php echo $contact['name']; ?></td>
<td class="contact-email"><?php echo $contact['email']; ?></td>
<td class="contact-telephone"><?php echo $contact['telephone']; ?></td>
<td class="contact-address"><?php echo $contact['address']; ?></td>
<td class="contact-delete"><form action='delete.php' method="post">
<input type="hidden" name="name" value="">
<input type="submit" name="submit" value="Delete">
</form></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
and, this is my delete.php script
<?php
//Define the query
$query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1";
//sends the query to delete the entry
mysql_query ($query);
if (mysql_affected_rows() == 1) {
//if it updated
?>
<strong>Contact Has Been Deleted</strong><br /><br />
<?php
} else {
//if it failed
?>
<strong>Deletion Failed</strong><br /><br />
<?php
}
?>
I cannot figure out why this is not working.
You have to pass a variable in the delete link. You have to pass <?php echo $contact['name']; ?> (the name value) in a hidden field or pass this value in URL:
Replace
<td class="contact-delete">
<form action='delete.php' method="post">
<input type="hidden" name="name" value="">
<input type="submit" name="submit" value="Delete">
</form>
</td>
With
<td class="contact-delete">
<form action='delete.php?name="<?php echo $contact['name']; ?>"' method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
</td>
USe javascript
<input name="Submit2" type="button" class="button" onclick="javascript:location.href='delete.php?id=<?php echo $your_id;?>';" value="« Back" />
and in delet.php
$id=$_GET['id'];
and put $id in your sql statement.
You are missing to pass name in this line:
<input type="hidden" name="name" value="">
You need to have something (<?php echo $contact['name']; ?>) in the value attribute.
BTW, do not use deprecated mysql_* functions, use PDO or mysqli_* instead.
<input type="hidden" name="name" value="">
You are missing a value which wil be picked up by this line in your delete file.
$query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1";
Right now it isn't receiving anything, which is why it will not work.
So add a value to it and it will work. Example:
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
First, you should not write the code in that way; the code has no protection against SQL injection.
1. Try to use primary IDs instead of using a name (what happens if 2 people has the same name?).
So, you can create a hidden field to know which 'person' you are dealing with.
<input type="hidden" name="contact_id" value="<?php $contact['contact_id']; ?>">
2. Sanitize variables to avoid attacks:
<?php $contact_id = isset($_POST['contact_id'])?intval($_POST['contact_id']):0;
// proceed with the query
if($contact_id>0) { $query = "DELETE FROM contacts WHERE contact_id = '$contact_id'";
}
// redirect to the main table with header("location: main.php");
?>
I'm trying to create a students attendance system and when I'm attempting to store information to the database, I'm getting various errors about the attendance status. I'm aware this isn't a typical problem for all but it would help a beginner of PHP.
<form action="tutor_attendance.php" method="post">
<table class="table table-striped">
<tr>
<th>Student ID</th> <th>Module Name</th> <th>Attendance Status </th>
</tr>
<?php $result=mysqli_query($conn, "SELECT * FROM student");
// $serialnumber=0;
$counter=0;
while($row=mysqli_fetch_array($result))
{
?>
<tr>
<td> <?php echo $row['stud_id']; ?> </td>
<input type="hidden" value="<?php echo $row['stud_id']; ?>" name="stud_id[]" >
<td> <?php echo $row['module_1']; ?> </td>
<input type="hidden" value="<?php echo $row['module_1']; ?>" name="module_1[]">
<td>
<input type="radio" name="attendance_status[<?php echo $counter; ?>]" value="Present">Present
<input type="radio" name="attendance_status[<?php echo $counter; ?>]" value="Absent">Absent
<input type="radio" name="attendance_status[<?php echo $counter; ?>]" value="Late">Late
</td>
</tr>
<?php
$counter++;
}
?>
</table>
<input type="submit" name="submit" class="btn btn-primary " value="submit" >
</form>
The code above is the form that I'm using to present data from the database and then capture their attendance with a radio button.
if(isset($_POST['submit']))
{
foreach($_POST['attendance_status'] as $id->$attendance_status)
{
$stud_id=$_POST['stud_id'][$id];
$module_1=$_POST['module_name'][$id];
$date=date("Y-m-d H:i:s");
mysqli_query($conn, "INSERT into attedance('stud_id, module_name, attendance_status, date) VALUES('$stud_id', '$module_1','$attendance_status','$date')");
}
}
Then the code above this is once the submit button has been pressed. The errors that I'm getting are: Undefined variable: attendance_status and Creating default object from empty value. Any help would be appreciated. The errors are occurring on line 8 - foreach($_POST['attendance_status'] as $id->$attendance_status)
I see a few different problems but the error you are getting is due to improper foreach syntax, which should be key => value not key->value
foreach($_POST['attendance_status'] as $id => $attendance_status) {
}
The errant quote in your SQL is not causing this error, but it is certainly another error. See this line:
"INSERT into attedance('stud_id, module_name, ...
Also did you really typo your table name for attendance? My guess is that it should be:
"INSERT into attendance(stud_id, module_name, ...
I'm trying to create an evaluation form for students, all questions are stored in db. I'm retrieving them from db any trying to store the answers for each question on db again. all answers should be selected from radio buttons as the following:
<?php
$sql = "SELECT Q_body, Q_ID FROM s_evaluation_questions WHERE Ev_ID='1'";
$result = $conn->query($sql);
?>
<?php
if ($result->num_rows > 0)
{
?>
<form action="AnswerS.php" method="POST">
<table align="right" id="keywords" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th colspan="4"> </th>
<th>Question</th>
</tr>
</thead>
<tbody>
<?php
while($row = $result->fetch_assoc())
{ $answer="s".$row["Q_ID"];
?>
<tr>
<td>
<input type="hidden" name="Q_ID" value="<?php echo $row["Q_ID"]; ?>" >
<input type="radio" name=<?php echo $answer?> value="Bad" >Bad
<input type="radio" name=<?php echo $answer?> value="Good"> Good
<input type="radio" name=<?php echo $answer?> value="VeryGood"> Very Good
<input type="radio" name=<?php echo $answer?> value="Excellent"> Excellent</td>
<td align="right"> <?php echo $row["Q_body"]?></td>
</tr>
<?php } ?>
</tbody>
</table></br></br></br></br></br></br></br>
<input type="submit" value="Send" />
</form>
<?php
?></div>
<?php
} else
{echo "not allowed";}
And on the AnswerS.php page I suppose to store the answers on db as the following:
$UID='1';
$answer=$_POST['answer'];
$Q_ID= $_POST['Q_ID'];
$sql = "INSERT INTO s_evaluation_answers(Q_ID, A_body, UID) VALUES($Q_ID, $answer, $UID) ";
$result = $conn->query($sql);
if ($result === TRUE) {
echo "done" ;
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
but unfortunately it doesn't work!, I tried to trace the value of Q_ID and it gives only the last question id not all questions.
how can I store the selected value from the radio button for each question and store it as the answer for this question? (note: all questions are brought from db)
thanks
If you see your source html you will find that you have many fields with name Q_ID:
<input type="hidden" name="Q_ID" value="one id" >
<input type="hidden" name="Q_ID" value="another id" >
<input type="hidden" name="Q_ID" value="some more id" >
So browser sends you the last value of Q_ID - in above case it is some more id.
To avoid this - use [] notation in name attribute:
<input type="hidden" name="Q_ID[]" value="one id" >
<input type="hidden" name="Q_ID[]" value="another id" >
<input type="hidden" name="Q_ID[]" value="some more id" >
After that, outputting $_POST['Q_ID'] will give you an array. You can iterate over it with foreach and gather other info you need. Something like (simplified):
foreach ($_POST['Q_ID'] as $q) {
// answer will be stored in a `s . $q` value of POST
$answer = $_POST['s' . $q];
$sql = "INSERT INTO s_evaluation_answers(Q_ID, A_body, UID) VALUES($q, $answer, $UID) ";
$result = $conn->query($sql);
}
I have outputted the results of a MySQL table to an HTML table. In the last column, I want to add a delete option which calls another form and deletes the user from the MySQL table. I can't seem to get it to work though.
This is my code for the results page:
<?php
$contacts = mysql_query("
SELECT * FROM contacts ORDER BY ID ASC") or die( mysql_error() );
// If results
if( mysql_num_rows( $contacts ) > 0 )
?>
<table id="contact-list">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Telephone</th>
<th>Address</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php while( $contact = mysql_fetch_array( $contacts ) ) : ?>
<tr>
<td class="contact-name"><?php echo $contact['name']; ?></td>
<td class="contact-email"><?php echo $contact['email']; ?></td>
<td class="contact-telephone"><?php echo $contact['telephone']; ?></td>
<td class="contact-address"><?php echo $contact['address']; ?></td>
<td class="contact-delete"><form action='delete.php' method="post">
<input type="hidden" name="name" value="">
<input type="submit" name="submit" value="Delete">
</form></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
and, this is my delete.php script
<?php
//Define the query
$query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1";
//sends the query to delete the entry
mysql_query ($query);
if (mysql_affected_rows() == 1) {
//if it updated
?>
<strong>Contact Has Been Deleted</strong><br /><br />
<?php
} else {
//if it failed
?>
<strong>Deletion Failed</strong><br /><br />
<?php
}
?>
I cannot figure out why this is not working.
You have to pass a variable in the delete link. You have to pass <?php echo $contact['name']; ?> (the name value) in a hidden field or pass this value in URL:
Replace
<td class="contact-delete">
<form action='delete.php' method="post">
<input type="hidden" name="name" value="">
<input type="submit" name="submit" value="Delete">
</form>
</td>
With
<td class="contact-delete">
<form action='delete.php?name="<?php echo $contact['name']; ?>"' method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
</td>
USe javascript
<input name="Submit2" type="button" class="button" onclick="javascript:location.href='delete.php?id=<?php echo $your_id;?>';" value="« Back" />
and in delet.php
$id=$_GET['id'];
and put $id in your sql statement.
You are missing to pass name in this line:
<input type="hidden" name="name" value="">
You need to have something (<?php echo $contact['name']; ?>) in the value attribute.
BTW, do not use deprecated mysql_* functions, use PDO or mysqli_* instead.
<input type="hidden" name="name" value="">
You are missing a value which wil be picked up by this line in your delete file.
$query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1";
Right now it isn't receiving anything, which is why it will not work.
So add a value to it and it will work. Example:
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
First, you should not write the code in that way; the code has no protection against SQL injection.
1. Try to use primary IDs instead of using a name (what happens if 2 people has the same name?).
So, you can create a hidden field to know which 'person' you are dealing with.
<input type="hidden" name="contact_id" value="<?php $contact['contact_id']; ?>">
2. Sanitize variables to avoid attacks:
<?php $contact_id = isset($_POST['contact_id'])?intval($_POST['contact_id']):0;
// proceed with the query
if($contact_id>0) { $query = "DELETE FROM contacts WHERE contact_id = '$contact_id'";
}
// redirect to the main table with header("location: main.php");
?>