Unable to fetch data from the selected checkbox - php

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.

Related

How to post a checkbox value with database content in it? PHP&MYSQL

I got the following issue.
I want to set a value from a database query to a checkbox value and then post the checkbox value to another file.
$sql = "SELECT * FROM Kunde WHERE UserID = $UserID";
foreach ($db->query($sql) as $zeile)
{
echo "<tr>";
//echo "<td>"; echo $zeile['id']; echo "</td>";
echo "<td>"; echo $zeile['Name']; echo "</td>";
echo "<td>"; echo $zeile['Vorname']; echo "</td>";
echo "<td>"; echo $zeile['Strasse']; echo "</td>";
echo "<td>"; echo $zeile['PLZ']; echo "</td>";
echo "<td>"; echo $zeile['Ort']; echo "</td>";
echo "<td>"; echo $zeile['Rufnummer']; echo "</td>";
echo "<td>"; echo $zeile['Email']; echo "</td>";
echo "<td>"; echo $zeile['Datum']; echo "</td>";
echo "<td>"; echo $zeile['Verlauf']; echo "</td>";
echo "<form action='update.php' method='post'>";
$id = $zeile['id'];
echo "<td>"; echo "<label>"; echo '<input type="checkbox" name="edit" value="'; echo $id; echo '"/>'; echo "</label>"; echo "</td>";
echo "<td>"; echo "<label>"; echo '<input type="checkbox" name="delete">'; echo "</label>"; echo "</td>";
echo "<td>"; echo "<button type='submit' class='submit'>Submit</button>"; echo "</td>";
Is this even possible? I also tried
echo '<input type="checkbox" name="edit" value="'; echo $zeile['id']; echo '"/>';
but without success. Any ideas to manage this would be great. THX
You cannot set the value of a checkbox, but you can have a hidden input that holds a value for the checkbox.
HTML
<input type='checkbox' name='myCheckbox' />
<input type='hidden' name='myCheckbox_data' value='<?= $data ?>' />

Send multiple values from a foreach Loop via a Variable

I have a foreach loop (see below):
<form action="code/update-to-dispatched.php" method="post" name="markAsDispatched">
<?php
foreach ($orders as $row) {
$_POST['Username'] = $row['Username'];
echo "<tr class='even'>";
echo "<td>";
echo "<strong>Order Date:</strong> ". $row['OrderDate'] ." <br />";
echo "</td>";
echo "<td>";
echo "<strong>Order ID:</strong> ". $row['OrderID'] ."";
echo "</td>";
echo "<td>";
echo "<strong>Username:</strong> <input type='text' name='username' value=". $row['Username'] ." readonly style='border: 0; background: none;'>";
echo "</td>";
echo "<td>";
echo '<input type="checkbox" name="chkBox[]" id="chkBox" value="'. $row['OrderID'] .'">';
echo "</td>";
echo "</tr>";
}
?>
<span class="tag">CHECK ALL</span>
<span class="tag">UNCHECK ALL</span>
<input type="submit" name="markAsDispatched" value="MARK AS DISPATCHED" />
</form>
I currently have 3 orders in my Database so the code above shows 3 orders. I am trying to pass all of the email addresses from the field via the $_POST['Username']. Why does the post variable only equal to the last email rather than a comma separated list such "email1#email.com, email2#email.com, email3#email.com"?
If you want a comma-delimited list, try this:
$usernames = array();
foreach ($orders as $row) {
//$_POST['Username'] = $row['Username'];
$usernames[] = $row['Username'];
echo "<tr class='even'>";
echo "<td>";
echo "<strong>Order Date:</strong> ". $row['OrderDate'] ." <br />";
echo "</td>";
echo "<td>";
echo "<strong>Order ID:</strong> ". $row['OrderID'] ."";
echo "</td>";
echo "<td>";
echo "<strong>Username:</strong> <input type='text' name='username' value=". $row['Username'] ." readonly style='border: 0; background: none;'>";
echo "</td>";
echo "<td>";
echo '<input type="checkbox" name="chkBox[]" id="chkBox" value="'. $row['OrderID'] .'">';
echo "</td>";
echo "</tr>";
}
echo implode(',', $usernames);
You should never override the $_POST parameters by the way, it's very bad practise.

My delete button is not functioning as expected

I'm stuck in the delete function, I wonder why my delete button is not functioning, and I already edited my code.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$semester = ($_POST["semester"]);
$level = ($_POST["level"]);
}
?>
Here is the form method:
<form method="post" action="<?php echo($_SERVER["PHP_SELF"]);?>" enctype="multipart/form-data">
Here is to display the data in table form, and SELECT * is functioning
$sql = mysqli_query ($connection, "SELECT * FROM subject");
echo " <table>
<th>Semester</th>
<th>Level</th>
</tr>";
while($record = mysqli_fetch_assoc ($sql)){
echo "<tr>";
echo "<td>" . $record['semester'] . "</td>";
echo "<td>" . $record['level'] . "</td>";
echo "<td>" . "<input type=submit name=delete value=Delete>" . "</td>";
echo "</tr>";
}
This is the delete button code
if (isset($_POST['delete']))
{
$delete = mysqli_query ($connection, "DELETE FROM subject WHERE semester = '($_POST[semester])'");
}
Try this :
while($record = mysqli_fetch_assoc ($sql)){
echo "<tr>";
echo '<form action="mypage.php" method="post">';
echo "<td>" . $record['semester'] . "</td>";
echo "<td>" . $record['level'] . "</td>";
echo "<td>" . $record['course'] . "</td>";
echo "<td>" . $record['subject'] . "</td>";
echo "<td>" . $record['section'] . "</td>";
// And add field form hidden
echo '<input type="hidden" name="semester" value="'.$record['semester'].'">';
echo "<td>" . '<input type="submit" name="delete" value="Delete">' . "</td>";
echo "</form>";
echo "</tr>";
}
if (isset($_POST['delete']) && isset($_POST['semester']))
{
$stmt = $connection->prepare('DELETE FROM subject WHERE semester = ?');
// if $_POST['semester'] is integer else see http://php.net/manual/en/mysqli-stmt.bind-param.php
$stmt->bind_param('i', $_POST['semester']);
$stmt->execute();
}

