Background :
when a user selects one of the option [ example : Undelivered ] in the drop down menu, then i am displaying only the rows which have that value [ example : Undelivered ]
<tr><th>
<select id="my_select" onchange="send_option();">
<option value="all">Status</option>
<?php
$query = "SELECT DISTINCT in_transit FROM do_order";
$result = mysqli_query ($mysqli, $query);
while ( $row = mysqli_fetch_array($result) )
echo "<option value='" . $row['in_transit'] . "'>" . $row['in_transit'] . "</option>";
?>
</select>
</th></tr>
<?php
$theBigQuery = "SELECT * FROM do_order WHERE 1";
if (isset($_POST['my_option']) && $_POST['my_option'] != "")
{
if($_POST['my_option'] == "all")
{
}
else
{
$theBigQuery .= " AND in_transit like '" . $_POST["my_option"] . "'";
}
echo "<script type='text/javascript'>function changeOption(){document.getElementById('my_select').value='".$_POST['my_option']."';} changeOption();</script>";
}
$orderrecords = $db_handle->runSelectQuery($theBigQuery);
?>
<tr><td id="<?php echo $orderrecords[$k]["tracking_id"];?>">
<?php echo $orderrecords[$k]["in_transit"]; ?>
</td></tr>
<form method="post" action"dashboard.php" style="display:none" id="my_form">
<input type="text" id="my_option" name="my_option"/>
</form>
script
function send_option ()
{
var sel = document.getElementById( "my_select" );
var txt = document.getElementById( "my_option" );
txt.value = sel.options[ sel.selectedIndex ].value;
var frm = document.getElementById( "my_form" );
frm.submit();
}
Requirement :
Now I want to display rows in decrement order , so in above code i changed below line . now rows are displaying in decrement order.
$theBigQuery = "SELECT * FROM do_order ORDER BY id DESC";
Issue :
but if i select any option in dropdown [ example : undelivered ] , its not displaying any rows.
Using you edit, when you select an option, your query will become :
$theBigQuery = "SELECT * FROM do_order ORDER BY id DESC AND in_transit like 'Undelivered'";
This is a wrong sql query!!
So to fix this; leave the $theBigQuery as it was, and after the if (isset( .. test, before running the query, add the ORDER BY clause :
$theBigQuery = "SELECT * FROM do_order WHERE 1";
if (isset($_POST['my_option']) && $_POST['my_option'] != "")
{
if($_POST['my_option'] == "all")
{
}
else
{
$theBigQuery .= " AND in_transit like '" . $_POST["my_option"] . "'";
}
echo "<script type='text/javascript'>function changeOption(){document.getElementById('my_select').value='".$_POST['my_option']."';} changeOption();</script>";
}
$theBigQuery .= " ORDER BY id DESC";
$orderrecords = $db_handle->runSelectQuery($theBigQuery);
Related
i have a country state city database that displaying in select boxes and everything is working fine. What i want when state select box selected to display city results in a div or another element. When select box defined for city results there is no problem but if i use div gives the error message
Notice: Undefined index: country in C:\wamp\www\list\post.php on line 3.
here is my codes, thanks.
post.php
include("connect.php");
$country = $_POST['country'];
$query = mysqli_query($connect, "select * from state where country_id='$country'");
while( $list = mysqli_fetch_array( $query ) ) {
echo '<option value=' . $list["id"]. '>' . $list["state_name"] . '</option>';
}
$state = $_POST['state'];
$query = mysqli_query($connect, "select * from city where state_id='$state'");
while( $list = mysqli_fetch_array( $query ) ) {
echo $list["city_name"];
}
jquery codes
$( "#countries" ).change( function(){
$("#states").empty();
var val = $(this).val();
$.post("post.php", {country:val}, function(a) {
$("#states").append(a);
});
});
$( "#states" ).change( function(){
$("#cities").empty();
var val = $(this).val();
$.post("post.php", {state:val}, function(a) {
$("#cities").append(a);
});
});
index.php
<?php include("connect.php"); ?>
<select id="countries">
<option>select</option>
<?php
$query = mysqli_query($connect, 'select * from country');
while( $list = mysqli_fetch_array( $query ) ) {
echo '<option value=' . $list["id"]. '>' . $list["country_name"] . '</option>';
}
?>
</select>
<select id="states">
<option>select</option>
</select>
<div id="cities"></div>
The script needs to cheeck whether it was called with the country or state parameter, and perform the appropriate query. And the code for returning the cities needs to put them in <option>, just like returning states does.
include("connect.php");
if (isset($_POST['country'])) {
$country = mysqli_real_escape_string($connect, $_POST['country']);
$query = mysqli_query($connect, "select * from state where country_id='$country'");
while( $list = mysqli_fetch_array( $query ) ) {
echo '<option value=' . $list["id"]. '>' . $list["state_name"] . '</option>';
}
elseif (isset($_POST['state'])) {
$state = mysqli_real_escape_string($connect, $_POST['state']);
$query = mysqli_query($connect, "select * from city where state_id='$state'");
while( $list = mysqli_fetch_array( $query ) ) {
'<option value=' . $list["id"]. '>' . $list["city_name"] . '</option>';
}
}
You should also learn to use parametrized queries instead of substituting variables, to prevent SQL injection. Until then, you should at least escape the parameters.
I have a project in php. When I bind select dropdown list with selected value as well as all others values inside the database the one value which is as selected will bind two times, one as first selected value and other as list values.
Below is my code.
$query1 = mysql_query("select * from pincode_master where pcode_id='1'");
while ($row1 = mysql_fetch_array($query1))
{
$city_id = $row1['city_id'];
$sql1="SELECT city_name from city_master where city_id='$city_id'";
$result1=mysql_query($sql1);
$row = mysql_fetch_row($result1);
#$city_name = $row[0];
$query11 = "select * from city_master";
$result11 = mysql_query($query11);
echo "<select name = 'cityname'>";
while (($row11 = mysql_fetch_row($result11)) != null)
{
echo "<option value = '{$row11['city_name']}'";
if ($city_name == $city_name)
echo "selected = 'selected'";
echo ">{$row11['city_name']}</option>";
}
echo "</select>";
}
You should write your query outside loop.
Write your code as below:-
// Check query error
$query1 = mysql_query( "select * from pincode_master where pcode_id='1'" ) or die( mysql_error());
// Check query error
$result11 = mysql_query( "select * from city_master" ) or die( mysql_error());
while ( $row1 = mysql_fetch_assoc( $query1 ) ) {
$city_id = $row1['city_id'];
echo "<select name = 'cityname'>";
while ($row11 = mysql_fetch_assoc( $result11 )) {
$selected = $row11['city_id'] == $city_id ? "selected = 'selected'" : '';
echo "<option value = '{$row11['city_name']}' $selected >". $row11['city_name'] ."</option>";
}
echo "</select>";
}
Hope it will help you :)
Please check this -
is that what you want ?
<select name="yourselection">
<option value="">--Select--</option>
<?php
$msql = mysql_query("SELECT * FROM tablename");
while($m_row = mysql_fetch_array($msql))
echo("<option value = '" . $m_row['table_column1'] . "'>" . $m_row['table_column2'] . "</option>");
?>
</select>
you will get more info here
I have two tables one for user and one for search. When I drag a search over to user it adds entire search list. How do I only insert the item dragged and not the entire search array?
THE PROBLEM IS recordsArray.
<?php
require("db.php");
$action = mysql_real_escape_string($_POST['action']);
$updateRecordsArray = $_POST['recordsArray'];
if ($action == "updateRecordsListings"){
$listingCounter = 1;
foreach ($updateRecordsArray as $recordIDValue) {
$query = "UPDATE records1 SET recordListingID = " . $listingCounter . " WHERE recordID = " . $recordIDValue;
mysql_query($query) or die('Error, update query failed');
//INSERTS array item that does NOT EXIT in userTable
if (mysql_affected_rows()==0) {
$query = mysql_query("INSERT INTO records1 SELECT * FROM records WHERE recordID = " . $recordIDValue);
$query = "UPDATE records1 SET recordListingID = " . $listingCounter . " WHERE recordID = " . $recordIDValue;
mysql_query($query) or die('Error, insert/update query failed');
}
$listingCounter = $listingCounter + 1;
}
echo '<pre>';
print_r($updateRecordsArray);
echo '</pre>';
echo 'If you refresh the page, you will see that records will stay just as you modified.';
}
?>
Both list arrays id must maintain the same id names in order for the above item to acknowledge and process the list items. Keeping things simple I just used the same code for the Search, User format:
<li id="recordsArray_<?php echo $result['recordID']; ?>"><?php echo $result['recordID'] . ". " . $result['recordText']; ?></li>
Javascript:
$(function() {
$("#contentLeft ul, #main ul").sortable({ accept: '.draggable', connectWith: "#contentLeft ul", opacity: 0.6, cursor: 'move', update: function() {
var order = $(this).sortable("serialize") + '&action=updateRecordsListings';
$.post("updateDB.php", order, function(theResponse){
$("#contentRight").html(theResponse);
});
}
});
}).disableSelection();
});
How can I call only the recordsArray items from #content ul?
I can only get one insert per refresh with this but it does drag and drop insert...
Still need an if statement for possible errors because +/-1 if
<?php
require("db.php");
$action = mysql_real_escape_string($_POST['action']);
$updateRecordsArray = $_POST['recordsArray'];
$uRA = $updateRecordsArray;
// Get total number of elements in $updateRecordsArray
$ifArray = $updateRecordsArray;
$resultCount = count($ifArray);
//echo '<br />';
//print_r($resultCount);
// Get total number of rows in $records1
$recordTable = array();
$result = mysql_query("SELECT recordListingID FROM records1");
$numRows = mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)) {
$recordTable = array_merge($recordTable, array_map('trim', explode(",", $row['recordListingID'])));
}
//Gets $id/$val of elements not in both arrays
$results = array_diff($uRA, $recordTable);
//echo '<br />';
//print_r($numRows);
//Give variables for +/- 1 $numRows
$plusNumRows = $numRows + 1;
$minusNumRows = $numRows - 1;
//echo '<br />';
//print_r($minusNumRows);
// Normal jQuery drag and drop ranking found online
if ($action == "updateRecordsListings"){
$listingCounter = 1;
foreach ($updateRecordsArray as $recordIDValue) {
//If statement for for +/- 1 $numRows
If ($resultCount == $numRows -1 || $resultCount == $numRows || $resultCount == $numRows + 1){
$sql = mysql_query("INSERT INTO records1 SELECT * FROM records WHERE recordID = $recordIDValue");
}
$query = "UPDATE records1 SET recordListingID = " . $listingCounter . " WHERE recordID = " . $recordIDValue;
mysql_query($query) or die('Error, update query failed');
$listingCounter = $listingCounter + 1;
}
echo '<pre>';
print_r($updateRecordsArray);
echo '</pre>';
echo 'If you refresh the page, you will see that records will stay just as you modified.';
}
?>
I have an attendance page which outputs a list of students in a class through the following loop:
$sql10 = "SELECT class.name, student_to_class.class_id, student_to_class.student_id
FROM
student_to_class
INNER JOIN
class
ON class.id=student_to_class.class_id
WHERE
class.name = '$classid'";
$result10 = mysql_query($sql10) or die(mysql_error());
while ($row = mysql_fetch_array($result10)) {
$student = $row['student_id'];
$classid = $row['class_id'];
$sql3 = "select * from student where id = '$student'";
$result3 = mysql_query($sql3) or die(mysql_error());
$row3 = mysql_fetch_assoc($result3);
$studentfname = $row3['first_name'];
$studentlname = $row3['last_name'];
$sql4 = "select * from student where first_name = '$studentfname' AND last_name = '$studentlname'";
$result4 = mysql_query($sql4) or die(mysql_error());
$row4 = mysql_fetch_assoc($result4);
$studentrfid = $row4['rfid'];
$sql5 = "select * from class where id = '$classid'";
$result5 = mysql_query($sql5) or die(mysql_error());
$row5 = mysql_fetch_assoc($result5);
$class_name = $row5['name'];
//Define the default variables assuming attendance hasn't been taken.
$david = "select * from student where rfid='$studentrfid'";
$davidresult = mysql_query($david) or die(mysql_error());
$drow = mysql_fetch_assoc($davidresult);
if (($drow['excused'] == '1') && ($drow['excuseddate'] == $date)) {
//if($drow['excuseddate'] == $date;
$excusedabsense = '<option value="Excused Absense" label="Excused Absense" selected="selected">Excused Absense</option>';
} else {
$excusedabsense = '';
}
$presentpunctual = '<option value="Present" label="Present">Present</option>';
$presenttardy = '<option value="Tardy" label="Tardy">Tardy</option>';
$unexcusedabsense = '<option value="Absent" label="Absent">Absent</option>';
if (isset($_POST['editdate'])) {
$date = $_POST['date'];
}
$realfname = $studentfname;
$reallname = $studentlname;
$sql4 = "select * from attendance_main where StudentID = '$studentrfid' AND date = '$date' AND classID = '$class_name'";
$result4 = mysql_query($sql4) or die(mysql_error());
$row4 = mysql_fetch_assoc($result4);
if ($row4['status'] == "Present") {
$presentpunctual = '<option value="Present" label="Present" selected="selected">Present</option>';
} else {
$presentpunctual = '<option value="Present" label="Present">Present</option>';
}
if ($row4['status'] == "Tardy") {
$presenttardy = '<option value="Tardy" label="Tardy" selected="selected">Tardy</option>';
} else {
$presenttardy = '<option value="Tardy" label="Tardy">Tardy</option>';
}
if ($row4['status'] == "Absent") {
$unexcusedabsense = '<option value="Absent" label="Absent" selected="selected">Absent</option>';
} else {
$unexcusedabsense = '<option value="Absent" label="Absent">Absent</option>';
}
$b++;
echo "<tr>";
if (!isset($dateform)) {
$dateform = date('m/d/Y');
}
$date = date('m/d/Y');
echo '<td><iframe src="flag.php?&flagdate=' . $dateform . '&curdate=' . $date . '&class=' . $classid . '&flag=1&user=' . $studentrfid . '&curflag=' . $realrfid['flag'] . '&flagclass=' . $classname . '" width="50" height="30" frameborder="0" scrolling="no"> </iframe></td>';
//Yesterday
$sql8 = "select * from attendance_main where StudentID = '$studentrfid' AND date='$yesterdaysql' AND classID = '$class_name'";
$result8 = mysql_query($sql8) or die(mysql_error());
$tooltiprow = mysql_fetch_assoc($result8);
if (mysql_num_rows($result8) == 0) {
$tooltipresult_yesterday = "N/A";
} else {
$tooltipresult_yesterday = $tooltiprow['status'];
}
//2 days
$sql8 = "select * from attendance_main where StudentID = '$studentrfid' AND date='$days2sql' AND classID = '$classid'";
$result8 = mysql_query($sql8) or die(mysql_error());
$tooltiprow = mysql_fetch_assoc($result8);
if (mysql_num_rows($result8) == 0) {
$tooltipresult_2days = "N/A";
} else {
$tooltipresult_2days = $tooltiprow['status'];
}
//3 days
$sql8 = "select * from attendance_main where StudentID = '$studentrfid' AND date='$days3sql' AND classID = '$class_name'";
$result8 = mysql_query($sql8) or die(mysql_error());
$tooltiprow = mysql_fetch_assoc($result8);
if (mysql_num_rows($result8) == 0) {
$tooltipresult_3days = "N/A";
} else {
$tooltipresult_3days = $tooltiprow['status'];
}
$tooltip = "<b>" . $yesterday . ":</b> " . $tooltipresult_yesterday . " - <b>" . $days2 . ":</b> " . $tooltipresult_2days . " - <b>" . $days3 . ":</b> " . $tooltipresult_3days;
echo "
<!-- Loop #" . $b . " --> <td><a href='#'";
?> onMouseover="ddrivetip('<?php
echo $tooltip;
?>')"; onMouseout="hideddrivetip()"> <?php
echo $realfname . " " . $reallname . "</a></td>";
echo '<td>
<select name="status' . $b . '">
' . $presentpunctual . '
' . $presenttardy . '
' . $excusedabsense . '
' . $unexcusedabsense . '
</select>
' . $hiddenfield . '
<input type="hidden" name="i" value="' . $b . '" />
<input type="hidden" name="studentid' . $b . '" value="' . $studentrfid . '">
<input type="hidden" name="classid" value="' . $class_name . '"></td>
<td><input type="text" name="comments' . $b . '" size="40" /></td></tr>
<!-- End Loop -->';
}
}
}
It essentially prints out student name and a drop down of statuses (if attendance was taken that day, the status will be whatever is set in the database). The date, flag, and tooltip functions are extra additions. (Date is for previous days, tooltip shows previous attendance on hover)
This data is being executed through the following loop:
if (isset($_GET['update'])) {
mysql_query("UPDATE teacher_accounts SET attendance = '1' WHERE username = '$username'") or die(mysql_error());
$error = 0;
$limit = $_GET['i'];
$starter = 0;
$num = 0;
while ($starter < $limit) {
$num++;
$statusinc = "status" . $num;
$studentinc = "studentid" . $num;
$commentsinc = "comments" . $num;
$starter++;
$studentID = $_GET[$studentinc];
$status = $_GET[$statusinc];
$comments = $_GET[$commentsinc];
$date = date("m/d/Y");
$sql = "select * from student where id = '$studentID'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$classid = $_GET['classid'];
if (isset($_GET['dateedit'])) {
$date = $_GET['dateedit'];
$count = "select * from attendance_main where StudentID = '$studentID' AND date = '$date' AND classID='$classid'";
$cresult = mysql_query($count) or die(mysql_error());
if (mysql_num_rows($cresult) > 0) {
$sql = "UPDATE attendance_main SET status='$status',comments='$comments',date='$date',classID='$classid' where StudentID = '$studentID'";
} else {
$sql = "INSERT INTO attendance_main (StudentID,status,comments,date,classID) VALUES ('$studentID','$status','$comments','$date','$classid')";
}
if (mysql_query($sql)) {
$return = "<h3>Successfully updated the attendance.</h3>";
}
} else {
$count = "select * from attendance_main where StudentID = '$studentID' AND date = '$date' AND classID='$classid'";
$cresult = mysql_query($count) or die(mysql_error());
if (mysql_num_rows($cresult) > 0) {
$sql = "UPDATE attendance_main SET status='$status',comments='$comments',date='$date',classID='$classid' where StudentID = '$studentID'";
if (mysql_query($sql)) {
$return = "<h3>Successfully updated the attendance for " . $num . " students.</h3>";
}
} else {
$sql = "INSERT INTO attendance_main (StudentID,status,comments,date,classID) VALUES ('$studentID','$status','$comments','$date','$classid')";
if (mysql_query($sql)) {
$return = "<h3>Successfully inserted today's attendance for " . $num . " students.";
}
}
}
}
echo $return;
For some reason, data is sometimes not being inserted properly. For example, a teacher might submit attendance on 02/08/2011, for a specific class, and certain students might appear twice under that attendance. This shouldn't be the case according to the code, because it should first check if they exist and, if they do, update the record rather than insert.
I've also seen cases where records are randomly deleted altogether. When a teacher takes attendance, all statuses are automatically set to Present. However, when I searched records on a certain date in the database, 2 students were missing records (which isn't even possible unless its being deleted)
Anyone have any idea why this might happen? I've tried replicating it myself (by repeatedly submitting the form, refreshing the page after it's processed, etc, to no avail.)
Thank you for the help!
Your query that check if a record exists is looking for all 3. 1) $studentID, 2) $classid and 3) $classid However the UPDATE statement is just looking for $studentID.
I would suggest you create a PRIMARY KEY (or UNIQUE INDEX) on StudentID,date,classID, then use the MySql INSERT ON DUPLICATE KEY UPDATE...
INSERT INTO attendance_main (StudentID,status,comments,date,classID)
VALUES ('$studentID','$status','$comments','$date','$classid')
ON DUPLICATE KEY UPDATE
status = VALUES(status),
comments = VALUES(comments)
Don't forget to sanitize the database input by using mysql_real_escape_string for example $status = mysql_real_escape_string($_GET[$statusinc]);.
<input type="checkbox" name="average" value="average" <? if (get_option('average') == 'average'){ echo 'checked="checked"';} ?>>Average
<input type="checkbox" name="petite" value="petite" <? if (get_option('petite') == 'petite'){ echo 'checked="checked"';} ?>>Petite
if ( get_option('average') == 'average' ): // choose category
$average = "AND build = '".get_option('average')."'";
endif;
if ( get_option('petite') == 'petite' ): // choose category
$petite = "OR build = '".get_option('petite')."'";
endif;
$qry = mysql_query("SELECT performerid,pic0 FROM ".$table." WHERE **$average $petite** ORDER BY RAND() LIMIT 20");
How can i make this code work? I need the sql query to work if $average is selected, is both $average and $petite are selected, or just $petite is selected?
TY!
you may use something like this:
$selectedCategories = array();
foreach(array('petite', 'average', 'athletic') as $category)
{
if (get_option($category) == $category)
{
$selectedCategories[] = $category;
}
}
$qry = mysql_query(
"SELECT performerid,pic0
FROM ".$table."
WHERE build IN('" . implode("', '", $selectedCategories) . "')
ORDER BY RAND() LIMIT 20;");
Note: this won't work if you select no category.
you need to use php function isset();
if(isset($_POST)){ //checks if post is set
if(isset($)POST['average']) && !isset($_POST['petite'])){
//do staff only average is set
}
elseif(isset($_POST['petite']) && !isset($_POST['average'])){
//do staff only petite is set
}
elseif(isset($_POST['average'] && isset($_POST['average']))){
//do staff both is set
}
}