Inserting multiple record in phpmyadmin att table keeping all the information same except attendance and student id after clicking the submit button.Its taking only one last row without attendance status.I'm fetching attendance sheet from database.I have been stuck here in my first php project.
faculty panel
<form action="<?php $PHP_SELF ?>" method="post">
<li><b>Select Courses :</b> <select name="courses" id="courses" class="form-
control action" >
<option value="">Courses</option>
<?php echo $courses; ?></select></li><br/>
<li><b>Select Semesters :</b> <br><select name="semesters" id="semesters"
class="form-control action" >
<option value="">Semesters</option></select></li><br/>
<li><b>Select Subjects :</b><br> <select name="subjects" id="subjects"
class="form-control " >
<option value="">Subjects</option></select>
<br/>
<li><b>Session No:<b><br/><input type="text" name="session_no"
id="session_no" placeholder ="Session No" class="form-control">
<br>
<label class="control-label" for="date">Date</label>
<br/>
<input class="form-control" id="date" name="date" placeholder="DD/MM/YYYY"
type="date"/>
<br/>
<li><b>Enter time:</b></li><br/>
From :<input type="time" name="time_from" id="time_from" class="form-
control"/> </li><li>
<br/>
To :<input type="time" name="time_to" id="time_to" class="form-control"/>
</li></div>
<br>
<li><label class="btn btn-info">Display attendance sheet</label> </li>
<br/>
//attendance table
<table id = "table" class = "table table-bordered">
<thead class = "alert-info">
<tr>
<th>Student ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Present</th>
<th>Absent</th>
</tr>
</thead>
<tbody>
<?php
$q_student = $conn->query("SELECT * FROM `student`")
or die(mysqli_error());
while($f_student = $q_student->fetch_array()){
?>
<tr>
<td><input name="student_no" value="<?php echo
$f_student['student_no']?>" class="form-control"
readonly/></td>
<td><?php echo $f_student['firstname']?></td>
<td><?php echo $f_student['lastname']?></td>
<td><input type="radio" name="attendance[<?php echo
$f_student['student_no']; ?>]" value="1"
class="form-control"></td>
<td><input type="radio" name="attendance[<?php echo
$f_student['student_no']; ?>]" value="0"
class="form-control"></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
//Insert Record into attendancetable
if(isset($_POST['submit']))
{
foreach ($_POST['attendance'] as $attendance){
$faculty_id =
mysqli_real_escape_string($conn,$_SESSION['faculty_id']);
$student_no =
mysqli_real_escape_string($conn,$_POST['student_no']);
$courses = mysqli_real_escape_string($conn,$_POST['courses']);
$semesters = mysqli_real_escape_string($conn,$_POST['semesters']);
$subjects = mysqli_real_escape_string($conn,$_POST['subjects']);
$session_no = mysqli_real_escape_string($conn,$_POST['session_no']);
$date = mysqli_real_escape_string($conn,$_POST['date']);
$time_from = mysqli_real_escape_string($conn,$_POST['time_from']);
$time_to = mysqli_real_escape_string($conn,$_POST['time_to']);
$attendance=var_dump($_POST['attendance']);
$conn=mysqli_connect("localhost","root","","db_sars");
$ins="INSERT INTO att( faculty_id,student_no,courses,semesters,subjects,session_no,date,time_from,time_to,attendance_status)
VALUES ('$faculty_id','$student_no','$courses','$semesters','$subjects','$session_no','$date','$time_from','$time_to','$attendance')";
}
if(#mysqli_query($conn,$ins))
{
echo "inserted";
}
else
{
die(#mysqli_error());
}
}
?><input type="submit" name="submit" value="Submit Form" class="btn btn-info"></form>
In the code you posted, you are not assigning the attendance data ($_POST['attendance']) to the $attendance variable due to use of var_dump.
You wrote:
$attendance=var_dump($_POST['attendance']);
From the php manual for var_dump:
Return Values
No value is returned.
Instead, try formatting this line like you did all the rest:
$attendance = mysqli_real_escape_string($conn,$_POST['attendance']);
Adding multiple students at a time- update
It appears that you are printing each student id in the front end table. I think that would be a better field to loop with. However, with your current front end code, it is creating a new post variable for each student id. Instead you probably want this instead:
<td><input name="student_no" value="StudentID[<?php echo $f_student['student_no']?>]" class="form-control" readonly/></td>
Note how this turns the student id into an array of student ids rather than a ton of different variables of each student ID.
Then for your foreach:
foreach ($_POST['StudentID'] as $StudentID) {
$StudentID is now updated for each iteration and allows you to look up the appropriate index in the other arrays. And you would change the pertinent rows as:
$student_no = mysqli_real_escape_string($conn,$StudentID); // The student ID is set by as in foreach
$StudentAttendance = $_POST['attendance'][$StudentID]; // Get the attendance status by looking up the student by student id in the attendance array
$attendance=mysqli_real_escape_string($conn,$StudentAttendance); // Escape input to avoid table disasters (reference Little Bobby Tables)
I haven't tested your code and understand that there is a lot more going on than this. However, it's probably best to tackle one thing at a time. What you have to do is create a loop to go over each student, and each field that you want to collect should be posted as an array with an index for each student by their student id.
Using var_dump is useful for understanding what a variable contains (for instance, it would help us to see what your variables contain if you would post the var_dump output with your question). If you have error messages, please post them as it helps guide us in the right direction as we attempt to help you out.
Related
My code is running a cycle that adds textareas where the user can input queries, which the div it is in also contains a dropdown with a list of servers available to run those queries in. At first it was working just fine for only the first iteration and not showing the list at all in the next dropdowns, but while I was messing with the code and pasted the query inside the cycle all dropdowns were filled but the server ID of the first textarea stopped being posted correctly, instead sending the value that's currently in the database. By other words it won't update properly.
<?php
if(mysqli_num_rows($result_query) > 0){
while($rowq = mysqli_fetch_assoc($result_query)){
$sql_servers = "SELECT id, name, address FROM servers ORDER BY id ASC";
$result_servers = mysqli_query($link, $sql_servers);
?>
<table>
<form name="formStep" method="post" action="">
<br>
<tr>
<textarea class="form-control scrollabletextbox" id="query<?php echo $rowq['step']?>" name="query<?php echo $rowq['step']?>"><?php echo $rowq['query'];?></textarea>
</tr>
<tr>
<td width="25%" style="vertical-align:middle;"><select id="server" name="server" class="form-control input-md">
<?php
if (mysqli_num_rows($result_servers) > 0) {
while($rows = mysqli_fetch_assoc($result_servers)){ ?>
<option value="<?php echo $rows["id"];?>" <?php if($rows['id']==$row_query2['id_server']) echo 'selected=\"selected\"' ?> ><?php echo $rows["name"];?></option>
<?php
}
}?>
</select>
</td>
<td style="padding:10px;"><input type="submit" name="submit" formaction="save.php?i=4&id=<?php echo $id; ?>&s=<?php echo $rowq['step'];?>" class="btn btn-block btn-primary" value="<?php echo $lableSave; ?>"></td>
</tr>
<?php
}
}?>
</form>
</table>
If I echo the server variable in the save.php it will not update for the new selected value in the dropdown, but it will work for all the subsequent iterations of the cycle.
Any way to solve this problem or the previous one before i started trying to hammer the code until it worked would be greatly appreciated.
This is the code for the admin panel of a certain site. Appointments asked by customers will be shown in this page. The admin will be able to change the appointments based on availability.
<?php
while($row = mysqli_fetch_assoc($result)){
?>
<form action="adminEdit.php" method="POST">
<tr>
<td><input type="text" id="name" value="<?php echo $row['Name'];?>"></input></td>
<td><input type="text" id="address" value="<?php echo $row['Address'];?>"></input></td>
<td><input type="text" id="phone" value="<?php echo $row['Phone'];?>"></input></td>
<td><input type="text" id="license" value="<?php echo $row['Car_License_No'];?>"></input></td>
<td><input type="text" id="engine" value="<?php echo $row['Car_Engine_No'];?>"></input></td>
<td><input type="text" id="date" value="<?php echo $row['Date'];?>"></input></td>
<td><input type="text" id="mechanic" value="<?php echo $row['Mechanic'];?>"></input></td>
<td><input type="submit" name="submit" value="Change"/></td>
</tr>
</form>
<?php
}
?>
Here, each row of data has a corresponding button which will be used for changing or modifying the records of that particular row. Once the admin changes a specific appointment it should get updated in database.
My problem is, all the rows are getting generated by a while loop. Now how can I access a specific row when the change button of that specific row has been clicked ? As the rows are getting generated by a loop, I wont be able to access them bynameoridbecause all of them will have the samenameandid`.
Searched for relevant questions but none of them matched with my scenario. It will be of great help getting an answer. Thanks in advance.
Personally I'd be inclined to take the output from being in the loop for better control over the data and resolving the issue. You're also creating a new form on each loop.
Just loop the DB and create a new variable, then use that variable to output the data in the form.
Example code to show the basic idea, not tested or stating it's complete etc:
while ($row = mysqli_fetch_assoc($result)) {
$someNewVar[$row['id']] = $row;
// The 'id' index would be from DB which identifies individual rows
}
?>
<form action="adminEdit.php" method="POST">
<?php
foreach ($someNewVar as $index => $value) {
?>
<tr>
<td>
<input
type="text"
id="<?php echo $index;?>"
value="<?php echo $value['Name'];?>">
</input>
</td>
<td>
<input
type="submit"
name="submit"
value="Change"/>
</td>
</tr>
<?php
}
?>
</form>
Then you'd need to have the row ID passed from clicking the submit button.
On a side note, this whole approach could be tidied up, and data should be obtained in one file separate to where you output it.
Then in the file which obtains the data you can set the array to something which is also identified in the output file to manage if no data was obtained.
ie
getData.php
while ($row = mysqli_fetch_assoc($result)) {
$dataFromDb[$row['id']] = $row;
}
$someNewVar = !empty($dataFromDb) ? $dataFromDb : false;
showData.php
if ($someNewVar) {
// do the loop and form
} else {
echo 'sorry no data found';
}
Sorry I'm a bit of a noob when it comes to PHP but I just wondered if someone had an idea on how I could solve this PHP/SQL problem.
I have a PDO statement that gets all users from a database.
With the array of users from the database I create a foreach loop to display all of the users in a table which I want to use to select a specific user, enter a number in the row of the user I select, then click submit and store the users name and also the number. I will use this information to populate another database later.
My question is, I cant seem to reference the user or the number in the table to extract the user and number I enter. When I try and request the numbered entered in the index.php, it will only ever display a number if I enter a number for a the final user in the table. When I try and view the FullName it never works and I get 'Undefined index: FullName' error.
I also specified to 'POST in the form but it doesnt seem to be doing that.
Does anyone have any ideas?
Thanks
//function.php
function getName($tableName, $conn)
{
try {
$result = $conn->query("SELECT * FROM $tableName");
return ( $result->rowCount() > 0)
? $result
: false;
} catch(Exception $e) {
return false;
}
}
//form.php
<form action "index.php" method "POST" name='form1'>
<table border="1" style="width:600px">
<tr>
<th>Name</th>
<th>Number Entered</th>
<tr/>
<tr>
<?php foreach($users as $user) : ?>
<td width="30%" name="FullName">
<?php echo $user['FullName']; ?>
</td>
<td width="30%">
<input type="int" name="NumberedEntered">
</td>
</tr>
<?php endforeach; ?>
</table>
<input type="submit" value="submit"></td>
</form>
//index.php
$users = getName('users', $conn);
if ( $_REQUEST['NumberedEntered']) {
echo $_REQUEST['NumberedEntered'];
echo $_REQUEST['FullName'];
}
The variable FullName isn't transmitted by your form to index.php. Only values of form elemnts are sent. You can add a hidden form field, that contains FullName like this:
<input type="hidden" name="FullName" value="<?php echo $user['FullName']">
But your second problem is, that your foreach loop will create several input fields with the exact same name. You won't be able to recieve any of the entered numbers, except the last one. have a look at this question for possible solutions.
Update
Putting each row in individual form tags should solve your problem:
<?php foreach($users as $user) : ?>
<form action="index.php" method="POST">
<tr>
<td align="center" width="40%" >
<?php echo $user['FullName']; ?>
<input type="hidden" name="FullName" value="<?php echo $user['FullName']; ?>" />
</td>
<td width="30%">
<input name="NumberedEntered"/>
</td>
<td>
<input type="submit" value="submit"/>
</td>
</tr>
</form>
<?php endforeach; ?>
Dunno if the title makes sense, but I have a variable which would to put it in basic terms would be called like this:
$_POST['something'+$variable2]
I have a form which is for editing selected records, this form contains entries for all previously selected records:
<form name="input" action="editcar.php" method="POST">
<input type="submit" value="Yes">
while($row = mysqli_fetch_assoc($result))
{
echo'
</div>
<table style="color:white">
<tr>
<td style="text-align:right">Manufacture:</td><td><input type="text" name="manufacture'.$row['carIndex'].'" value="'.$row['make'].'"></td>
<td style="text-align:right">Model: </td><td><input type="text" name="model'.$row['carIndex'].'" value="'.$row['model'].'"></td>
</tr>
<tr>
<td style="text-align:right">Colour: </td><td><input type="text" name="colour'.$row['carIndex'].'" value="'.$row['colour'].'"></td>
<td style="text-align:right">Reg: </td><td><input type="text" name="reg'.$row['carIndex'].'" value="'.$row['Reg'].'"></td>
</tr>
<tr>
<td style="text-align:right">Price: </td><td><input type="text" name="price'.$row['carIndex'].'" value="'.$row['price'].'"></td>
<td style="text-align:right">Mileage: </td><td><input type="text" name="mileage'.$row['carIndex'].'" value="'.$row['miles'].'"></td>
</tr>
<tr>
<td style="text-align:right">Max MPH: </td><td><input type="text" name="mph'.$row['carIndex'].'" value="'.$row['mph'].'"></td>
<td style="text-align:right">MPG: </td><td><input type="text" name="mpg'.$row['carIndex'].'" value="'.$row['mpg'].'"></td>
</tr>
</table>
</form>
</div> ';
}
?>
</form>
The form is looped for each record previously chosen, to enable mass editing. The isue arouses when I realised I'd have multiple inputs with the same name, so I did:
<input type="text" name="model'.$row['carIndex'].'" value="'.$row['model'].'">
Placing the primary key of the record it was currently tired to on the end of it's name. Which seemed like a logical way to go about things.
However now I need to call these variables to place in the mysql query and I dunno how to do that, or even if I can.
I have the selected records saved in an array so I have:
foreach ($postid as $carID)
{
$query = "stuff";
mysqli_query($db, $query);
}
Each loop has $carID containing the variables that was put on the end of the form input names.
So something like:
$_POST['something'+$variable2]
is all I can think of but doesn't work.
Any method that works for my overall code is welcome not just a solution to the issue I've made.
Actually your way should work. Just replace the + with . in $_POST['something'+$variable2].
My tip is: use an array as name in your html instead:
<input type="text" name="model[]" value="'.$row['model'].'">
On php-Side you can loop through all $_POST['model'] since its an array now.
You can add the index for every entry in your html, too:
<input type="text" name="model['.$row['carIndex'].']" value="'.$row['model'].'">
PHP uses a dot for concatenation, not + like Java and Javascript:
$_POST['something' . $variable2]
Try something like this:
<form ...>
<?php
while($row = mysqli_fetch_assoc(...):
$index = $row['carIndex'];
?>
<input type="text" name="carmodel[<?php echo $index?>][model]" value="<?php echo $row['model'] ?>">
<?php endforeach; ?>
</form>
This way you will have the data stored in $_POST['carmodel'] as an array indexed by carIndex value as the structure of data in $_POST is defined by names of inputs, here you will have names likee carmodel[1][model] for example so then in post it will be in $_POST['carmodel'][1][model]
you can read here as well
How would I create this array structure in an HTML form?
So i've created an administration page that creates X-number of forms based on how many users we have in our database, each of which have a submit button next to them to submit changes to the dates we have in our DBs. My problem is that when I want to get the value of what gets posted I can't extract exactly what I need from what gets posted. It gets saved into an array called and when I print_r the array I get exactly what I want, which is:
[1] => "whatever date they typed in"
(obviously the 1 changes depending on which item they changed the date of)
I need be able to query my datebase by:
UPDATE users SET subdate="whatever they typed in" WHERE id="the array reference number"
I know exactly what I need to do, I'm just not as familiar with SQL as i'd like to be, so any help would be greatly appreciated. Thanks in advance.
Code for reference:
<div class="form-section grid12" id="changedates">
<h1>Change Dates</h1>
<?php
$query = mysql_query("SELECT * FROM users WHERE admin='y'");
?>
<table>
<?php
while($row = mysql_fetch_assoc($query)) {
?>
<tr>
<td>
<h5><?php echo $row['displayname'];?></h5>
</td>
<td>
<form action="" method="POST">
<input type="text" name="subdate[<? echo $row['id'] ?>]" value="<?php echo $row['submissiondate'];?>">
<input type="text" name="nextupdate[<? echo $row['id'] ?>]" value="<?php echo $row['nextupdate'];?>">
</td>
<td>
<input type="submit" value="Set Date" name="setdate">
</form>
</td>
<?php
}
?>
</table>
</div>
You could use foreach...
foreach ($_POST[nextupdate] as $rowId => $time)
{
// do db update
}
Edit: Just realised you have more than one input per form.
Why not name each input with an array name:
<input type="text" name="form_data[<?= $row_id ?>][subdate]">
<input type="text" name="form_data[<?= $row_id ?>][nextupdate]">
In PHP:
foreach ($_POST[form_data] as $rowId => $values)
{
$subdate = $values[subdate];
$nextupdate = $values[nextupdate];
// do SQL stuff
}