I'm working on this code I used Oracle HR database as my database, I'm trying to filter database results based on multiple select, but I keep getting this error:
Warning: oci_execute(): ORA-00933
Warning: oci_fetch_array(): ORA-24374
and no results, my code:
index.php
<html>
<head>
<title>Employees Search</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
function checkinput()
{
if(document.Form2.DEPARTMENT_ID.value=="" && document.Form2.MANAGER_ID.value=="")
{
alert("Please select department or manager!");
return;
}
document.Form2.submit();
}
</script>
</head>
<body>
<form method="get" name="Form2" id="Form2">
<select id="DEPARTMENT_ID" name="DEPARTMENT_ID">
<option value="">DEPARTMENT:</option>
<option value="90">90</option>
<option value="60">60</option>
<option value="100">100</option>
<option value="30">30</option>
</select>
<select id="MANAGER_ID" name="MANAGER_ID" >
<option value="">MANAGER ID:</option>
<option value="100">100</option>
<option value="102">102</option>
<option value="103">103</option>
<option value="108">108</option>
</select>
<input type="button" value=" Search " onclick="checkinput()" />
</form>
<br />
<div id="result">
<?php include "emp.php"; ?>
</div>
</body>
</html>
emp.php
<?php
$conn = oci_connect('hr', ' ', 'My-PC:1521/XE');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
if(isset($_GET['DEPARTMENT_ID'])||isset($_GET['MANAGER_ID'])){
$DEPARTMENT_ID=$_GET['DEPARTMENT_ID'];
$MANAGER_ID=$_GET['MANAGER_ID'];
$sql="SELECT * FROM EMPLOYEES WHERE ";
if($DEPARTMENT_ID != "")
{
$sql = $sql."DEPARTMENT_ID=".$DEPARTMENT_ID;
if($MANAGER_ID != "")
{
$sql = $sql."MANAGER_ID=".$MANAGER_ID;
}
}
else
{
if($MANAGER_ID != "")
{
$sql = $sql."MANAGER_ID=".$MANAGER_ID;
}
}
$stid = oci_parse($conn, $sql);
oci_execute($stid);
echo " <table><tr><th>Employee No.</th><th>First Name</th><th>Last Name</th><th>PHONE NUMBER</th><th>Email</th><th>Job Title</th><th>MANAGER_ID</th><th>DEPARTMENT_ID</th></tr>";
while ($row = oci_fetch_array($stid, OCI_ASSOC )) {
echo "<tr>";
echo "<td>".$row['EMPLOYEE_ID']."</td>";
echo "<td>".$row['FIRST_NAME']."</td>";
echo "<td>".$row['LAST_NAME']."</td>";
echo "<td>".$row['PHONE_NUMBER']."</td>";
echo "<td>".$row['EMAIL']."</td>";
echo "<td>".$row['JOB_ID']."</td>";
echo "<td>".$row['MANAGER_ID']."</td>";
echo "<td>".$row['DEPARTMENT_ID']."</td>";
echo "</tr>";
echo"</table>";
}
}
?>
Everything seems correct expect SQL when Department and manager id is not null.
<?php
$conn = oci_connect('hr', ' ', 'My-PC:1521/XE');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
if(isset($_GET['DEPARTMENT_ID'])||isset($_GET['MANAGER_ID'])){
$DEPARTMENT_ID=$_GET['DEPARTMENT_ID'];
$MANAGER_ID=$_GET['MANAGER_ID'];
$sql="SELECT * FROM EMPLOYEES WHERE ";
if($DEPARTMENT_ID != "")
{
$sql = $sql."DEPARTMENT_ID=".$DEPARTMENT_ID;
if($MANAGER_ID != "")
{
$sql = $sql."AND MANAGER_ID=".$MANAGER_ID;
}
}
else
{
if($MANAGER_ID != "")
{
$sql = $sql."MANAGER_ID=".$MANAGER_ID;
}
}
$stid = oci_parse($conn, $sql);
oci_execute($stid);
echo " <table><tr><th>Employee No.</th><th>First Name</th><th>Last Name</th><th>PHONE NUMBER</th><th>Email</th><th>Job Title</th><th>MANAGER_ID</th><th>DEPARTMENT_ID</th></tr>";
while ($row = oci_fetch_array($stid, OCI_ASSOC )) {
echo "<tr>";
echo "<td>".$row['EMPLOYEE_ID']."</td>";
echo "<td>".$row['FIRST_NAME']."</td>";
echo "<td>".$row['LAST_NAME']."</td>";
echo "<td>".$row['PHONE_NUMBER']."</td>";
echo "<td>".$row['EMAIL']."</td>";
echo "<td>".$row['JOB_ID']."</td>";
echo "<td>".$row['MANAGER_ID']."</td>";
echo "<td>".$row['DEPARTMENT_ID']."</td>";
echo "</tr>";
echo"</table>";
}
}
?>
Changed Line :
$sql = $sql."AND MANAGER_ID=".$MANAGER_ID;
May be reason as SQL failed to parse this query.
As If both are not empty and assumed 10 then you query will be looked like SELECT * FROM EMPLOYEES WHERE DEPARTMENT_ID=10 MANAGER_ID=10; which is not syntactically correct.
Related
Table sorting and searching text filed is not working properly using bootstrap 3. I am basically developing a query builder and making a dynamic table of my data but table sorting and searching is not working properly. Data display accurately but the table sort and search text box do not display data.
<?php
ini_set( "display_errors", 0);
include "include.php";
include "connect_to_mysql.php";
$table='Genomics_data_ncbi';
$display = array();
$qu_mul="";
?>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript">
function formValidation(form)
{
var ccc = document.getElementById("first_box").value;
if(ccc == "") {
alert("Please enter a keyword !!!");
return false;
}
}
var intTextBox=0;
//FUNCTION TO ADD TEXT BOX ELEMENT
function addElement()
{
intTextBox = intTextBox + 1;
var contentID = document.getElementById("content");
var newTBDiv = document.createElement("div");
newTBDiv.setAttribute("id","strText"+intTextBox);
newTBDiv.innerHTML = "<select name='op[]'><option value='and'>AND</option> <option value='or'>OR</option> </select><select name='fieldsi[]'><option value='all'>All Fields</option><option value='' align='center' style='background-color: #FFCCCC'>Genome Information</option><option value='Gene_id'>Gene_id</option><option value='Gene_name'>Gene_name</option><option value='Gene_product'>Gene_product</option><option value='Function_of_proteins'>Function_of_proteins</option><option value='Protein_id'>Protein_id</option><option value='Localization_of_proteins'>Localization_of_proteins</option><option value='Strain_id'>Strain_id</option></select><select name='fieldsi_op[]'><option value='like'>LIKE</option><option value='not like'>Not LIKE</option><option value='equal'>Equal to</option><option value='not_equal'>Not equal to</option><option value='='> = </option><option value='!='> != </option><option value='<='> <= </option><option value='>='> >= </option><option value='<'> < </option><option value='>'> > </option></select> <input type='text' id='" + intTextBox + "' name='keyword[]'>";
contentID.appendChild(newTBDiv);
}
//FUNCTION TO REMOVE TEXT BOX ELEMENT
function removeElement()
{
if(intTextBox != 0)
{
var contentID = document.getElementById("content");
contentID.removeChild(document.getElementById("strText"+intTextBox));
intTextBox = intTextBox-1;
}
}
</script>
<!--<staticContent>
<remove fileExtension=".woff" />
<mimeMap fileExtension=".woff" mimeType="application/x-font-woff" />
<remove fileExtension=".woff2" />
<mimeMap fileExtension=".woff2" mimeType="application/font-woff2" />
</staticContent>
<p>
<button type = "button" class = "btn btn-default">
<span class = "glyphicons-halflings-regular.eot"></span>
</button>
<button type = "button" class = "btn btn-default">
<span class = "glyphicons-halflings-regular.ttf"></span>
</button>
<button type = "button" class = "btn btn-default">
<span class = "glyphicons-halflings-regular.woff"></span>
</button>
<button type = "button" class = "btn btn-default">
<span class = "glyphicons-halflings-regular.svg"></span>
</button>
<link href="tablesort/css/glyphicons-halflings-regular.eot" rel="stylesheet">
<link href="tablesort/css/glyphicons-halflings-regular.ttf" rel="stylesheet">
<link href="tablesort/css/glyphicons-halflings-regular.woff" rel="stylesheet">
<link href="tablesort/css/glyphicons-halflings-regular.svg" rel="stylesheet">-->
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css" rel="stylesheet">
<script src="tablesort/js/jquery-3.3.1.js" type="text/javascript"></script>
<script src="tablesort/js/jquery.dataTables.min.js" type="text/javascript"></script>
<script src="tablesort/js/dataTables.bootstrap.min.js" type="text/javascript"></script>
<link href="tablesort/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<link href="tablesort/css/dataTables.bootstrap.min.css" rel="stylesheet" type="text/css"/>
<script>
$(document).ready(function() {
$('#example').DataTable();
} );
</script>
</head>
<br>
<br>
<br>
<br>
<font color="#3d4983" face="Verdana" size="3"><b>Query Builder</b></font>
<br>
<p align='justify'>Users may build complex queries using the logical operators 'AND' and 'OR'. Each sub-query can be built using other operators such as LIKE, NOT LIKE, EQUAL and NOT EQUAL TO while dealing with strings like
words or letters and =, !=, <=, >=, < and > while dealing with numerical values. The Query builder aids to the flexibility of performing search on a number of fields simultaneously.</p>
<br>
<form name='form1' action="<?=$_SERVER['PHP_SELF']; ?>" method='post' onsubmit="return formValidation(this);">
<select name='fieldsi[]'>
<option value='all'>All Fields</option>
<option value='' style="background-color: #FFCCCC">Gene Information</option>
<option value='Gene_id'>Gene id</option>
<option value='Gene_name'>Locus tag</option>
<option value='Gene_product'> Gene product</option>
<option value='Function_of_proteins'> Protein function</option>
<option value='Protein_id'> Protein ID</option>
<option value='Localization_of_proteins'> Localization of protein</option>
<option value='Strain_id'> Strain ID</option>
</select>
<select name='fieldsi_op[]'>
<option value='like'>LIKE</option>
<option value='not like'>Not LIKE</option>
<option value='equal'>Equal to</option>
<option value='not_equal'>Not equal to</option>
<option value='='> = </option>
<option value='!='> != </option>
<option value='<='> <= </option>
<option value='>='> >= </option>
<option value='<'> < </option>
<option value='>'> > </option>
</select>
<input type='text' name='keyword[]' id='first_box'>
<input type="button" onclick="addElement()" value="+"> <input type="button" onclick="removeElement()" value="-">
<div id="content" ></div>
<br>
<input type='submit' value='Search'>
</form>
<br>
<br>
<body>
<?php
$dbServerName = "abc";
$dbUsername = "abc";
$dbPassword = "abc";
$dbName = "abc";
// create connection
$conn = new mysqli($dbServerName, $dbUsername, $dbPassword, $dbName);
// check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SHOW COLUMNS FROM $table";
if(isset($_POST['keyword'])){
$key = $_POST['keyword'];
$op = $_POST['op'];
$field = $_POST['fieldsi'];
$field_op = $_POST['fieldsi_op'];
for($i=0;$i<count($key);$i++){
$keyw = $key[$i];
$field_val = $field[$i];
$field_op_val = $field_op[$i];
if($i>0){
$j = $i-1;
$operate = $op[$j];
if($operate != 'not'){
if($field_val == 'all'){
$result = mysqli_query($conn,$sql);
$result1 = mysqli_query("SHOW COLUMNS FROM $table");
echo "$result1";
if (mysqli_num_rows($result) > 0)
{
$i=0;
while ($row = mysqli_fetch_array($result))
{
if($i==0)
{
$fi_name = $row[0];
$display["$fi_name"] = 0;
$qu_mul = $qu_mul.' '.$operate.' '."$fi_name $field_op_val '%$keyw%'";
}
else
{
$fi_name = $row[0];
$display["$fi_name"] = 0;
$qu_mul.=" or "."$fi_name $field_op_val '%$keyw%'";
}
$i++;
}
}
}
else{
$display["$field_val"] = 0;
if(!preg_match('/[like]/i', $field_op_val)){
$qu_mul = $qu_mul.' '.$operate.' '."$field_val $field_op_val $keyw";
}
else if(preg_match('/equal/i', $field_op_val)){
$qu_mul = $qu_mul.' '.$operate.' '."$field_val ='$keyw'";
}
else if(preg_match('/not_equal/i', $field_op_val)){
$qu_mul = $qu_mul.' '.$operate.' '."$field_val !='$keyw'";
}
else{
$qu_mul = $qu_mul.' '.$operate.' '."$field_val $field_op_val '%$keyw%'";
}
}
}
else{
$qu_mul = $qu_mul.' '."and $field_val not like '%$keyw%'";
}
}
else{
if($field_val == 'all'){
$result = mysqli_query("SHOW COLUMNS FROM $table");
if (mysqli_num_rows($result) > 0)
{
$i=0;
while ($row = mysqli_fetch_array($result))
{
if($i==0)
{
$fi_name = $row[0];
$display["$fi_name"] = 0;
if(!preg_match('/[like]/i', $field_op_val)){
$qu = "$fi_name $field_op_val $keyw";
}
else if(preg_match('/equal/i', $field_op_val)){
$qu = "$fi_name ='$keyw'";
}
else if(preg_match('/not_equal/i', $field_op_val)){
$qu = "$fi_name !='$keyw'";
}
else{
$qu = "$fi_name $field_op_val '%$keyw%'";
}
}
else
{
$fi_name = $row[0];
$display["$fi_name"] = 0;
if(!preg_match('/[like]/i', $field_op_val)){
$qu.=" or "."$fi_name $field_op_val $keyw";
}
else if(preg_match('/equal/i', $field_op_val)){
$qu.=" or "."$fi_name ='$keyw'";
}
else if(preg_match('/not_equal/i', $field_op_val)){
$qu.=" or "."$fi_name !='$keyw'";
}
else{
$qu.=" or "."$fi_name $field_op_val '%$keyw%'";
}
}
$i++;
}
}
}
else{
$display["$field_val"] = 0;
if(!preg_match('/[like]/i', $field_op_val)){
$qu = "$field_val $field_op_val $keyw";
}
else if(preg_match('/equal/i', $field_op_val)){
$qu = "$field_val ='$keyw'";
}
else if(preg_match('/not_equal/i', $field_op_val)){
$qu = "$field_val !='$keyw'";
}
else{
$qu = "$field_val $field_op_val '%$keyw%'";
}
}
}
}
$dis="";
$display["Gene_id"] = 0;
#echo "$display";
$display["Gene_name"] = 0;
$display["Gene_product"] = 0;
$display["Function_of_proteins"] = 0;
$display["Protein_id"] = 0;
#$display["Protein_sequence"] = 0;
#$display["Nucleotide_sequence"] = 0;
$display["Protein_length"] = 0;
$display["Localization_of_proteins"] = 0;
$display["Strain_id"] = 0;
$display["Source"] = 0;
foreach($display as $key => $value){
$dis = "$dis"."$key,";
#echo "$dis";
}
$dis = preg_replace("/,$/",'',$dis);
$qu_pre = "select $dis from $table where ";
$sql = "$qu_pre"."$qu"."$qu_mul";
$res = $conn->query("$sql") or die($conn->error);
#echo"<table border=1>";
$res_no = mysqli_num_rows($res);
if($res_no == 0){
"<font color='brown' size='4'><b>No Record Found in CRAB-DB coressponding to your keyword !!!</b></font><br><br><input type='button' value='Go Back' onClick='history.go(-1)'><br><br><br>";
}
else{
$tot_res = mysqli_num_rows($res);
echo "Total number of records = $tot_res";
echo '<br>';
#<!-- back working properly-->
echo "<input type='button' onClick='history.go(-1)' value='Back'><br><br>";
echo '<div style="padding:0px 0px 0px 0px;">';
$query = mysqli_query($conn, $sql);
#echo "<table cellspacing=\"1\" class=\"tablesorter\">";
#echo "<thead><tr>";
if (mysqli_num_rows($query)){
$headerRow = true;
while ($row = mysqli_fetch_assoc($query)){
if ($headerRow){
$headerRow = false;
echo '<table id="example" class="table table-striped table-bordered" style="width:100%">';
echo "<thead>";
echo "<tr>";
foreach (array_keys($row) as $header)
{
echo "<th align = left>$header</a></th>";
}
echo "</tr>";
echo "</thead>";
}
echo '<tbody>';
#echo "<tbody>";
echo "<tr>";
#echo "<td>";
foreach ($row as $value){
echo "<td>$value</td>";
}
echo "</td>";
echo "</tr>";
#echo '</tr>';
echo '</tbody>';
}
}
}
while($row=mysqli_fetch_array($res)){
for($i=0;$i<mysqli_num_fields($res);$i++){
$f_name = mysqli_field_name($res, $i);
$f_val = $row[$f_name];
if($f_name == 'Gene_name'){
print"<td>$f_val</td>";
print "<td><a href='#####.php?Gene_name=$f_val&type=Gene_name' title='Click to See Details' style='text-decoration: none;'>$f_val</a></td>";
}
else if($f_name == 'Gene_id'){
$c_id = $row['Gene_name'];
$res_pub = mysqli_query("select * from $table where Gene_name='$c_id'");
}
print "</th>";
}
}
}
echo "</table>";
echo "<tfoot>";
echo "<tr>";
foreach (array_keys($row) as $header)
{
echo "<th align = left>$header</a></th>";
}
echo "</tr>";
echo "</table>";
echo "</body>";
echo "</html>";
Your code uses relative links. Don't do that. Use Absolute links!
<script src="jquery-3.3.1.js" type="text/javascript"></script>
Depending which folder this script is in, it will look from there, and not the site root.
However, changing it to
<script src="/path/to/jquery-3.3.1.js" type="text/javascript"></script>
Will load it from the site root all the time! In the example I put /path/to/jquery.js, if your index.php is in the public folder, then i usually put js in a js folder, so the link would be /js/jquery-3.3.1.js
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'm struggling for this, please some one help. I'm not a html or php expert.
I've created a simple webpage that pulls the records from mysql database from last 7 days, 10 days etc by changing the select statement every time. Works fine till here.
But it is a pain to change the select statement every time, instead I want to create a simple drop down list.
I've added simple drop down with the list of the dates like 1(indicates one day of records), 7- 7 days of records etc. Once the user select 7, then it has to jump to that query that pulls 7 day records and then continue.
But what am I doing wrong here, I can't able to execute it.
Here is my code
<!DOCTYPE html>
<html>
<head>
<title>PHP</title>
</head>
<body>
<p>
<select name="value">
<option value="7">7</option>
<option value="1">1</option>
</select>
<?php
$dbh = mysqli_connect("","", "", "");
if (!$dbh) {
echo "Error: Unable to connect to mysqli." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
print ("<h1>Metrics</h1>");
print ("<table class=\"hovertable\">\n");
if($_POST['value'] == '7')
{
// query to get 7 records
$select="select * from table where FROM_UNIXTIME(date)>= (NOW() - INTERVAL 7 DAY)";
}
else
{
// query to get 1 records
$select="select * from table where FROM_UNIXTIME(date)>= (NOW() - INTERVAL 1 DAY)";
}
$result = $dbh->query($select);
print "Total records :". ' '. $result->num_rows ;
if ($result->num_rows > 0)
{
while ($row = $result->fetch_assoc())
{
// Capture each record
$data = array('row'=>$row);
print ("<tr>");
print ("<td style=\"white-space: nowrap\"> $ID</td>");
print ("<td style=\"white-space: nowrap\"> $department</td>");
print ("<td style=\"white-space: nowrap\"> $customername</td>");
print ("<td style=\"white-space: nowrap\"> $date</td>");
print ("</tr>\n");
}
}
else
{
echo "0 results";
}
exit;
?>
$dbh->close();
</p>
</body>
</html>
I see that you are checking for the user posted value here :
$_POST['value']
But I did not see where and how you are posting this value to the page. Maybe you need a form and a submit button to post the selected value:
<form action="youpage.php" method="post">
//your dropdown code here
<input type="submit" value="Submit">
</form>
You most post the value of the select by either a form or JavaScript.
Using a form would look like:
<form method="post">
<select name="value">
<option value="7">7</option>
<option value="1">1</option>
</select>
<input type="submit" value="go" />
</form>
Using JavaScript
<form method="post" >
<select name="value" onchange="this.form.submit()">
<option value="7">7</option>
<option value="1">1</option>
</select>
</form>
If you don't want the whole page to reload, you'd want to separate or copy the query and table into another PHP page, and submit the form using an XHR.
Updated to include a function to create the select options.
<?php
Function ddform_Interval($array) {
// generates the dropdown selects based on values in array
$result = "";
$post = 1; // default post value
IF (isset($_POST['value'])) {
// validate the post value. It must match a value in array
IF (in_array($_POST['value'], $array)) { $post = $_POST['value']; }
}
Foreach($array as $value) {
IF ($post == $value) { $result .= "<option selected value=\"".$value."\">".$value."</option>";
}ELSE{ $result .= "<option value=\"".$value."\">".$value."</option>";
}
}
return $result;
}
$allowed_postvals = array('1', '7'); // add more values to add select options
$result = "";
$dbh = mysqli_connect("","", "", "");
if (!$dbh) {
echo "Error: Unable to connect to mysqli." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
// set & validate post value
IF (isset($_POST['value'])) {
IF (in_array($_POST['value'], $allowed_postvals)) { $postvalue = $_POST['value']; }ELSE{ $postvalue = '1'; }
}ELSE{
$postvalue = 1;
}
$query = "SELECT * FROM table WHERE FROM_UNIXTIME(date)>= (NOW() - INTERVAL ".$postvalue." DAY)";
$sql = $dbh->query($query);
if ($sql->num_rows > 0) {
$result .= "Total records :". ' '. $sql->num_rows ;
$result .= "<table class=\"hovertable\">\n";
while ($row = $sql->fetch_assoc()) {
// Capture each record
$data = array('row'=>$row);
$result .= "<tr>";
$result .= "<td style=\"white-space: nowrap\"> ".$row['ID']."</td>";
$result .= "<td style=\"white-space: nowrap\"> ".$row['department']."</td>";
$result .= "<td style=\"white-space: nowrap\"> ".$row['customername']."</td>";
$result .= "<td style=\"white-space: nowrap\"> ".$row['date']."</td>";
$result .= "</tr>\n";
}
$result .= "</table>\n";
}else{
$result .= "0 results";
}
$dbh->close();
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP</title>
</head>
<body>
<p>
<form action="" name="form" method="post">
<select name="value"><?php echo(ddform_Interval($allowed_postvals)); ?></select>
<input type="submit" name="submit" value="Submit">
</form>
</p>
<h1>Metrics</h1>
<?php echo($result); ?>
</body>
</html>
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[]">
I am currently working with a dynamic dropdown menu(dependable select boxes). I am pulling the values straight from MySQL DB(if your curious here is how i have the DB SETUP). I am able to get the values of each table and display them accordingly. The problem I am having is echoing the SELECTED value of each selct box. I have a created JS function that will request postfile.php which will then echo the SELECTED value of each box. I am not getting anything echoed. I have checked with firebug but nothing is being posted.
How can I make this work? or Am I approaching this wrong? or Is there a better way? EXAMPLE
Working HTML/PHP
<?php
include ('includes/dbConnect.php');
try {
$pdo = get_database_connection();
$sql = "SELECT *
FROM `categories`
WHERE `master_id` = 0";
$statement = $pdo->query($sql);
$list = $statement->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo 'There was a problem';
}
?>
<select name="main" id="main" size="7" class="update">
<option value="">Select one</option>
<?php if (!empty($list)) { ?>
<?php foreach($list as $row) { ?>
<option value="<?php echo $row['id']; ?>">
<?php echo $row['name']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
<select name="subc1" id="subc1" size="7" class="update" disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
<select name="subc2" id="subc2" size="7" class="update" disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
<select name="subc3" id="subc3" size="7" class="update" disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
JS
<script type="text/javascript">
$(document).ready(function () {
$('#main).change(function() {
if ($(this).val()!='
') {
$("#subc1").load("postfile.php",{main_id: $(this).val()});
//$("#subc1").removeAttr('
disabled hidden ');
}
});
//code on change of sel_source
$('#subc1 ').change(function() {
if ($(this).val()!='
') {
$("#subc2").load("postfile.php",{subc1_id: $(this).val()});
//$("#colour").removeAttr('
disabled ');
}
});
$('#subc2 ').change(function() {
if ($(this).val()!='
') {
$("#subc3").load("postfile.php",{subc2_id: $(this).val()});
//$("#colour").removeAttr('
disabled ');
}
});
});
</script>
PHP- postfile.php
if(isset($_REQUEST['main_id']) && !empty($_REQUEST['main_id'])) {
try {
include ('../includes/dbConnect.php');
$pdo = get_database_connection();
$sql = ("select * from `categories` where id='".$_REQUEST['main_id']."' ");
$result = $con->prepare($sql);
$result->execute();
$number_of_rows = $result->fetchColumn();
}catch(PDOException $e) {
echo 'There was a problem';
}
if($number_of_rows > 0) {
$output = '<option value="">Select</option>';
while($row = mysql_fetch_assoc($result)) {
$output .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
} else {
$output = '<option value="">Select</option>';
}
echo $output;
}
if(isset($_REQUEST['subc1_id']) && !empty($_REQUEST['subc1_id'])) {
$result = mysql_query("select * from table where id='".$_REQUEST['subc1_id']."' ");
if($number_of_rows > 0) {
$output = '<option value="">Select</option>';
while($row = mysql_fetch_assoc($result)) {
$output .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
} else {
$output = '<option value="">Select</option>';
}
echo $output;
}
if(isset($_REQUEST['subc2_id']) && !empty($_REQUEST['subc2_id'])) {
$result = mysql_query("select * from table where id='".$_REQUEST['subc2_id']."' ");
if($number_of_rows > 0) {
$output = '<option value="">Select</option>';
while($row = mysql_fetch_assoc($result)) {
$output .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
} else {
$output = '<option value="">Select</option>';
}
echo $output;
}
Maybe you would like to use jQuery UI auto-complete. It is easier to use and less code. It has a remote data source also. Try this one, maybe this can solve your problem. http://jqueryui.com/demos/autocomplete/
Add on submit action to the form. In the function use the following:
var Myvar = $('#subc3 :selected').text();