i am trying to bulid an online attendace in php, i am fetching student list from student table, and want to put all chkboxs if checkd as present and if unchecked as absent,
i am unable to do that.
<div class="attendance">
<form accept="att.php" method="POST">
<?php
$sel_sql = "SELECT * FROM student";
$run_sql = mysqli_query($conn,$sel_sql);
while($rows = mysqli_fetch_assoc($run_sql)){
$id = $rows['id'];
echo '<input type="checkbox" name="status" value="'.$id.'" checked>'.ucfirst($rows['f_name']).'';
}
?>
<input type="submit" name="submit_attendance" value="Post Attendance">
<?php echo $msg; ?>
</form>
</div>
it shows prefect students list, but i dont know how to set insert query for all of these chkboxes
try this query to insert from another table
SELECT * INTO TABLE2 FROM student
use where condition on student table as where student.column value to select checked values
Applied same above things with checkbox input field
echo '<input type="checkbox" name="status" value="'.$id.'" 'if($row['present_field_of_database']) ? 'checked' : ''
'>'.ucfirst($rows['f_name']).'';
it's fine then updated code with your problems i hope it's work for you
<div class="attendance">
<form accept="att.php" method="POST">
<?php
$sel_sql = "SELECT * FROM student";
$run_sql = mysqli_query($conn,$sel_sql);
while($rows = mysqli_fetch_assoc($run_sql)){
$id = $rows['id'];
// if your $id value is right from $rows['id'] then
// change your student table name to the another table where available status of the student
$wh_sql = "SELECT * FROM student WHERE id=".$id;
$run_wh_sql = mysqli_query($conn, $wh_sql);
$Wh_rows = mysqli_fetch_assoc($run_wh_sql);
// add student present or absent value to the rows data
$rows['status'] = $Wh_rows['status'];
}
// set value as A or P respectively absent or present add jquery plugins for onclick event while client click on checkbox change value respectively
echo '<input type="checkbox" name="status" value="'.$rows['status'].'" 'if($rows['status'] == "A") ?'checked': '' '>'.ucfirst($rows['f_name']).' onclick= "$(this).val(this.checked ? P : A)"';
?>
<input type="submit" name="submit_attendance" value="Post Attendance">
<?php echo $msg; ?>
</form>
</div>
if (isset($_POST['send'])) {
$s_id = $_POST['status'];
$id = $_POST['student'];
if ($s_id) {
foreach ($s_id as $s) {
foreach ($id as $i) {
if (mysqli_query($conn, "INSERT INTO attendence(s_id,status) VALUES('".
mysqli_real_escape_string($conn, $s)."','".
mysqli_real_escape_string($conn, $i)."')")) {
$msg = "success";
}else{
$msg = "failed";
}
}
}
}
}
i have 3 students. when i press send it sends 9 entries. i am unable to understand how to insert all students data
This is attendance table
This is attandance table
i want to put entries like this if check box chekd it wil post p and if not it wil post a. i just need how to insert all sutdent at once quert
Related
For a school project I am trying to write to a table called enrolment where the student number and the course they have selected are added after they have been tested to make sure the student name and number exists in another database. No errors are coming up, however when I check my database afterward enrolment says its an empty set. Does anyone have suggestions?
<?php
require 'connect.php';
//making a variable from the user data
$name = $_POST["name"];
$number = $_POST["snumber"];
$course = $_POST["pcourse"];
//linking up the database
$link = mysqli_connect(HOST, USER, PASS, DB) or die (mysqli_connect_error());
// select all from table student which show student name and number
$squery = "SELECT * FROM student";
$sresult = mysqli_query($link, $squery);
$found = 0;
while ($srow = mysqli_fetch_array($sresult)) {
// testing if the student name and number match the users data
if ($name == $srow['family'] && $number == $srow['uid']) {
$enrol = "INSERT INTO enrolment (uid course) VALUES('$number' '$course')";
$found = 1;
break;
}
}
mysqli_close($link);
?>
<html>
<body>
<form action="index.php" method="post">
<br>
<input type = "submit" value="back" name="back">
</form>
</body>
</html>
index.php (form)
<!DOCTYPE html>
<html>
<body>
<h1>Course Selection</h1><br>
<form action="next.php" method="post">
Name: <input type="text" name="name" placeholder="Name" required="required" maxlength="50">
<br><br>
Student Number: <input type="text" name= "snumber" required="required" maxlength="9">
<br><br>
<?php
//form
require 'connect.php';
echo "Select a course: <select name = \"pcourse\">\n";
$link = mysqli_connect(HOST, USER, PASS, DB) or die(mysqli_connect_error());
$query = "SELECT * FROM course";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)) {
echo "<option> $row[code] $row[name] $row[maxenroll]</option><br>";
}
mysqli_free_result($results);
mysqli_close ($link);
echo " </select>\n";
?>
<br><br>
<input type = "submit" value="submit" name= "submit">
</form>
</body>
</html>
Your insert code just a string. You should send to mysql your insert code. Try this
$enrol = "INSERT INTO enrolment (uid, course) VALUES($number, $course)";
$link->query($enrol);
My guess is that when checking the result set from student table - there is no such family and uid in it, which means - in the table. Instead of doing insert right away, try to display matching record from the database - if this is actually what you wanted to find. Then you can check what is actually stored in the database - and you can compare both.
Other thing is - why not limit select to exact that student?
Rebuild your query, something like:
$squery = "select * from student where family='".$name."' and uid='".$number."'".
Then you can check how many records were selected and display that number before doing any inserts.
<?php
include("connection.php");
echo "Do you really want to DELETE this field? " ;
?>
<form method="post">
<input type="submit" name="yes" value="YES" />
<?php
$res = mysql_query("SELECT * FROM members");
while($row = mysql_fetch_array($res))
{
if(isset($_REQUEST['yes']))
{
$id=$row['id'];
echo $id;
mysql_query("DELETE from members WHERE id= '$id' ");
header("location: view.php");
}
}
?>
<input type="submit" name="no" value="NO" />
<?php
if(isset($_REQUEST['no']))
{
header("location: view.php");
}
?>
</form>
</body>
This is my code for delet random row from list view when click on delete button but i m not getting the result so please help me out of these.
Pass db connection as second parameter to your mysql_query() to delete rows from members table:
mysql_query("DELETE from members WHERE id= '$id'" , $dbCon);
Here DB connection I mean something this that you have defined in your connection.php file:
$db_host="localhost";
$db_name="test";
$username="root";
$password="";
$dbCon=mysql_connect($db_host,$username,$password);
I hope this solve your problem!
Change this line:
mysql_query("DELETE from members WHERE id= '$id' ");
to that:
mysql_query("DELETE FROM members WHERE id= '".$id."' ");
Your query SELECT * FROM members without ordering will select all the lines in a unpredictable order.
The ifs in your while loop will be either always true or always false:
if the user click on the YES button, all the rows of the table will be deleted as you don't have exit; after the header("location: view.php"); (see this post)
if the user click the NO button, then every iteration of the while loop won't do nothing.
So what you should do:
Have an hidden parameter in your form containing the id of the field you want to delete.
Test when the user submit the form that the hidden field is present and sanitized.
Do not select all the rows of the table
Delete the rows secificaly by its primary key.
Call exit; after header("location: view.php");
When do an if inside a loop, move it outside the loop if possible if the values do not change.
<?php
include("connection.php");
echo "Do you really want to DELETE this field? " ;
?>
<form method="post">
<input type="submit" name="yes" value="YES" />
<?php
if(isset($_REQUEST['yes']))
{
if(isset($_GET['delet']))
{
$id = $_GET['delet'];
$res = mysql_query("SELECT * FROM members WHERE id = '$id' ");
$row = mysql_fetch_array($res);
$did = $row['id'];
$dname = $row['name'];
$dphn = $row['phn'];
/*echo $did;
echo $dname;
echo $dphn;*/
$ins = "INSERT INTO del (mid,name,phn) values ('$did','$dname','$dphn')";
mysql_query($ins);
mysql_query("DELETE FROM members WHERE id = '$id' ");
header('location: view.php');
}
}
?>
<input type="submit" name="no" value="NO" />
<?php
if(isset($_REQUEST['no']))
{
header("location: view.php");
exit;
}
?>
</form>
in my db i have 20+ columns. i added 19 columns throught input form and stored in db succesfully. i fetch few rows from db in my main page. in my main page 1 more column is there. that is status column, it is a combo box type. if i click status column it should show 4 values. i want to select one of the values and then when i click save button it must go to stored in db with that same ID. how to do that? i tried but its not updated in mysql db...
mainpage combo box coding:
echo "\t<td><form action=statusdb.php method=post>
<select name=update><option value=empty></option><option value=Confirm>Confirm</option><option value=Processing>Processing</option><option value=Pending>Pending</option><option value=Cancelled>Cancelled</option></select>
<input name=\"update[".$a_row['slno']."]\"; type=submit id=id value=Save></form>
</td>\n";
status db coding:
if (isset($_GET['id']))
{
$id = mysql_real_escape_string($_GET['id']);
$sql = mysql_query("UPDATE guestdetails SET status = '" . $_POST['update'] ."'");
if(!$sql)
{
die("Error" .mysql_error());
}
}
help me how to do that?
In your IF should be $_POST, not $_GET
Also, need to add WHERE clause, like this:
if (isset($_POST['id']))
{
$id = mysql_real_escape_string($_POST['id']);
$update= mysql_real_escape_string($_POST['update']);
$sql = mysql_query("UPDATE guestdetails SET status = '$update' WHERE id='$id'");
if(!$sql)
{
die("Error" .mysql_error());
}
}
Also, you used name update twice, once in <select> once in <input>, take that one from <input> out, make it hidden field with name id and value of your slno row:
echo "\t<td>
<form action=statusdb.php method=post>
<select name=update>
<option value=empty></option>
<option value=Confirm>Confirm</option>
<option value=Processing>Processing</option>
<option value=Pending>Pending</option>
<option value=Cancelled>Cancelled</option>
</select>
<input name='id' type='hidden' value='".$a_row['slno']."';>
<input type='submit'>Save</button>
</form>
</td>\n";
Try like below:
<form action="statusdb.php" method="post">
<?php
while($a_row = mysql_fetch_array($sql)) {
$sl_no = $a_row['slno'];
echo '<select name="update['.$sl_no.']"><option value=empty></option><option value=Confirm>Confirm</option><option value=Processing>Processing</option><option value=Pending>Pending</option><option value=Cancelled>Cancelled</option></select> ';
echo '<input type="hidden" name="sl_no[]" value="'.$sl_no.'" />';
}
?>
<input name="update_rows" type="submit" value="Save">
</form>
<?php
if(isset($_POST['update_rows'])) {
$nums = $_POST['sl_no'];
$update = $_POST['update'];
foreach($nums as $sl) {
$sl_no = $sl;
$val = $update[$sl_no];
$sql_update = mysql_query("UPDATE guestdetails SET status = '$val' WHERE sl_no='$sl_no'");
}
}
?>
you are not submitting any id to status db.
I have a form that allows the user to add or remove users from a job by using checkboxes. At the moment the engineer's names etc are stored in a table called 'users' and they are identified by their user type, engineers. The checkboxes on the form are created by using a while loop to display a mysql query. This is done so that new users can be added to the database via a webpage at a later date and their name will automatically be added to the list of available engineers. To add an engineer to a job their name is simply ticked and the form submitted.
This works well but I would like the user to be able to add or remove users by checking and unchecking boxes on another form but I can't think for the life of me how to do this.
The users assigned to a job are stored in a many-to-many table called calls_engineers which has 3 fields
id_ce (unique id)
call_ce (id of job, a foreign key)
engineer_ce (id of engineer, a foreign key)
The engineer_ce is the users primary key in the 'users' table which is 'id'.
The code I have so far is...
<? $sql = "SELECT * FROM calls_engineers WHERE call_ce = '$diary_id'";
$result = mysql_query($sql) or die(mysql_error());
$row2 = mysql_fetch_array($result);
$sql = "SELECT * FROM users WHERE type = 'engineer' ORDER BY first_name";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)){ ?>
<div style="width:70px; float:left">
<?
if($row2['engineer_ce'] == $row['id']){
?>
<input type="checkbox" name="engineer[]" checked="checked" value="<? echo $row['id']; ?>" />
<? echo ' '.$row['first_name'];
} else { ?>
<input type="checkbox" name="engineer[]" value="<? echo $row['id']; ?>" />
<? echo ' '.$row['first_name'];
}?>
</div>
<?
}
?>
All this does is create checkboxes for each user which is an engineer but it doesn't check them if the user is assigned to the job.
Any help will be greatly appreciated
You never loops the calls_engineers table. Using
$row2 = mysql_fetch_array($result);
Will not actually find all assignments for the engineer. Now check every single engineer if this is included in current job:
<?
$sql = "SELECT * FROM users WHERE type = 'engineer' ORDER BY first_name";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)){ ?>
<div style="width:70px; float:left">
<input type="checkbox" name="engineer[]" <?
$result2 = mysql_query("SELECT * FROM calls_engineers WHERE call_ce = '".mysql_real_escape_string($diary_id)."' AND engineer_ce = ".$row['id']);
if(mysql_num_rows($result2) > 0) echo 'checked="checked"';
?> value="<? echo $row['id']; ?>" />
<? echo ' '.$row['first_name']; ?>
</div>
<? } ?>
Below code !Gives me check boxes and a delete button, In input tag all check box have same name (check)!! There check box can be retreive from database with id .
Problem Is:: when I am selecting multiple checkbox for deletion...only last one checkbox is deleted ! means it can delete 1 data from a database.
url like -> http://localhost/demo/delete.php?check=10&check=13&check=14&submit=Delete
I need while I am selecting a checkbox more than 1 checkbox ,check box datas is deleted from database ! Any one help me to overcome this problem thanks
index.php
<?php
$sql = mysql_connect('localhost', 'root', '');
mysql_select_db('database_section', $sql);
?>
<form name="checkbox" method="get" action="delete.php">
<table>
<tr>
<?php
$sql = "select * from data";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
?>
<td><input type="checkbox" name="check" value="<?php echo $row['id']?>"><?php echo $row['data'];?>
</td>
<?php
}
?>
<tr>
<td><input type="submit" name="submit" value="Delete"></td>
</tr>
</table>
</form>
Now, In delete.php..code below...
<?php
$sql = mysql_connect('localhost', 'root', '');
mysql_select_db('database_section', $sql);
if ($_REQUEST['submit']) {
$abc = $_GET['check'];
$sql = "Delete from data where id=$abc";
$result = mysql_query($sql) or die(mysql_error());
if (isset($result)) {
echo "data deleted";
}
else
{
echo "not possible";
}
}
?>
Use check box as an array holder. name it as check[] to hold all selected values. And on post you will get the selected array list.
Now your $abc will be a array, use foreach in delete.php to get the checked ids.
Change name="check" to name="check[]"
See more here: http://www.kavoir.com/2009/01/php-checkbox-array-in-form-handling-multiple-checkbox-values-in-an-array.html
[...]
while ($row = mysql_fetch_array($result))
{
?>
<td>
<input type="checkbox" name="check[]" value="<?php echo $row['id']?>"><?php echo $row['data']; ?>
</td>
<?php
}
?>
[...]