No error, and No changes in the Database - php

I need to set the classid's of the checked checkboxes to be PAID
and the non-checked to be UNPAID.
What can be the problem that is preventing database changing?
Im checking some checkboxes in the html page.
When i press Save the page resets and when i check the database(PhpMyAdmin) there are no changes.
Here's my code :
while($row = mysql_fetch_array($result))
{
echo "<tr>";
?> <input type="hidden" name="id1[]" value="<?php echo $row['classid']; ?>" /><?php
echo "<td>" . $row['class_date'] . "</td>";
echo "<td>" . $row['class_time'] . "</td>";
echo "<td>" . $row['sal_teach'] . "</td>";
echo "<td align=center>" . $row['status_teach'] . "</td>"; ?>
<!--echo "<td><a href='getpaid2.php?id={$row['classid']}'>Get Paid</a></td>";-->
<td align="center"><input name="ONOFF[<?php echo $row['classid']; ?>]" type="checkbox" /></td>
<?php
echo "</tr>";
}
echo "<tr><td colspan=5><input type='submit' name='submit1' value='Save'/></td></tr>";
echo "</table></div>";
if(isset($_POST['submit1'])){
foreach($_POST['id1'] as $id1)
{
$status1 = 'UNPAID';
if (isset($_POST['ONOFF'][$id1]))
{
$status1 = 'PAID';
}
$sql1="UPDATE class SET status_teach='$status1' WHERE classid=$id1";
$myconn=mysql_connect('localhost','root','') or die("Couldn't Connect to the Server");
mysql_select_db('bddschool', $myconn) or die ("message");
mysql_query($sql1,$mycon);
}
}

Should Be :
if (isset($_POST['ONOFF'][$id1])) {
$status1 = 'PAID';
} else {
$status1 = 'UNPAID';
}

Your checkbox is missing a value. It is always empty.
<input name="ONOFF[<?php echo $row['classid']; ?>]" type="checkbox" value="1" />

Related

I have 3 users with different id in the database and it keeps getting the last user id that been loop when I accept or reject?

