selection of query using multiple selection - php

I am beginner to php.I want to select the department from the options and after the selection of the department, I want to display the roll no in the next drop down box belong to that department. Help me by providing some ideas related to my questions.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Untitled Document</title>
</head>
<body>
<?php include('C:\wamp\www\fms\background.php'); ?>
<?php include('C:\wamp\www\fms\adminmenu.php'); ?>
<?php
if (isset($_POST['delete'])) {
$con = mysql_connect("localhost", "root", "");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
$cid = $_POST['cid'];
$sql = "DELETE from addpassenger WHERE rno='$rno'";
mysql_select_db('fms');
$retval = mysql_query($sql, $con);
if (!$retval) {
die('Could not delete data: ' . mysql_error());
}
echo "Deleted data successfully\n";
mysql_close($con);
} else {
?>
<p></p>
<p></p>
<p></p>
<p></p>
<center>
<form method="post">
<table width="344" border="0" cellspacing="1" cellpadding="2">
<tr>
<td>SELECT THE DEPARTMENT</td>
<td><?php
$con = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = #mysql_select_db("fms", $con) or die(mysql_error());
$str = "select dept from addpassenger";
$res1 = #mysql_query($str);
echo '<select name="dept">';
echo '<option selected="----------"></option>';
while ($row = mysql_fetch_array($res1)) {
echo '<option value="' . $row['dept'] . '">' . $row['dept'] . '</option>';
}
echo '</select>';
?></td>
</tr>
<tr>
<td width="208">SELECT THE ROLL NO</td>
<td width="125">
<?php
$con = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = #mysql_select_db("fms", $con) or die(mysql_error());
$str = "select rno from addpassenger ";
$res1 = #mysql_query($str);
echo '<select name="rno">';
echo '<option selected="----------"></option>';
while ($row = mysql_fetch_array($res1)) {
echo '<option value="' . $row['rno'] . '">' . $row['rno'] . '</option>';
}
echo '</select>';
?>
</select>
</td>
</tr>
<tr>
<td width="208"></td>
<td></td>
</tr>
<tr>
<td width="208"></td>
<td>
<input name="delete" type="submit" id="delete" value="Delete">
</td>
</tr>
</table>
</form>
</center>
<?php
}
?>
</body>
</html>

select rno from addpassenger
needs to have a where clause which selects based on previous selection

Related

Updating Data in Mysql through php on a current page

Hey All i have an issue while updating a data in MYSQL from front end php when i click once it update data when i click again without any change it again update and bring previous data. I am beginner to php and do not know what is the problem as code have no error and no warning please help and guide thanks and appreciation for your time in Advance
My code is
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Update in PHP</title>
</head>
<body>
<?php
$servername="localhost";
$username="root";
$password="";
$conn=mysql_connect($servername,$username,$password);
if(!$conn ) {
die('Could not connect: ' . mysql_error());
}
$sq1 = 'select * from biodata';
mysql_select_db('firstdb');
$display=mysql_query($sq1,$conn);
if(!$display ) {
die('Could not get data: ' . mysql_error());
exit;
}
if (mysql_num_rows($display) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>ID</th>
<th>Fname</th>
<th>Lname</th>
<th>Email</th>
<th>Phone</th>
<th>Message</th>
<th>Update</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $display ) ){
echo
"<form method= 'post' />
<tr>
<td><input name='UID' value='{$row['ID']}' /></td>
<td><input name='upfname' value='{$row['fname']}' /></td>
<td><input name='uplname' value='{$row['lname']}' /></td>
<td><input name='upemail' value='{$row['email']}' /></td>
<td><input name='upphone' value='{$row['phone']}' /></td>
<td><input name='upmessage' value='{$row['message']}' /></td>
<td><input type='Submit' name='update' value='Update' id='".$row["ID"]."' </td>
</tr>
</form>";
}
?>
</tbody>
</table>
<?php
if(isset($_REQUEST['update']))
{
$id = $_REQUEST['UID'];
$upfn = $_REQUEST['upfname'];
$upln = $_REQUEST['uplname'];
$upem = $_REQUEST['upemail'];
$upph = $_REQUEST['upphone'];
$upms = $_REQUEST['upmessage'];
$up="UPDATE biodata
SET
fname='$upfn',
lname='$upln',
email='$upem',
phone='$upph',
message='$upms'
WHERE ID = $id";
$updbb=mysql_query($up,$conn);
}
?>
</body>
</html>
redirect the same page
header('Location: url'); like header('Location: inbox.php?u='.$log_username.'');

