I want to ask about this code. I have two dropdown menu and one button. I want to search in sql database what I choose in those drop down menu. What is the sql syntax for search item in sql database by using two drop down menu.
This is my database = test
this is my table
Table = student
name | class | sex | mark |
John | Five | Male | 75
Jashi | Four | Female | 89 |
This is my code
----------
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db ("test");
$whereClauses = '';
$class = count($_POST['class']);
$sex = count($_POST['sex']);
$i = 0;
if (! empty($_POST['class'])) {
foreach ($_POST['class'] as $class) {
$whereClauses .="class='".mysql_real_escape_string($class)."'";
if ($i++ == $class) {
$whereClauses .= " AND";
}
}
}
if (! empty($_POST['sex'])) {
foreach ($_POST['sex'] as $sex) {
$whereClauses .="sex='".mysql_real_escape_string($sex)."'";
}
if ($i++ == $sex) {
$whereClauses .= " AND";
}
}
$sql = "SELECT * FROM student '".$where."' ORDER BY id DESC '".$limit."'";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo $row['name'];
echo $row['class'];
echo $row['sex'];
echo $row['mark'];
}
?>
----------
HTML
<form action="search2.php" method="post">
<select name="class">
<option value="" selected="selected">Class</option>
</select>
<select name="sex">
<option value="" selected="selected">Sex</option>
</select>
<input type="submit" value="search" />
</form>
ANY HELP WOULD BE APPRECIATED
mysql_connect('localhost', 'root', '');
mysql_select_db ("test");
$whereClauses = '';
if (! empty($_POST['class'])) {
foreach ($_POST['class'] as $class) {
$whereClauses[] .="class='".mysql_real_escape_string($class)."'";
}
}
if (! empty($_POST['sex'])) {
foreach ($_POST['sex'] as $sex) {
$whereClauses[] .="sex='".mysql_real_escape_string($sex)."'";
}
}
$where = !empty($whereClauses) ? implode(' and ', $whereClauses) : '';
$sql = "SELECT * FROM student '".$where."' ORDER BY id DESC '".$limit."'";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo $row['name'];
echo $row['class'];
echo $row['sex'];
echo $row['mark'];
}
you can do like this example easy and simple
you got something like this
<form method=post>
<?php
// some fetching over here like
$id = $_GET['id']; // getting id just for example
$sql = mysql_query("SELECT FROM `somewhere` where `id`='$id'");
<select name="somename">
while($someid = mysql_fetch_array($sql))
{
?>
<option value="<?php echo $someid['id']; ?>"><?php echo $displayname; ?></option>
<?php } ?>
</select>
<input type="submit" name="sub" /></form>
<?php
if(isset($_POST['sub']))
{
$search_id = $_POST['somename']; // value from option
$search = mysql_query("SELECT * FROM `somewhere` WHERE `id`='$search_id'");
// or you can use LIKE //
// LIKE THIS //
/*
$search = mysql_query("SELECT * FROM `somewhere` WHERE `something` LIKE %SOMETHING%");
*/
// Just example result down here //
$result = mysql_fetch_array($search);
echo '<table cellpadding="10">
<tr>
<th>Name</th><th>SOMETHING</tH>
</tr>
<tr>
<Td>'.$result['name'].'</td><td>'.$result['something'].'</td>
</tr>
</table>';
}
?>
your Solution is here, try this
Database Table Structure
CREATE TABLE IF NOT EXISTS `student` (
`name` varchar(255) NOT NULL,
`class` varchar(255) NOT NULL,
`sex` varchar(255) NOT NULL,
`mark` int(12) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Insert Record
INSERT INTO `student` (`name`, `class`, `sex`, `mark`) VALUES
('John', 'Five', 'Male', 75),
('Jashi', 'Four', 'Female', 89);
<form action="" method="post">
<select name="class">
<option value="">-SELECT CLASS-</option>
<option value="Four">Four</option>
<option value="Five">Five</option>
</select>
<select name="sex">
<option value="">-SELECT GENDER-</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<input type="submit" name="search" value="search" />
</form>
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db ("test");
extract($_POST);
if(isset($search))
{
if($class!='' || $sex!='')
{
$wh='';
if($class!='') $wh.=" AND class='".$class."'";
if($sex!='') $wh.=" AND sex='".$sex."'";
$qry=mysql_query("SELECT * FROM student where 1 ".$wh."");
$total_record=mysql_num_rows($qry);
if($total_record>0)
{
echo "<table border='1'>
<tr>
<td>Name</td>
<td>Class</td>
<td>Sex</td>
<td>Mark</td>
</tr>
";
while($row=mysql_fetch_array($qry))
{
echo "<tr>
<td>".$row['name']."</td>
<td>".$row['class']."</td>
<td>".$row['sex']."</td>
<td>".$row['mark']."</td>
</tr>";
}
}
else
{
echo "No Record Found";
}
}
else
{
echo "No Record Found";
}
}
?>
Pls mark correct answer if your problem solved
<form action="" method="post">
<select name="class">
<option value="">-SELECT CLASS-</option>
<option value="Four">Four</option>
<option value="Five">Five</option>
</select>
<select name="sex">
<option value="">-SELECT GENDER-</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<input type="submit" name="search" value="search" />
</form>
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db ("test");
extract($_POST);
if(isset($search))
{
if($class!='' || $sex!='')
{
$wh='';
if($class!='') $wh.=" AND class='".$class."'";
if($sex!='') $wh.=" AND sex='".$sex."'";
$qry=mysql_query("SELECT * FROM student where 1 ".$wh."");
$total_record=mysql_num_rows($qry);
if($total_record>0)
{
echo "<table border='1'>
<tr>
<td>Name</td>
<td>Class</td>
<td>Sex</td>
<td>Mark</td>
</tr>
";
while($row=mysql_fetch_array($qry))
{
echo "<tr>
<td>".$row['name']."</td>
<td>".$row['class']."</td>
<td>".$row['sex']."</td>
<td>".$row['mark']."</td>
</tr>";
}
}
else
{
echo "No Record Found";
}
}
else
{
echo "No Record Found";
}
}
?>
<form action="" method="post">
<select name="class">
<option value="">-SELECT CLASS-</option>
<option value="Four">Four</option>
<option value="Five">Five</option>
</select>
<select name="sex">
<option value="">-SELECT GENDER-</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<input type="submit" name="search" value="search" />
</form>
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db ("test");
extract($_POST);
if(isset($search))
{
if($class!='' || $sex!='')
{
$wh='';
if($class!='') $wh.=" AND class='".$class."'";
if($sex!='') $wh.=" AND sex='".$sex."'";
$qry=mysql_query("SELECT * FROM student where 1 ".$wh."");
$total_record=mysql_num_rows($qry);
if($total_record>0)
{
echo "<table border='1'>
<tr>
<td>Name</td>
<td>Class</td>
<td>Sex</td>
<td>Mark</td>
</tr>
";
while($row=mysql_fetch_array($qry))
{
echo "<tr>
<td>".$row['name']."</td>
<td>".$row['class']."</td>
<td>".$row['sex']."</td>
<td>".$row['mark']."</td>
</tr>";
}
}
else
{
echo "No Record Found";
}
}
else
{
echo "No Record Found";
}
}
?>
Wow - what service! Thanks for the PROMPT response to my problem. Your code is working fine. I really do appreciate. Thanks
Related
I have this problem here where when I press the Add button, it should save my selected choices into my table in database.
But when I press it, my table in database did not receive any data.
Did I do something wrong with my code? I need to find a right way to save the data into my designated table.
Any help would be greatly appreciated. Thanks
<?php
include "..\subjects\connect3.php";
//echo "Connection successs";
$query = "SELECT * FROM programmes_list";
$result = mysqli_query($link, $query);
?>
<form name = "form1" action="dropdownindex.php" method="post">
<table>
<tr>
<td>Select Pragramme</td>
<td>
<select id="programmedd" onChange="change_programme()">
<option>select</option>
<?php while($row=mysqli_fetch_array($result)) { ?>
<option value="<?php echo $row["ID"]; ?>"><?php echo $row["programme_name"]; ?></option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td>Select intake</td>
<td>
<div id="intake">
<select>
<option>Select</option>
</select>
</div>
</td>
</tr>
<tr>
<td>Select Subjects</td>
<td>
<div id="subject">
<select >
<option>Select</option>
</select>
</div>
</td>
</tr>
<input type="submit" value="Add" name="send">
</table>
</form>
<?php
if(isset($_POST['Add'])) {
//print_r($_POST);
$course1 = implode(',',$_POST['programmedd']);
$course2 = implode(',',$_POST['intake']);
$course3 = implode(',',$_POST['subject']);
$db->query("INSERT INTO programmes(programme_registered, intake_registered, subjects_registered)
VALUES (' ".$course1." ',' ".$course2." ', ' ".$course3." ' )");
echo $db->affected_rows;
}
?>
<script type="text/javascript">
function change_programme()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?programme="+document.getElementById("programmedd").value,false);
xmlhttp.send(null);
document.getElementById("intake").innerHTML=xmlhttp.responseText;
if(document.getElementById("programmedd").value=="Select"){
document.getElementById("subject").innerHTML="<select><option>Select</option></select>";
}
}
function change_intake()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?intake="+document.getElementById("intakedd").value,false);
xmlhttp.send(null);
document.getElementById("subject").innerHTML=xmlhttp.responseText;
}
</script>
//ajax.php
<?php
$dbhost = 'localhost' ;
$username = 'root' ;
$password = '' ;
$db = 'programmes' ;
$link = mysqli_connect("$dbhost", "$username", "$password");
mysqli_select_db($link, $db);
if (isset($_GET["programme"])) {
$programme = $_GET["programme"];
} else {
$programme = "";
}
if (isset($_GET["intake"])) {
$intake = $_GET["intake"];
} else {
$intake = "";
}
if ($programme!="") {
$res=mysqli_query($link, "select * from intakes where intake_no = $programme");
echo "<select id='intakedd' onChange='change_intake()'>";
echo "<option>" ; echo "Select" ; echo "</option>";
while($value = mysqli_fetch_assoc($res)) {
echo "<option value=".$value['ID'].">";
echo $value["intake_list"];
echo "</option>";
}
echo "</select>";
}
if ($intake!="") {
$res=mysqli_query($link, "select * from subject_list where subject_no = $intake");
echo "<select>";
echo "<option>" ; echo "Select" ; echo "</option>";
while($value = mysqli_fetch_assoc($res)) {
echo "<option value=".$value['ID'].">";
echo $value["subjects"];
echo "</option>";
}
echo "</select>";
}
?>
Your error is where you check for button click.
Change to this
If(isset($_POST['send']))
For future purposes and references, When doing the above, include the name attribute from your button and not the value attribute
I have three list boxes on my HTML form,AUTHOR,GENRE and YEAR.These are linked to a sql database called Authors and a table called books.The idea is to click on one or all of the List boxes,the choice is then placed in variables then into a customised sql statement which extracts the data from the mysql DB and places the result in a table.The result only partly works.If I click one item from each box,then it reponds fine For Example EG Ken Davies(choice in author list) Adventure(choice in genre list) and 2007(choice in year list) Then this works fine.Also if I just click on an authors name,this works fine,or if I just click on genre,this works fine.However when I click on any of the years in the year list box,I dont get anything,despite the years working if I combine them into all three(authors,genre and year).Has anyone any suggestions please Many thanks.
<html>
<head>
<title>My Page</title>
</head>
<body>
<br>
<form name="myform" action="dropdown2.php" method="POST">
<select name="author" size="4">
<option value="ken davies">ken davies</option>
<option value= "arthur smith">arthur smith</option>
<option value="gill rafferty">gill rafferty</option><br />
<option value="molly brown">molly brown</option><br />
<option value="gilbert riley">gilbert riley</option><br />
<input type = "submit" name = "submit" value = "go">
<select name="genre" size="4">
<option value="adventure">adventure</option>
<option value="biography">biography</option>
<option value="crime">crime</option><br />
<option value="romance">romance</option>
<option value="2007">thriller</option>
<input type = "submit" name = "submit" value = "go">
<select name="year" size="4">
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<input type = "submit" name = "submit" value = "go">
<?php
$bird = ( ! empty($_POST['author'])) ? $_POST['author'] : null;
$cat = ( ! empty($_POST['genre'])) ? $_POST['genre'] : null;
$mouse = ( ! empty($_POST['year'])) ? $_POST['year'] : null;
$con = mysql_connect("localhost","root","");
If (!$con){
die("Can not Connect with database" . mysql_error());
}
Mysql_select_db("authors",$con);
if(isset($_POST['author'])&&isset($_POST['genre'])&&isset($_POST['year']))
{
$sql = "SELECT * FROM books WHERE author = '$bird' AND genre = '$cat' AND year = '$mouse' ";
unset($_POST['cat']);
unset($_POST['bird']);
unset($_POST['mouse']);
}
elseif(!isset($_POST['author']))
{
$sql = "SELECT * FROM books WHERE genre = '$cat' ";
unset($_POST['genre']);
}
elseif(!isset($_POST['genre']))
{
$sql = "SELECT * FROM books WHERE author = '$bird'";
unset($_POST['author']);
}
elseif(!isset($_POST['year']))
{
$sql = "SELECT * FROM books WHERE year = '$mouse'";
unset($_POST['author']);
unset($_POST['genre']);
unset($_POST['year']);
$myData = mysql_query($sql,$con);
echo"<table border=1>
<tr>
<th>id</th>
<th>author</th>
<th>title</th>
<th>publisher</th>
<th>year</th>
<th>genre</th>
<th>sold</th>
</tr>";
while($record = mysql_fetch_array($myData)){
echo "<tr>";
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . $record['author'] . "</td>";
echo "<td>" . $record['title'] . "</td>";
echo "<td>" . $record['publisher'] . "</td>";
echo "<td>" . $record['year'] . "</td>";
echo "<td>" . $record['genre'] . "</td>";
echo "<td>" . $record['sold'] . "</td>";
echo "<tr />";
}
echo "</table>";
mysql_close($con);
?>
</form>
</body>
</html>
add mysql_error() for each query you have used and apart review your Html code it seems to be not pretty.
considering your database table name is books and its fields are title, author, genre, year and etc.
<?php
//database connection code here
?>
<form action="aaa.php" method="POST">
<table>
<tr>
<td>Author</td>
<td>
<select name="author">
<option value="">Select</option>
<?php
//gets all the name of the author in the database
$result1 = mysql_query("SELECT distinct author from books ORDER by author");
while($row1 = mysql_fetch_assoc($result2))
{
echo "<option>".$row21['author']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>Genre</td>
<td>
<select name="genre">
<option value="adventure">adventure</option>
<option value="biography">biography</option>
<option value="crime">crime</option><br />
<option value="romance">romance</option>
<option value="2007">thriller</option>
</select>
</td>
</tr>
<tr>
<td>Year</td>
<td>
<select name="year">
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
</select>
</td>
</tr>
<tr>
<td colspan="2"><input type="text" name="submit" value="submit"/></td>
</tr>
</table>
</form>
<?
if(isset($_POST['submit']))
{
echo "<table>";
$result = mysql_query("SELECT * from books where author='".$_POST['author']."' and genre='".$_POST['genre']."' and year='".$_POST['year']."' ");
while($row = mysql_fetch_assoc($result))
{
echo "<tr>":
echo "<td>".$row['title']."</td>":
echo "<td>".$row['author']."</td>":
echo "<td>".$row['genre']."</td>":
echo "<td>".$row['year']."</td>":
echo "</tr>":
}
echo "</table>";
}
?>
I am trying to submit a form value in a database with php. In form a select box value comes from database.
<?php include_once 'header.php';
$sql="SELECT uid,name FROM emitra_basic where block='$user'";
$result = $conn->query($sql);
//form validion
if(isset($_POST['submit']))
{
$eid =$_POST["eid"];
if($eid=="blank")
{
$flag=1;
$idErr="please Select E-MITRA";
}
$miatm =trim($_POST["miatm"]);
if(empty($miatm) || !preg_match("/^[a-zA-Z0-9 ]*$/",$miatm)) {
$flag=1;
$miErr="Please Enter Valid Id";
}
.............like this
if($flag==0)
{
$sqll="insert into **********";
}
//my form is
<form id="basic" method="post" name="basic">
<select class="select-style gender" name="eid">
<option value="blank">Please Select E-MITRA ID</option>
<?php
while($row=mysqli_fetch_array($result))
{
?>
<option value="<?php echo $row['uid']; ?>"><?php echo $row['uid']." (" . $row['name'] .")"; ?></option>
<?php
}
?>
</select>
<p class="contact"><label for="bid">Micro-ATM Serial No</label></p>
<input type="text" name="miatm" value ="<?php if (isset($miatm)) echo $miatm; ?>" /> <?php echo $miErr; ?>
<p class="contact"><label for="bid">Micro-ATM TID No</label></p>
<input type="text" name="tid" value ="<?php if (isset($tid)) echo $tid; ?>" /> <?php echo $tiErr; ?>
<input class="buttom" name="submit" id="submit" value="Add Me" type="submit">
Its seems Ok.but when i tried to submit the form if some of one field remain empty then its show blank value in select box.
how can i remain the same selected value in select box even if textbox remain empty.
You need to retain the value of drop down after form submit.
User selected attribute of select option.
<?php
if (isset($_POST['submit'])) {
$eid =$_POST["eid"];
if ($eid=="blank") {
$flag=1;
$idErr="please Select E-MITRA";
}
}
$sql="SELECT uid,name FROM emitra_basic where block='$user'";
$result = $conn->query($sql);
?>
<select class="select-style gender" name="eid">
<option value="blank">Please Select E-MITRA ID</option>
<?php
while($row=mysqli_fetch_array($result)) {
$selected = (isset($_POST["eid"]) && $_POST["eid"] == $row['uid']) ? 'selected="selected"' : '';
?>
<option value="<?php echo $row['uid']; ?>" <?php echo $selected;?>><?php echo $row['uid']." (" . $row['name'] .")"; ?></option>
<?php
}
?>
</select>
You need to use selected="" or selected="selected" after submission in your select tag as a attribute as:
<?
$sql="SELECT uid,name FROM emitra_basic where block='$user'";
$result = $conn->query($sql);
?>
<select class="select-style gender" name="eid">
<option value="blank">Please Select E-MITRA ID</option>
<?php
while($row=mysqli_fetch_array($result))
{
$selected = ((isset($_POST["eid"]) && $_POST["eid"] == $row['uid']) ? 'selected=""' : '');
?>
<option <?=$selected?> value="<?php echo $row['uid']; ?>"><?php echo $row['uid']." (" . $row['name'] .")"; ?></option>
<?php
}
if(isset($_POST['submit']))
{
$eid = $_POST["eid"];
if($eid=="blank")
{
$flag=1;
$idErr="please Select E-MITRA";
}
?>
</select>
Side Note:
In your question ist two lines are not inside the php, i hope this is type error.
I need a help exporting search query by clicking on export button. I am able to get data from database to the browser. However, when I click on export button, it refreshes the page, and doesnt do anything.
I am new to php and and programming. Any help will be appreciated :)
<?php
include('dbcon.php');
?>
<form id="searchform" action="index.php" method="post">
<table>
<tr>
<td class="searchleftcol"><h3>Service:</h3></td>
<td>
<select id="service" name="service" class="searchoption">
<option value="">-- Select Service Name --</option>
<?php
$resultservice = mysqli_query($con,"Select * from services") ?>
<?php
while ($line = mysqli_fetch_array($resultservice)) {
?>
<option value="<?php echo $line['serviceid'];?>"> <?php echo $line['service'];?> </option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>
<h3>Environment:</h3>
</td>
<td>
<select id="environment" name="environment" class="searchoption">
<option value="">-- Select Environment --</option>
<?php
$resultdomain = mysqli_query($con,"Select * from evn") ?>
<?php
while ($line = mysqli_fetch_array($resultdomain)) {
?>
<option value="<?php echo $line['envid'];?>"> <?php echo $line['env'];?> </option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>
<h3>Status:</h3>
</td>
<td>
<select name="status" class="searchoption">
<option value="Active">Active</option>
<option value="Inactive">Inactive</option>
</select>
</td>
</tr>
</table>
<input type="reset" name="reset">
<input type="submit" name="submit" value="Search">
<input type="submit" name="export" value="Export" />
</ul>
</form>
<?php
if (isset($_POST['submit'])) {
if (empty($_POST['service'])) {
echo "Please select service in dropdown" . "</br>";
}
else {
$service = $_POST['service'];
}
if (empty($_POST['environment'])) {
echo "Please select Environment in dropdown" . "</br>";
}
else {
$env = $_POST['environment'];
}
if ((!empty($service)) && (!empty($env))) {
$sql="SELECT * from servers";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
$mydata = mysqli_query($con,$sql);
$rowcount = mysqli_num_rows($mydata);
// Here I erased code that displays data from MySQL.
if (isset($_POST['export'])) {
if (empty($_POST['service'])) {
echo "Please select service in dropdown" . "</br>";
}
else {
$service = $_POST['service'];
}
if (empty($_POST['environment'])) {
echo "Please select Environment in dropdown" . "</br>";
}
else {
$env = $_POST['environment'];
}
if ((!empty($service)) && (!empty($env))) {
$sql="SELECT * from servers";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
$mydata = mysqli_query($con,$sql);
$rowcount = mysqli_num_rows($mydata);
//Programetically get the Headings of the excel columns
$columns_total = mysqli_num_fields($sql);
for ($i = 0; $i < $columns_total; $i++) {
$heading = mysql_field_name($sql, $i);
$contents .= '"'.$heading.'",';
}
$contents .="\n";
// Get Records from the table
while ($row = mysqli_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$contents.='"'.$row["$i"].'",';
}
$contents.="\n";
}
// Remove html and php tags etc.
$contents = strip_tags($contents);
//header to make force download the file
Header("Content-Disposition: attachment; filename=ProductsReport".date('d-m-Y').".csv");
print $contents;
}
}
mysqli_close($con);
}
?>}
Thanks,
Ray
I dynamically create table rows with checkboxes. Check a few of them and then perform and update query on the selected ones. But the problem I face is that only the first selected record gets updated even though I have used foreach loop.
Following is the code.
<?php
$checkbox = $_POST['pr'];
$year = $_POST['promoteyearselect1'];
$semester = $_POST['promotesemselect1'];
# $db = mysql_connect("abc", "abc", "");
mysql_select_db("abc");
foreach($checkbox as $value){
if(isset($checkbox)){
if(($semester%2)==0) {
$strSQL = "UPDATE student SET year='".++$year."', semester='".++$semester."' WHERE enrollment='".$value."'";
$rs = mysql_query($strSQL);
if($rs){
echo 'Promotion Successful';
header("location:page1.php");
echo '<script> alert("Promotion Successful");</script>';
}
else echo "Sorry, but that did not work. ";
}
else{
$strSQL = "UPDATE student SET semester=".++$semester."' WHERE enrollment='".$value."'";
$rs = mysql_query($strSQL);
if($rs){
echo 'Promotion Successful';
header("location:page1.php");
echo '<script> alert("Promotion Successful");</script>';
}
else echo "Sorry, but that did not work. ";
}
}
}
mysql_close($db);
?>
Even though I use the foreach loop to get through the array of checkboxes, still only the first checked record gets updated.
This is the html part
<div class="dropdown dropdown-dark">
<select name="promoteyearselect1" id="promoteyearselect1" class="dropdown-select" onfocus="showhidephdmenu()" form="promotionform" required>
<option value="">Select an option</option>
<div id="yearselect1">
<option value="1">1st</option>
<option value="2">2nd</option>
<option value="3">3rd</option>
<option value="4">4th</option>
<option value="5">5th</option>
</div>
</option>
</select>
</div>
<div class="dropdown dropdown-dark">
<select name="promotesemselect1" id="promotesemselect1" class="dropdown-select" form="promotionform" required>
<option value="">Select an option</option>
<option value="1">1st</option>
<option value="2">2nd</option>
<option value="3">3rd</option>
<option value="4">4th</option>
<option value="5">5th</option>
<option value="6">6th</option>
<option value="7">7th</option>
<option value="8">8th</option>
<option value="9">9th</option>
<option value="10">10th</option>
</select>
</div>
<button id="promotego" class="login-button" style=" position:relative; padding: 0 0 0; " onclick="getpromotestudents()"></button>
</div>
<form id="promotionform" action="promotestudents.php" method="POST">
<div id="promoteresults">
The results will show up here..!!
</div>
<div style=" position:relative; margin-top:10px; padding-left:44%;">
<input type="submit" value="Promoted" class="button black"></input>
Passed Out
</div>
</form>
This is the PHP that gets the records and generates checkboxes.
$i=1;
while($r = mysql_fetch_array($rs)){
echo "<tr>";
echo "<td class='promotetabledata'>".$r[7]."</td>";
echo "<td class='promotetabledata'>".$r[6]."</td>";
echo "<td class='promotetabledata'><input type='checkbox' name='pr[]' value='".$r[7]."'/></td>";
echo "</tr>";
$i++;
}
Here is the modified code that finally worked for me, I added spaces in the if($semester % 2 != 0)
<?php
$checkbox = $_POST['pr'];
$year = $_POST['promoteyearselect1'] + 1 ;
$semester = $_POST['promotesemselect1'] + 1;
$con = mysqli_connect("localhost","root","","university") or die("Error " . mysqli_error($con));
foreach($checkbox as $value){
if(isset($checkbox)){
// echo $value;
if( $semester % 2 != 0) {
// echo $value;
$strSQL = "UPDATE student SET year='".$year."', semester='".$semester."' WHERE enrollment='".$value."'";
$rs = mysqli_query($con, $strSQL);
if($rs){
echo 'Promotion Successful';
header("location:page1.php");
echo '<script> alert("Promotion Successful");</script>';
}
else echo "Sorry, but that did not work. ";
}
else{
$strSQL = "UPDATE student SET semester='".$semester."' WHERE enrollment='".$value."'";
$rs = mysqli_query($con, $strSQL);
if($rs){
echo 'Promotion Successful';
header("location:page1.php");
echo '<script> alert("Promotion Successful");</script>';
}
else echo "Sorry, but that did not work. ";
}
}
}
mysqli_close($con);
?>
I didn't found any checkbox with name 'pr' in your code..If there is any ..please make it an array and try
like <input type="checkbox" name="pr[]">