PHP Firebase input is inputting blank instead of the submitted value? - php

I am working on a grading system but whenever I input a grade, it saves a blank instead of the submitted value.
The form is on the table itself and not on a separate page. I know doing it on a separate page will solve my problem but I want to do it on the table itself.
Like this
Here is the code for the table
<tr>
<th>Student Number</th>
<th>Course</th>
<th>Email</th>
<th>Name</th>
<th>Grade</th>
<th>Remark</th>
<th>Add/Edit</th>
<?php
$table = "Users";
$fetch = $database->getReference($table)->getValue();
if($fetch > 0)
{
foreach($fetch as $key => $row)
{
?>
<tr>
<td> <?= $row['Student_number']; ?> </td>
<td> <?= $row['Course']; ?> </td>
<td> <?= $row['Email']; ?> </td>
<td> <?= $row['Name']; ?> </td>
<?php
if(!$row['grade']
{
?>
<td><input type = "text" name = "grade" id = "grade"></input></td>
<td><button type = "submit" name = "update" value = "<?=$key?>"</button></td>
<td>N/A</td>
}
else
{
?>
<td><?=$row[grade];?></td>
<?php
}
}
}
?>
And here is the code for the input
if(isset($_POST[update]))
{
$key = $_POST[update];
$grade = $_POST[grade];
$postGrade = ['grade' => $grade,];
$table = 'Users;
$updateResult = $database->getReference($table)->update($postGrade);
}
I need help finding what's wrong with my code that makes the input a blank instead of the intended submitted value.

Related

MYSQL query in PHP is deleting all the rows in my table

I'm working on a PHP script that uses delete and I only want to delete 1 row. But everytime I tried to delete 1 row, the SQL executes but deletes the entire rows in the table.
The following is my PHP script
<table>
<tr>
<th>No.</th>
<th>Publisher Code</th>
<th>Publisher Name</th>
<th>City</th>
<th> </th>
</tr>
<!-- PHP FETCH/RETRIEVE FROM DB -->
<?php
include_once 'dbconnect.php';
$sql = mysqli_query($conn, "SELECT * FROM tbl_publisher");
$ctr = 1;
$record = mysqli_num_rows($sql);
if ($record > 0) {
while ($record = mysqli_fetch_array($sql)) {
?>
<tr>
<td> <?php echo $ctr++ ?> </td>
<td> <?php echo $record['TBL_PUBLISHER_CODE']; ?> </td>
<td> <?php echo $record['TBL_PUBLISHER_NAME']; ?> </td>
<td> <?php echo $record['CITY']; ?> </td>
<td id="actions">
<center>
Delete
</center>
</td>
</tr>
<!-- closing tag for php script -->
<?php
}
}
?>
<!-- -->
</table>
and below is my delete.php
if(isset($_GET['del-pub']))
{
$row = intval($_GET['del-pub']);
$sql = mysqli_query($conn,"DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE = $row;");
if ($sql) {
header("Location: publisher.php");
}
else {
echo "<script>alert('Publisher was not deleted.');</script>";
header("Location: publisher.php");
}
}
I want to know how to delete only the 1 row. But it keeps deleting the entire rows in the table.
After dumping the contents of $sql, I found out that my sql syntax is the culprit.
Since TBL_PUBLISHER_CODE uses char as ID. Instead of doing "="
DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE = $row;
I used
DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE LIKE '".$row."'

Deleting record from database with button in PHP

I have to delete a record from database using a button but my delete query does not work. Records are entered in database successfully with insertion query. I followed exact tutorial for php code available on YouTube "How to delete records from database with PHP & MySQL" by "kanpurwebD". The code in tutorial works fine but my code still does not delete record. (I have 2 records entered in database).
My code is as follows:
<div class="container">
<div class="row">
<form action='add_record.php' method='get'><button type='submit' name='id' value='submit' class='btn btn-default'>ADD RECORD</button><br />
</form>
<table class="table table-hover table-responsive">
<thead>
<tr>
<th>Topic #</th>
<th>Name</th>
<th>Admin ID</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
echo '<br />';
$query = "SELECT * FROM tb_topic";
$result = $con->query($query);
if(isset($_POST['submitDeleteBtn'])){
$key = $_POST['keyToDelete'];
$check = "Select * from tb_topic where topic_id = '$key'";
if(mysqli_num_rows($con, $check)>0){
$query_delete = mysqli_query($con,"Delete from tb_topic where topic_id = '$key'");
echo 'record deleted';
}
}
while($query_row = mysqli_fetch_array($result)) {?>
<tr>
<td><?php echo $query_row['topic_id'];?></td>
<td><?php echo $query_row['topic_name'];?></td>
<td><?php echo $query_row['aid'];?></td>
<td><input type = 'checkbox' name = 'keyToDelete' value = "<?php echo $query_row['topic_id'];?>" required></td>
<td><input type="submit" name="submitDeleteBtn" class="btn btn-danger"></td>
</tr>
<?php }
?>
</html>
I got it resolved by using following statement:
if(isset($_GET['delete'])) {
$page = filter_input(INPUT_GET, 'delete', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
$sql = "DELETE FROM tb_topic WHERE topic_id = $page";
}
$_GET() was not taking id as int so I tried typecasting it and it worked for me.

php // what should i do to display all courses?

i need to display all courses at once
in my code only display one course what should i do to fix it ?
when i click delete it reload page and display next courses
my code ::
$course = $conn->query("select * from student_course where student_id = $id ") ;
if($course->num_rows > 0) {
?>
<table class="table" >
<tr>
<thead>
<th> courseName </th>
<th> courseID </th>
<th> teacher </th>
<th> DELETE </th>
</thead> </tr>
<?php
while ($Scourse = $course->fetch_assoc()){
$cid=$Scourse['course_id'];
$tid=$Scourse['teacher_id'];
$teacher = $conn->query("select * from teacher where id = $tid ") ;
if($teacher->num_rows > 0) {
while($teacherC=$teacher->fetch_assoc()){
$course = $conn->query("select * from course where id = '$cid' ") ;
if($course->num_rows>0){
while($Ncourse=$course->fetch_assoc()){ ;
?>
<tr> <td><?php echo $Ncourse['name']; ?> </td>
<td><?php echo $Scourse['course_id']; ?> </td>
<td> <?php echo $teacherC['name']; ?> </td>
<td><form method="post"><button type="submit" name="<?php echo $cid ?>"> Delete </button></form> </td></tr>
<?PHP
if (isset($_POST["$cid"])){
$dcourse = $conn->query("delete from student_course where course_id = '$cid' and
student_id=$id ") ;
header("location:scourse.php");
}
}
}
}
}
}
}
</table>
i try to delete teacher and course query it work as i need
You're handling associate arrays improperly.
You will need a foreach loop to loop through each row in the associate array.
Example:
foreach($Scourse as $x) {
echo $x['name'];
echo $x['course_id'];
}
See Loop through Associate Arrays here:
https://www.w3schools.com/php/php_arrays.asp
Edit: See #Qirel comments

PHP explode function from checkboxes

I have a form where I'm submitting some values (from checkboxes) to another page and on that page I'm using an explode function to break the string inside the array. But I'm getting an additional (+1) value when I put the count() function on the explode.
HTML
<form name = "view" method = "POST" action ="cart.php">
<table align = 'center' width = '100%' border = '4'>
<tr>
<td colspan = '20' align = 'center'><h2> Viewing all the Products </h2></td>
</tr>
<tr align = 'center'>
<th>Item ID</th>
<th>Name</th>
<th>Price</th>
<th>Select</th>
</tr>
<tr align = 'center' class = "odd">
<?php
while($row = mysql_fetch_array($run))
{
$i_id = $row['item_id'];
$i_name = $row['item_name'];
$i_price = $row['item_price'];
?>
<td><?php echo $i_id; ?></td>
<td><?php echo $i_name; ?></td>
<td><?php echo $i_price; ?></td>
<?php
$item = $i_name ." ". $i_price;
?>
<td><input type="checkbox" name="addcart[]" value="<?php echo $item; ?>" onClick="return KeepCount()" />Tick</td>
</tr>
<?php }?><input type = "hidden" name = "check">
<button type= "submit" onclick = "location.href = 'cart.php';" id = "cart">Add to Cart</button> <?php } ?>
</table>
</form>
PHP (on page 2)
$prd = implode(",",$_POST['addcart']);
$final = explode(",", $prd);
for($i=0; $i<=count($final); $i++)
{
echo count($final); //this is where I'm getting the +1 to original count and hence everything falls apart.
echo $final[$i];
}
Note: I have include all the essential files like config.php and everything in the PHP file already.
Why that much extra code, directly do like below:-
foreach($_POST['addcart'] as $val){
echo $val;
}
Since count() starts counting from 1 and for loop starting from 0.
Replace this line:
for($i=0; $i<=count($final); $i++)
with
for($i=0; $i<=count($final)-1; $i++)

Update multiple rows with where clause in codeigniter

I have a list of records from database, see the attachment. How can i insert a vat no for each record using student id(see leftmost column). Each record has a input field(see rightmost column).
Here is my view:
<?php $attributes = array('id' => 'VATformAdd', 'name' =>
'VATformAdd', 'autocomplete' => 'off'); echo form_open('report/addVATno', $attributes);?>
<fieldset>
<legend>Income By Student Admission</legend>
<table class="">
<thead>
<tr>
<th width="15%">Student ID</th>
<th width="15%">Student Form No</th>
<th width="15%">Course</th>
<th width="20%">Student Name</th>
<th width="15%">Admitted by</th>
<th width="10%">Amount</th>
<th width="10%">VAT Serial No</th>
</tr>
</thead>
<tbody>
<?php
if (isset($addmission_income)) {
foreach ($addmission_income as $addmissionincome) {
?>
<tr>
<td><?php echo $addmissionincome->student_id;?></td>
<td><?php echo $addmissionincome->student_form_no;?></td>
<td><?php echo $addmissionincome->course_name;?></td>
<td><?php echo $addmissionincome->student_full_name;?></td>
<td><?php echo user_profile_name($addmissionincome->student_added_by); ?></td>
<td><?php echo $addmissionincome->student_fee_paid;?></td>
<td><input type="text" name="vatno[<?php echo $addmissionincome->student_id;?>]" class="width-100"/></td>
<?php
}
?>
</tr>
<?php
}
?>
</tbody>
</table>
<p>
<input id="submit" name="submit" type="submit" value="Add VAT Number" />
</p>
</fieldset>
<?php echo form_close();?>
Here is my controller:
function addVATno(){
$studentid = $this->input->post('studentid');
foreach($studentid as $a){
if($val[$a]!=''){
$this->report_mdl->addVATno($a);
}
}
}
Here is Model:
function addVATno($a){
$val = $this->input->post('vatno');
$updatevatsl = array(
'dupVATserial_no' => $val);
$this->db->where('student_id', $a);
$query = $this->db->update('data_student_master', $updatevatsl);
return $query;
}
make your view file with a hidden student_id like:
<input type="hidden" name="student_id[]" value="<?php echo $addmissionincome->student_id;?>"/>
and vatno field:
<input type="text" name="vatno[]" class="width-100"/>
Adding the following to your controller will do everything you need (without needing to put any code in your model):
$student_id = $this->input->post('student_id');
$vatno = $this->input->post('vatno');
for ($i = 0; $i < count($student_id); $i++) {
$sm_data[] = array(
'vatno' => $vatno[$i],
'student_id' => $student_id[$i]
);
}
$this->db->update_batch('data_student_master', $sm_data, 'student_id');
Note: It would be better to move the db call to your model to adhere to MVC conventions.
Your issue is that you aren't pulling the correct values from POST.
You are not submitting the student id on its own anywhere in the form, so I can't see how you would be able to access it from POST.
You are also trying to access 'vatno' from POST to insert it into a DB, but 'vatno' in POST is an array.
Because you are using the student id as the array key in your form, you can access both the student ID and the vat number you want to update from the same array. In your controller, you can do:
function addVATno(){
//vatnos is an array, the key is the student id
$vatnos = $this->input->post('vatno');
foreach($vatnos as $key=>$vatno){
$this->report_mdl->addVATno($key, $vatno);
}
}
Then in your model, you just need:
function addVATno($studentid, $vatno){
$updatevatsl = array(
'dupVATserial_no' => $vatno
);
$this->db->where('student_id', $studentid);
$query = $this->db->update('data_student_master', $updatevatsl);
return $query;
}

Categories