export mysql query to csv on button click

so i have a php code that shows a table from the mysql database
there is also a filter field which filters the table.
i need help exporting the results of the table to a csv file when the button "export data" is clicked.
how do i do that?
below is the code
<?php
error_reporting(0);
include("config.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TAD Customer Search</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<style>
BODY, TD {
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
}
</style>
</head>
<body>
<form id="form1" name="form1" method="post" action="index.php">
<label>Search: </label>
<input type="text" name="string" id="string" size="20" value="<?php echo $_REQUEST["string"]; ?>" />
<label>Country: </label>
<select name="country">
<option value="">--</option>
<?php
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY country ORDER BY country";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$row["country"]."'".($row["country"]==$_REQUEST["country"] ? " selected" : "").">".$row["country"]."</option>";
}
?>
</select>
<input type="submit" name="button" id="button" value="Filter" />
</label>
Reset
</form>
<br />
<table style="text-align: left; width: 100%;" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="text-align: left;"><input type="submit" name="btnexportdata" value="Export Data" /></td>
<td style="text-align: right;">
<form method="post" action="input.php">
<input type="submit" value="Add" />
</form></td>
</tr>
</tbody>
</table>
<br />
<table width="100%" border="1" cellspacing="0" cellpadding="4">
<tr>
<td width="150" bgcolor="#CCCCCC"><strong>Account Number</strong></td>
<td width="150" bgcolor="#CCCCCC"><strong>Company Name</strong></td>
<td width="150" bgcolor="#CCCCCC"><strong>Country</strong></td>
<td width="150" bgcolor="#CCCCCC"><strong>Email</strong></td>
<td width="150" bgcolor="#CCCCCC"><strong>Phone Number</strong></td>
<td width="150" bgcolor="#CCCCCC"><strong>Brands</strong></td>
</tr>
<?php
if ($_REQUEST["string"]<>'') {
$search_string = " AND (cname LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR email LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR accnumber LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR pnumber LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR brands LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%')";
}
if ($_REQUEST["country"]<>'') {
$search_country = " AND country='".mysql_real_escape_string($_REQUEST["country"])."'";
}
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id>0".$search_string.$search_country;
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {
?>
<tr>
<td><?php echo $row["accnumber"]; ?></td>
<td><?php echo $row["cname"]; ?></td>
<td><?php echo $row["country"]; ?></td>
<td><?php echo $row["email"]; ?></td>
<td><?php echo $row["pnumber"]; ?></td>
<td><?php echo $row["brands"]; ?></td>
</tr>
<?php
}
} else {
?>
<tr><td colspan="6">No results found.</td>
<?php
}
?>
</table>
</body>
</html>
here is the code you can try
// Define database connection variable dynamically
$DB_Server = "localhost"; //MySQL Server
$DB_Username = "root"; //MySQL Username
$DB_Password = ""; //MySQL Password
$DB_DBName = "test1"; //MySQL Database Name
$DB_TBLName = "tabletest"; //MySQL Table Name
$filename = "excelfilename"; //File Name
//create MySQL connection
$sql = "Select * from csvtable";
$Connect = #mysqli_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect to MySQL:<br>" . mysqli_error() . "<br>" . mysql_errno());
//select database
$Db = #mysqli_select_db( $Connect,$DB_DBName) or die("Couldn't select database:<br>" . mysqli_error() . "<br>" . mysql_errno());
//execute query
$result = #mysqli_query( $Connect,$sql) or die("Couldn't execute query:<br>" . mysqli_error() . "<br>" . mysql_errno());
function cleanData(&$str)
{
if ($str == 't')
$str = 'TRUE';
if ($str == 'f')
$str = 'FALSE';
if (preg_match("/^0/", $str) || preg_match("/^\+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str)) {
$str = "'$str";
}
if (strstr($str, '"'))
$str = '"' . str_replace('"', '""', $str) . '"';
}
// filename for download
$filename = "file_" . date('Ymd') . ".csv";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: text/csv;");
$out = fopen("php://output", 'w');
$flag = false;
while ($row = mysqli_fetch_assoc($result))
{
if (!$flag)
{
// display field/column names as first row
fputcsv($out, array_keys($row), ',', '"'); $flag = true;
}
array_walk($row, 'cleanData');
// insert data into database from here
fputcsv($out, array_values($row), ',', '"');
}
fclose($out);
exit;
//end

Search with pagination [duplicate]

This question already exists:
How can i edit the search and pagination because i don't want it to be echoed?
Closed 7 years ago.
I have this search with pagination, the search function is working fine and the pagination is also working fine but my real problem is that I don't know how to merge those two. Every time I try to search, the search result is showing but the pagination is not functioning normal. Please someone help me about this issue. I don't know where to start.
I have this code:
<?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("region_survey", $con);
$sql="SELECT * FROM municipality";
if(isset($_POST['search'])){
$search_term=mysql_real_escape_string($_POST['search_box']);
$sql .= " WHERE province_id LIKE '%{$search_term}%' ";
}
$query=mysql_query($sql) or die (mysql_error());
?>
<form name="search_form" method="POST" action="">
Search:<input type="text" name="search_box" value="" />
<input type="submit" name="search" value="search the table" />
</form>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
//Include the PS_Pagination class
include('ps.php');
//Connect to mysql db
$conn = mysql_connect('localhost','root','');
if(!$conn) die("Failed to connect to database!");
$status = mysql_select_db('region_survey', $conn);
if(!$status) die("Failed to select database!");
$sql = 'SELECT * FROM municipality';
//Create a PS_Pagination object
$pager = new PS_Pagination($conn, $sql, 15, 17);
//The paginate() function returns a mysql result set for the current page
$rs = $pager->paginate();
?>
<?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
//Count total number of records after search
mysql_select_db("region_survey", $con);
if(isset($_POST['search'])){
$search_term=mysql_real_escape_string($_POST['search_box']);
$sql="SELECT * FROM municipality WHERE province_id LIKE '%$search_term%'";
}
$result = mysql_query($sql, $con);
$row = mysql_num_rows($result);
echo "Total Number: ";
echo $row;
?>
<table border="1" cellpadding="0" cellspacing="0" id="resultTable">
<tr>
<th> <strong>ID</strong> </th>
<th> <strong>Province ID</strong> </th>
<th> <strong>Municipality Name</strong> </th>
</tr>
<?php
while($row = mysql_fetch_array($query))
{
?>
<tr>
<td> <?php echo $row["id"]; ?> </td>
<td> <?php echo $row["province_id"]; ?></td>
<td> <?php echo $row["municipality_name"]; ?></td>
<td><input name="selector[]" type="checkbox"
id="checkbox[]" value="<?php echo $row['id'];?>"></td>
<td>Edit </td>
</tr>
<?php
}
?>
</table>
<?php
//Display the navigation
//echo $pager->renderFullNav();
echo '<div style="text-align:center">'.$pager->renderFullNav().'</div>';
?>
</body>
</html>
Pages will have URL like example.com/search/term/?page=2 (or example.com/search.php?term=xyz&page=2, doesn't matter if you redirect URLs or not).
In PHP then will be something like:
$pagelimit = 10; // records per page
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1; // set current page
And LIMIT with OFFSET in your SQL query:
$sql = "SELECT *
FROM municipality
WHERE province_id LIKE '%$search_term%'
LIMIT " . ($page - 1) * $pagelimit . ", " . $pagelimit;

Displaying results from a dropdown list

I am stuck on creating a dropdown list that is connected to another php page. I have used a sql query to list the staffNames but i need them to have the value of staffID. I have connected the page task7.php (which has a query that displays purchase information of a given staffID), so once the user clicks on a name then clicks submit, that persons order information should be displayed.Currently I am able to view the drop down list, select a name, but when i click submit the table only has field names with an empty table. HERES MY CODE:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Prac 2 Task 9</title>
</head>
<body>
<?php
$conn = mysql_connect("localhost", "twa291", ".......");
mysql_select_db("factory291", $conn)
or die ('Database not found ' . mysql_error() );
?>
<form method="get" action="task7.php">
<select name="list" id="list" size="12">
<?php
$sql = "SELECT staffID, staffName FROM staff";
$result = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());
while ($row = mysql_fetch_array($result)){
$title=$row["staffName"];
$id=$row["staffID"];
echo "<option value= ".$id.">".$title."</option>";
}
?>
<input type="submit" value="Submit" method="get">
</select>
</form>
<?php
mysql_close($conn); ?>
</body>
</html>
HERE IS MY task7.php FILE:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Prac 2 Task 3</title>
</head>
<body>
<?php
$conn = mysql_connect("localhost", "twa291", "......");
mysql_select_db("factory291", $conn)
or die ('Database not found ' . mysql_error() ); ?>
<?php
$staffid= $_GET["staffID"];
?>
<?php
$sql = "SELECT orderID, orderDate, orderDate, shippingDate, staffName FROM purchase,
staff
WHERE staff.staffID='$staffid'";
$rs = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());
?>
<table border="1" summary="Staff Orders">
<tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Shipping Date</th>
<th>Staff Name</th>
</tr>
<?php
while ($row = mysql_fetch_array($rs)) { ?>
<tr>
<td><?php echo $row["orderID"]?></td>
<td><?php echo $row["orderDate"]?></td>
<td><?php echo $row["shippingDate"]?></td>
<td><?php echo $row["staffName"]?></td>
</tr>
<?php }
mysql_close($conn); ?>
</table>
</body>
</html>
You have wrapped unwanted string with " "
<?php
$sql = "SELECT staffID, staffName FROM staff";
$result = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());
while ($row = mysql_fetch_array($result))
{
$title=$row["staffName"];
$id=$row["staffID"];
echo "<option value= '.$id.'>".$title."</option>";
}
?>
?php
$staffid= $_GET["list"];
?>
<?php
$sql = "SELECT orderID, orderDate, orderDate, shippingDate, staffName FROM purchase,
staff
WHERE staff.staffID='$staffid'";
?>