How to get data from mysql database?

I am having problem in getting values from db. Iam new in php
I am using checkboxes to get values from database. Only checked values should be printed.
<form method="POST" action="gradoviexport.php" id="searchform">
<div id="GRADOVI BIH">
<h3>GRADOVI BOSNE I HERCEGOVINE</h3><hr/>
<input type="checkbox" name="gradovi[]" value="sarajevo"> Sarajevo
<input type="checkbox" name="gradovi[]" value="banovici"> Banovići
<input type="checkbox" name="gradovi[]" value="banjaluka"> Banja Luka
<input type="checkbox" name="gradovi[]" value="bihac"> Bihać
<input type="checkbox" name="gradovi[]" value="bileca"> Bileća
</div>
<div id="snimi">
<input type="submit" name="submit" value="EXPORT">
</div>
</form>
If Sarajevo is checked I want to print values from database. It does not have to be only one value checked If all values are checked it should print all values.
$con=mysqli_connect("$host","$username","$password", "$database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//connecting to db
$variable=$_POST['grad'];
foreach ($variable as $variablename)
{
$sql_select="SELECT * FROM `clanovi` WHERE `GRAD` = $variablename " ;
$queryRes = mysql_query($sql_select);
print"$sql_select";
}
echo "<table border='5'>
<tr>
<th>IME</th>
<th>PREZIME</th>
<th>FIRMA</th>
<th>ADRESA</th>
<th>TELEFON</th>
<th>FAX</th>
<th>MOBITEL</th>
<th>EMAIL </th>
<th>WEB_STRANICA </th>
<th>GRAD </th>
<th>KATEGORIJA </th>
</tr>";
while($row = mysqli_fetch_array($queryRes))
{
echo "<tr>";
echo "<td>" . $row['IME'] . "</td>";
echo "<td>" . $row['PREZIME'] . "</td>";
echo "<td>" . $row['FIRMA'] . "</td>";
echo "<td>" . $row['ADRESA'] . "</td>";
echo "<td>" . $row['TELEFON'] . "</td>";
echo "<td>" . $row['FAX'] . "</td>";
echo "<td>" . $row['MOBITEL'] . "</td>";
echo "<td>" . $row['EMAIL'] . "</td>";
echo "<td>" . $row['WEB_STRANICA'] . "</td>";
echo "<td>" . $row['GRAD'] . "</td>";
echo "<td>" . $row['KATEGORIJA'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
Assume you posted gradovi[] array values to submitted page.
Submit page:
$grad = array();
$grad = $_POST['gradovi']; //get array value
$grad = implode(',',$grad); //convert it into comma separated string
//Insert it into data base
Getting from database:
//fetch the gradovi field from the db like below
echo $row['gradovi']; // print all values
or
$grad = explode(',',$row['gradovi']);
foreach($grad as $check) {
echo $check; //print one by one
}
There is few errors in your code.
There is no escaping of the string from POST data. Use mysqli_real_escape_string
There is an error in your while loop. You redefining mysql query result.
Fixed code:
//connecting to db
$variable=$_POST['grad'];
foreach($variable as $key => $val) {
$variable[$key] = mysql_escape_string($val);
}
$sql_select="SELECT * FROM `clanovi` WHERE `GRAD` IN ('" . implode("','", $variable) . "')" ;
$queryRes = mysql_query($sql_select);
print"$sql_select";

No error, and No changes in the Database

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" />

Categories