There are 3 id that been view from this table
$sql = mysqli_query($conn, "SELECT * FROM user_appointment WHERE event = '' ");
while($row = mysqli_fetch_assoc($sql)){
$id = $row["id"];
$date = $row["date"];
$office = $row['office'];
echo "<table>";
echo "<tr>";
echo "<td colspan='2'> <strong>Name: </strong>" . $row['first_name'] . " " . $row['middle_name'] . " " . $row['last_name'] . "</td>";
echo "<td><strong>You're request is: </strong>" . $row['event'] . "</td>";
echo "</tr>";
echo "<tr><td colspan='3'> <strong>Address: </strong>" . $row['address'] . " </td></tr>";
echo "<tr><td colspan='3'> <strong>Office to go: </strong>" . $row['office'] . " </td></tr>";
echo "<tr>";
echo "<td> <strong>Contact#: </strong>" . $row['phone'] . "</td>";
echo "<td> <strong>Request made from: </strong>" . $row['curdate'] . "</td>";
echo "<td> <strong>Time request: </strong>" . $row['time'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'><strong><i>Message: </i></strong><br>". $row['message'] . "</td>";
echo "</tr>";
echo "<tr> <td colspan='3'>";
echo "<center><form method='GET'>
<div class='center'>
<label for=''>Select Date:</label><br>
<input type='date' name='userDate' id='userDate' value='' required>
</div><br>
<button type='submit' name='approveSubmit' class='btn btn-success'>ACCEPT</button>
<button type='submit' name='rejectSubmit' class='btn btn-danger'>REJECT</button>";
echo "</form> </center>";
echo "</td></tr>";
echo "</table>";
echo 'Either I choose one of the users, it still getting the user id that been loop last';
if(isset($_GET['approveSubmit'])){
isset($_GET['userDate']);
$date = $_GET['userDate'];
header("location: ../approve_insert.php?id=$id&date=$date");
}
if(isset($_GET['rejectSubmit'])){
header("location: ../reject_insert.php?id=$id");
}
}
You are not passing the correct $id to your header: Location(...
To solve this you would need to pass the id of the user to the form as well, so this value become available when an user is clicked.
You can do this by adding an extra hidden input to the form you are creating
<input type='hidden' name='id' value='".$id."' />
Also there is no need to place the code that controls the action you want to do inside the loop that creates the table. Just place it above (or below) the code that generates the table
<?php
if(isset($_GET['approveSubmit'])){
$date = $_GET['userDate'];
header('location: ../approve_insert.php?id='.$_GET['id'].'&date='.$date);
exit;
}
if(isset($_GET['rejectSubmit'])){
header('location: ../reject_insert.php?id='.$_GET['id']);
exit;
}
$sql = mysqli_query($conn, "SELECT * FROM user_appointment WHERE event = '' ");
while($row = mysqli_fetch_assoc($sql)){
$id = $row["id"];
$date = $row["date"];
$office = $row['office'];
echo '... table start ...';
echo "<center><form method='GET'>
<div class='center'>
<label for=''>Select Date:</label><br>
<input type='date' name='userDate' id='userDate' value='' required>
</div><br>
<button type='submit' name='approveSubmit' class='btn btn-success'>ACCEPT</button>
<button type='submit' name='rejectSubmit' class='btn btn-danger'>REJECT</button>
<input type='hidden' name='id' value='".$id."' />
";
echo "</form> </center>";
echo '... table end ...';
}
Keep in my mind you would still need to sanitize the input of $_GET['id'] and $_GET['userDate'] before using it in your code/queries
My assumption at this point is that multiple users meet the conditions at the end of your loop. If your goal is to redirect to the location specified, from the first header location call, you'd have to prevent the loop from continuing. Typically this would be done with exit().
header("location: ../reject_insert.php?id=$id");
exit();
Also, you're going to get an error that you can't set headers because you've already output body content. The header("location...") can only be called before your echo ... statements.

Unable to fetch data from the selected checkbox

Here is the html code and php script, I am unable query data for the selected checkbox:
<code>
Search the Database using multiple options:<br /><br />
<form id="Advanced_form" action="checkbox.php" enctype="multipart/form-data" method="POST">
<input type="checkbox" name="checkbox1[]" value="1" /> Location: projectid
<input type="text" name="projectid" size="10" /> basesq30 from
<input type="text" name="start" size="10" /> To
<input type="text" name="end" size="10" />
<br /><br />
<input type="checkbox" name="checkbox1[]" value="2" /> platform:
<select name="key4" onchange="SetText(key4,word4)">
<option selected="selected" value="All">Select</option>
<option value="miseq">miseq</option>
<option value="hiseq">hiseq</option>
</select>.<br /><br />
<input type="checkbox" name="Genotype" value="3" /> run no:
<select multiple="multiple" name="checkbox1[]" size="5">
<option selected="selected" value= "2">2</option>
<option value= "3">3</option>
<option value= "4">4</option>
<option value= "5">5</option>
<option value= "6">6</option>
</select>
<center>
<INPUT TYPE="Reset" VALUE="Reset">
<input style="align: center;" type="Submit" name="submit" value="Search" /></center>
</form>
<?php
/*
Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password)
*/
$link = mysqli_connect("localhost", "root", "root123", "newdb");
// Check connection
if($link === false)
{
die("ERROR: Could not connect. " . mysqli_connect_error());
}
//code
if(isset($_POST['submit']))
{
foreach($_POST['checkbox1'] as $checkbox1)
//printf("%S<br>",$checkbox1);
if($checkbox1==1)
$text=$_POST['projectid'];
$a=$_POST['start'];
$b=$_POST['end'];
{
$sql="select * from qcdata where project_id='$text' and bases_q30>='$a' and bases_q30<='$b'";
}
if($checkbox1==2)
{
$value=$_POST['key4'];
$sql1="select * from qcdata where platform='$value'";
}
}
echo "$sql";
if($result = mysqli_query($link, $sql))
{
if(mysqli_num_rows($result) > 0)
{
echo "<table align='left'>";
echo "<tr>";
echo "<th>sno</th>";
echo "<th>project_id</th>";
echo "<th>file_name</th>";
echo "<th>read_pair</th>";
echo "<th>total_bases</th>";
echo "<th>total_reads</th>";
echo "<th>bases_q20</th>";
echo "<th>bases_q30</th>";
echo "<th>average_read_length</th>";
echo "<th>readlength_range_min</th>";
echo "<th>readlength_range_max</th>";
echo "<th>quality_range_min</th>";
echo "<th>quality_range_max</th>";
echo "<th>phread_range_min</th>";
echo "<th>phread_range_max</th>";
echo "<th>gc_percentage</th>";
echo "<th>a</th>";
echo "<th>t</th>";
echo "<th>g</th>";
echo "<th>c</th>";
echo "<th>n</th>";
echo "<th>platform</th>";
echo "<th>run_no</th>";
echo "<th>creation_by</th>";
echo "<th>creation_date</th>";
echo "<th>last_modified</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" .$row['sno']. "</td>";
echo "<td>" .$row['project_id'] . "</td>";
echo "<td>" .$row['file_name']. "</td>";
echo "<td>" .$row['read_pair'] . "</td>";
echo "<td>" .$row['total_bases'] . "</td>";
echo "<td>" .$row['total_reads'] . "</td>";
echo "<td>" .$row['bases_q20'] . "</td>";
echo "<td>" .$row['bases_q30'] . "</td>";
echo "<td>" .$row['average_read_length'] . "</td>";
echo "<td>" .$row['readlength_range_min'] . "</td>";
echo "<td>" .$row['readlength_range_max'] . "</td>";
echo "<td>" .$row['quality_range_min'] . "</td>";
echo "<td>" .$row['quality_range_max'] . "</td>";
echo "<td>" .$row['phread_range_min'] . "</td>";
echo "<td>" .$row['phread_range_max'] . "</td>";
echo "<td>" .$row['gc_percentage'] . "</td>";
echo "<td>" .$row['a'] . "</td>";
echo "<td>" .$row['t'] . "</td>";
echo "<td>" .$row['g'] . "</td>";
echo "<td>" .$row['c'] . "</td>";
echo "<td>" .$row['n'] . "</td>";
echo "<td>" .$row['platform'] . "</td>";
echo "<td>" .$row['run_no'] . "</td>";
echo "<td>" .$row['creation_by'] . "</td>";
echo "<td>" .$row['creation_date'] . "</td>";
echo "<td>" .$row['last_modified'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Close result set
mysqli_free_result($result);
}
else{
echo "No records matching your query were found.";
}
}
else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
</code>
MySQL DB screenshot images are here part 1 part 2
I would remove the []'s from the name of your checkbox.

Cant seem to EDIT/MODIFY my php table by id

<?php
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
if(!isset($_POST['submit'])){
$result = mysql_query("SELECT * FROM pleasework ORDER BY ID");
$row = mysql_fetch_array($result);
}
?>
<form action="?php echo $_SERVER['PHP_SELF'];?>" id="form2" method="post" name="form2">
<img id="close1" src="X.png" width="25" height="25" onclick ="div_hide1()">
<h2><font size="6">Please change existing data</font></h2>
<hr>
<br>
<font color="yellow">Change Name to: </font><input type="text" name="New" value="<?php echo $row['Name'];?>"/><br><br>
<font color="yellow"> Change Cause to: </font> <input type="text" name="New1" value="<?php echo $row['Cause'];?>"/><br><br>
<font color="yellow">Change Symptom to: </font><input type="text" name="New2" value="<?php echo $row['Symptom'];?>"/><br><br>
<font color="yellow"> Change Gene_affected to: </font><input type="text" name="New3"value="<?php echo $row['Gene_affected'];?>" /><br><br>
<input type="hidden" name="id" value="<?php echo $_GET['ID'];?>"/>
<input type="submit" onclick="clicked(event)" />
</form>
<?php
if(isset($_POST['submit'])){
mysql_query("UPDATE pleasework SET Name= '$_POST[New]' WHERE ID='$_POST[id]'");
mysql_query("UPDATE pleasework SET Cause= '$_POST[New1]' WHERE ID='$_POST[id]'");
mysql_query("UPDATE pleasework SET Symptom= '$_POST[New2]' WHERE ID='$_POST[id]'");
mysql_query("UPDATE pleasework SET Gene_affected= '$_POST[New3]' WHERE ID='$_POST[id]'");
echo "Change Successful<br>" ;
header("Location: databse.php");
mysql_close($con);
}
else {}
?>
This is my php file.
while($row = mysql_fetch_array($result))
{
echo "<TR>";
echo "<TD>" . $row['ID'] ."</TD>";
echo "<TD>" . $row['Name'] . " </TD>";
echo "<TD>" . $row['Cause'] . " </TD>";
echo "<TD>" . $row['Symptom']. " </TD>";
echo "<TD>" . $row['Gene_affected'] . " </TD>";
echo "<TD><font color='red'>Delete row</font> </TD>";
echo "<TD><font color='red'>modify</font> </TD>";
echo "</TR>";
}
And this is the section which has a modify button that links to the edit.php file. The error here is that is doesnt bring over the values in the table to the editing page and then submitting the form doesnt work too. help please
Your code appears a bit confused.
First of all, why to put the modify routine after output the form? Especially since after modify you send the header function, that fails if previously there are some output.
Note also a typo: you forgot to properly open the php tag in the form declaration. Change-it in this way:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" id="form2" method="post" name="form2">
The main problem is that you check if the $_POST[submit] if set, but this is not set, due to the absence of attribute name.
Change it in this way:
<input type="submit" name="submit" onclick="clicked(event)" />
Now your script should work (I don't have tested the sql).
Please also note that your UPDATE routine is redundant: you can reduce the 4 statement to only one in this way:
$result = mysql_query
(
"UPDATE pleasework SET Name='{$_POST[New]}', Cause='{$_POST[New1]}', Symptom='{$_POST[New2]}', Gene_affected='{$_POST[New3]}' WHERE ID={$_POST[id]}"
);
About PHP Original MySQL API:
This extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0
NOTE: mysql_* deprecated, so try to use PDO or mysqli_*.
Simple way:
<?php
if(isset($_POST['submit'])){
$result = mysql_query("UPDATE pleasework
SET Name='".$_POST['New']."',
Cause='".$_POST['New1']."',
Symptom='".$_POST['New2']."',
Gene_affected='".$_POST['New3']."'
WHERE ID=".$_POST['id'].");
if($result ){
echo "Change Successful<br>" ;
header("Location: databse.php");
}
mysql_close($con);
}
YOUR PHP:
while($row = mysql_fetch_array($result))
{ $spaces = " ";
echo "<TR>";
echo "<TD>" . $row['ID'] ."</TD>";
echo "<TD>" . $row['Name'] . $spaces."</TD>";
echo "<TD>" . $row['Cause'] . $spaces."</TD>";
echo "<TD>" . $row['Symptom']. $spaces."</TD>";
echo "<TD>" . $row['Gene_affected'] . $spaces."</TD>";
echo "<TD><a href='delete.php?id=".$row['ID'] ."'>";
echo "<font color='red'>Delete row</font></a>".$spaces."</TD>";
echo "<TD><a href='edit.php?id=" . $row['ID'] ."'>";
echo "<font color='red'>modify</font></a>".$spaces."</TD>";
echo "</TR>";
}

insert multiple rows of data by single submit button using php

How do I retrieve data from a SQL table, modify the data and store it in another database table with multiple rows & columns and with single submit button I want insert every rows at a time I don't know how to get that hidden value and work properly with that
<?php
include"connect_database.php";
if(isset($_POST['submit'])) {
$amt = $_POST['total'];
if($amt > 0) {
$qry = "INSERT INTO attendance(rollno, name, year, attendance, reason) VALUES "; // Split the mysql_query
for($i=1; $i<=$amt; $i++) {
$qry .= "('".$_POST["rollno$i"]."', '".$_POST["name$i"]."', '".$_POST["year$i"]."', '".$_POST["attendance$i"]."', '".$_POST["reason$i"]."' ),"; // loop the mysql_query values to avoid more server loding time
}
$qry = substr($qry, 0, strlen($qry)-2);
$insert = mysqli_query($dbcon, $qry); // Execute the mysql_query
}
// Redirect for each cases
if($insert) {
$msg = '<script type="text/javascript">alert("added");</script>';
}
else {
$msg = '<script type="text/javascript">alert("Server Error, Kindly Try Again");</script>';
}
};
if (isset($_POST['select']))
{
$sql = "SELECT * FROM data WHERE year='" . $_POST['yr'] . "'";
$myData = mysqli_query($dbcon, $sql);
$num = mysqli_num_rows($myData);
echo "<table border=1>
<tr>
<th>Rollno</th>
<th>Name</th>
<th>Year</th>
<th>Attendance</th>
<th>reason</th>
</tr>";
for ($i=0; $i <$num; $i++)
{
$record = mysqli_fetch_array($myData);
echo "<form action=smanage.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name=rollno$i value=" . $record['rollno'] . " </td>";
echo "<td>" . "<input type=text name=name$i value=" . $record['name'] . " </td>";
echo "<td>" . "<input type=text name=year$i value=" . $record['year'] . " </td>";
echo "<td> "."<select name=attendance$i >
<option value=Present >present</option>
<option value=Absent >Absent</option>
</select>"."</td>";
echo "<td>". "<textarea cols=15 rows=2 name=reason$i placeholder=Enter reason ...></textarea>" . "</td>" ;
echo "<td>" . "<input type=hidden name=total value=" . $i-1 . "</td>";
echo "</tr>";
}
echo"</table>";
echo "<input type=submit name=submit value=save class=Button3>";
echo "</form>";
};
mysqli_close($dbcon);
?>
you are opening multiple forms, for each row in your table on.
This causes your html to be invalid, just start the form before displaying the table.
You could use this html
<table>
<?php
for ($i = 0; $i < $num; $i++) {
$record = mysqli_fetch_array($myData);
?>
<tr>
<td><input type="text" name="rollno[<?= $record['rollno'] ?>]" value="<?= $record['rollno'] ?>" </td>
<td><input type="text" name="name[<?= $record['rollno'] ?>]" value="<?= $record['name']?>" </td>
<td><input type="text" name="year[<?= $record['rollno'] ?>]" value="<?= $record['year'] ?>" </td>
<td><select name="attendance[<?= $record['rollno'] ?>]" >
<option value="Present" >present</option>
<option value="Absent" >Absent</option>
</select></td>
<td><textarea cols="15" rows="2" name="reason[<?= $record['rollno'] ?>]" placeholder="Enter reason ..."></textarea></td>
</tr>
<?php
}
?>
</table>
with this your values will every row will be put into the $_POST-Array, you can access the values via the indexes (I am guessing rollno represents the ID of the dataset).
When you really only want to insert all the values into a table, you can leave the index out. Meaning you could write
<td><input type="text" name="rollno[]" value="<?= $record['rollno'] ?>" </td>
Instead of
<td><input type="text" name="rollno[<?= $record['rollno'] ?>]" value="<?= $record['rollno'] ?>" </td>
You don't need the hidden field, you can just count the items in the array.
$total = count($_POST['rollno']);
<?php
include"connect_database.php";
if(isset($_POST['submit'])) {
$amt = $_POST['total'];
$rollnos= $_POST['rollno'];
if($amt > 0) {
$qry = "INSERT INTO attendance(rollno, name, year, attendance, reason) VALUES "; // Split the mysql_query
$i=0;
foreach($rollnos as $rollno) {
$qry .= "('".$rollno."', '".$_POST["name"][$i]."', '".$_POST["year"][$i]."', '".$_POST["attendance"][$i]."', '".$_POST["reason"][$i]."' ),"; // loop the mysql_query values to avoid more server loding time
$i=$i+1;
}
$qry = substr($qry, 0, strlen($qry)-2);
$insert = mysqli_query($dbcon, $qry); // Execute the mysql_query
}
// Redirect for each cases
if($insert) {
$msg = '<script type="text/javascript">alert("added");</script>';
}
else {
$msg = '<script type="text/javascript">alert("Server Error, Kindly Try Again");</script>';
}
};
if (isset($_POST['select']))
{
$sql = "SELECT * FROM data WHERE year='" . $_POST['yr'] . "'";
$myData = mysqli_query($dbcon, $sql);
$num = mysqli_num_rows($myData);
echo "<table border=1>
<tr>
<th>Rollno</th>
<th>Name</th>
<th>Year</th>
<th>Attendance</th>
<th>reason</th>
</tr>";
for ($i=0; $i <$num; $i++)
{
$record = mysqli_fetch_array($myData);
echo "<form action=smanage.php method=post>";
echo "<tr>";
echo "<td>" . "<input type='text' name='rollno[]' value='" . $record['rollno'] . "'> </td>";
echo "<td>" . "<input type='text' name='name[]' value='" . $record['name'] . "'> </td>";
echo "<td>" . "<input type='text' name='year[]' value='" . $record['year'] . "'> </td>";
echo "<td> "."<select name='attendance[]' >
<option value='Present' >present</option>
<option value='Absent' >Absent</option>
</select>"."</td>";
echo "<td>". "<textarea cols='15' rows='2' name='reason[]' placeholder='Enter reason ...'></textarea>" . "</td>" ;
echo "<td></td>";
echo "</tr>";
}
echo "<input type='hidden' name='total' value='" . $i-1 . "'>";
echo"</table>";
echo "<input type='submit' name='submit' value='save' class='Button3'>";
echo "</form>";
};
mysqli_close($dbcon);
?>

Selecting values from the database and make the checkbox checked if it does exist

//query to check if part id number exists in table ATTEND where service id = ...
$result2 = mysql_query("SELECT * FROM attend WHERE SIDno='$SIDno' and ServiceID='$id");
//if exists $ok = true;
if (mysql_num_rows($result2)>0) {
$ok == true;
}
echo "<tr bgcolor=$bgcolor>";
echo "<td><a name=$row1[0] id=$row1[0]>$row1[0]</td>";
echo "<td>" . $row1[1] . "</td>";
echo "<td>" . $row1[5] . "</td>";
echo "<td>" . $row1[2] . "</td>";
echo "<td>" . $row1[3] . "</td>";
echo "<td><input type='checkbox' name='checkbox[]' value=" . $row1[0];
if ($ok == true) {
echo 'disabled="disabled" checked="checked"';
}
echo "></td>";
echo "<input type='hidden' name='ServiceID' value=" . $id . ">";
echo "<input type='hidden' name='Year' value=" . $Year . ">";
echo "<input type='hidden' name='Stype' value='Recollection'>";
echo "</tr>";
}
}
echo "<tr>
<td colspan='5' align='right' bgcolor='#FFFFFF'><input name='SUBMIT1' type='submit' id='SUBMIT'value='SUBMIT'></td>
</tr>";
How can implement that on the next load if the value of the checkbox is already available in the database it will now be checked. but if it is not yet existing i can check it and save it to the database.
I'm guessing the problem lies in this line:
if (mysql_num_rows($result2)>0) {
$ok == true;
}
It should be:
if (mysql_num_rows($result2)>0) {
$ok = true;
}
In the first snippet you are just testing if $ok is equal to true, while in the second example an actual assignment to the variable is performed.
Remember:
= != ==

Categories