Form is failing to update mysql table

So I'm currently working with a form where the admin can select multiple users from the database via a tickbox system, then change the welcome message or general message to a client when they log in:
<?php
session_start();
include_once("isadmin.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Update Client Message</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
echo '<ul class="err">';
foreach($_SESSION['ERRMSG_ARR'] as $msg) {
echo '<li>',$msg,'</li>';
}
echo '</ul>';
unset($_SESSION['ERRMSG_ARR']);
}
?>
<form id="updateform" name="updateform" method="post" action="updateexec.php">
<table width="500" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<th width="200">Select User</th>
<td>
<?php
require_once('config.php');
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
$useruploadids = mysql_query("SELECT member_id, firstname, lastname FROM members");
while ($row = mysql_fetch_assoc($useruploadids)) {
$userid = $row['member_id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
?>
<input type="checkbox" name="userid_<?php echo $userid ?>" value="y" /><?php echo $firstname ?><?php echo $lastname ?><br />
<?php } ?>
</td>
</tr>
<tr>
<th>Message For Client </th>
<td>
<textarea input name="otherdeets" type="textarea" class="textfield" id="otherdeets" style="width: 356px; height: 176px">
</textarea>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Update" /></td>
</tr>
</table>
</form>
</body>
</html>
So this is the form, and it works fine, it calls all users from the database and displays them in tickbox fasion.
I can only assume my issue is in the exec script:
<?php
echo( "<pre>" );
print_r( $_POST );
echo( "</pre>" );
include ("config.php");
$tbl_name="members";
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//This gets all the other information from the form
$update = $_POST['otherdeets'];
$id = $_POST['userid'];
// Cycle through each member and check that it needs to be added to the db
$useruploadids = mysql_query( "SELECT member_id FROM members" );
while ($row = mysql_fetch_assoc($useruploadids))
{
// Check that the member was sent from the last form
if( isset( $_POST['userid_'.$row['member_id']] ) && $_POST['userid_'.$row['member_id']] == "y" )
{
// update data in mysql database
$sql="UPDATE $tbl_name SET otherdeets='$update' WHERE id='$id'";
$result=mysql_query($sql);
}
}
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='admin-welcome.php'>Admin Home</a>";
}
else {
echo "ERROR";
}
?>
When I run the script it simply says:
Array
(
[userid_1] => y
[otherdeets] => Blah Blah
[Submit] => Update
)
ERROR
Any idea what is wrong? Knowing my luck it would probabaly be a spelling mistake
Thank you
Hey your query is wrong there should be where condition with appropriate column name i.e. "member_id"
And one more thing
you are fetching the $id = $_POST['userid']; which is an error as there is no value exist with that key
rather you do in the if condition before doing the update query, i.e.
$id = $POST['userid'.$row['member_id']];

Categories