I have a user database which I want the admin to be able view users. I want the admin to be able to sort the users by different fields. (E.g first name or surname).
My problem:
Is that it wont sort. Im trying to do it using a drop down box and a submit button. I have have tried the Sql in phpMyAdmin to make sure the statement is correct which it is. I have also added a or die to make sure its not a mysql error. Im getting no errors its just not sorting. I have also tried echoing out the drop down to make sure the right value is being added to the variable.
code:
HTML drop down
<form action="View_users.php" enctype="multipart/form-data" name="sort" id="sort" method="post" align="right">
<label>
<select name="sortdropdown" id="sortdropdown">
<option value="<?php echo $sortby; ?>"><?php echo $sortby; ?></option>
<option value="name">First Name</option>
<option value="surname">Surname</option>
<option value="email">Email</option>
<option value="signupdate">Date</option>
</select>
</label>
<label>
<input type="submit" name="button" id="button" value="Sort" />
</label>
</form>
logic:
$sortby = "";
if (!isset($_POST['sortdropdown'])) {
$sortby = "surnname";
}else{
$sortby = mysql_real_escape_string($_POST['sortdropdown']);
}
// This block grabs the whole list for viewing
$product_list = "";
$counter = 0;
$sql = mysql_query("SELECT * FROM users ORDER BY '$sortby' ASC") or die(mysql_error());
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$counter++;
$id = $row["id"];
$email = $row["email"];
$name = $row["name"];
$surname = $row["surname"];
$lastlogin = $row["lastlogin"];
$signupdate = $row["signupdate"];
if (is_float($counter/2)) {$class = "#CCCCCC"; }
else {$class = "white";}
$product_list .= '<tr bgcolor="'.$class.'">
<td>'.$surname.'</td>
<td>'.$name.'</td>
<td>'.$email .'</td>
<td>View More</td></tr>';
}
} else {
$product_list = "There are no users in the system yet";
}
Thanks
The comments are correct, you should be using back ticks `$sortby` instead of '$sortby'
The reason is - single quotes are used to wrap values in MySQL, which is not what you want. You're passing the name of a table column, so you use back ticks because they are used to wrap table column names.
Related
I'm sorry for such a basic question but I'm a bit stumped. I've been trying to build a basic website for a database I created that will graph the data. There are two variable lists that the user selects from and the data from this then is supposed to generate the requested information. However, I'm completely lost as to how to get the data selected from the list to appear in the results page. It will search under the 'country' variable, but not goods. I know that I'm probably doing something stupid, but I'm not sure what it is.
The code I'm using for the dropdown menus on the forms is as follows:
<form id="Country" name="Country" method="get" form action="/database/results_page.php"><table border="1">
<tr>
<td width="68">Name</td>
<td width="48"><span id="sprytextfield4">
<select name="selcountry" id="selcountry" title="<?php echo $row_rsCountrydropdown['']; ?>">
<?php
do {
?>
<option value="<?php echo $row_rsCountrydropdown['country']?>" <?php if($varcountry_rsexportsearch == $row_rsCountrydropdown['country']){echo 'selected';}?>><?php echo $row_rsCountrydropdown['country']?></option>
<?php
} while ($row_rsCountrydropdown = mysql_fetch_assoc($rsCountrydropdown));
$rows = mysql_num_rows($rsCountrydropdown);
if($rows > 0) {
mysql_data_seek($rsCountrydropdown, 0);
$row_rsCountrydropdown = mysql_fetch_assoc($rsCountrydropdown);
}
?>
</select>
<select name="selgoods" id="selgoods" title="<?php echo $row_rsGoodsdropdown['']; ?>">
<?php
do {
?>
<option value="<?php echo $row_rsGoodsdropdown['name']?>" <?php if($vargoods_rsexportsearch == $row_rsGoodsdropdown['name']){echo 'selected';}?>><?php echo $row_rsGoodsdropdown['name']?></option>
<?php
} while ($row_rsGoodsdropdown = mysql_fetch_assoc($rsGoodsdropdown));
$rows = mysql_num_rows($rsGoodsdropdown);
if($rows > 0) {
mysql_data_seek($rsGoodsdropdown, 0);
$row_rsGoodsdropdown = mysql_fetch_assoc($rsGoodsdropdown);
}
?>
</select>
</tr>
</table>
<input type="submit" name="submit" id="submit" value="Submit" onChange="row_rsCountrydropdown.submit()" />
</form>
and my SQL for the results page is as follows:
mysql_select_db($database_cork_normalised, $cork_normalised);
$query_query = "SELECT exports.trade_year, country_id.country, goods.name, exports.Cork FROM country_id, goods, exports WHERE country_id.country_id='varcountry' and goods.goods_id='vargoods'";
$query = mysql_query($query_query, $cork_normalised) or die(mysql_error());
$row_query = mysql_fetch_assoc($query);
$maxRows_query = 10;
$pageNum_query = 0;
if (isset($_GET['pageNum_query'])) {
$pageNum_query = $_GET['pageNum_query'];
}
$startRow_query = $pageNum_query * $maxRows_query;
$vargoods_query = "-1";
if (isset($_POST['selgoods'])) {
$vargoods_query = $_POST['selgoods'];
}
$varcountry_query = "-1";
if (isset($_POST['selcountry'])) {
$varcountry_query = $_POST['selcountry'];
}
If anyone could help I'd be really grateful, this is my first foray into PHP and I'm a bit lost in it.
I'm new to PHP, and I have a simple table from a MySQL database. What I want to do is sort the table using the selected value from the drop down box, with the data from that row in the database.
Here is my code (selected is what I need for the drop down box):
$orderby = $_GET['orderby'];
if ($orderby == 'selected') {
$orderby_query = "WHERE timeStamp LIKE 'selected'";}
else {
$orderby_query = "";
}
$con = mysql_connect("xxxx","xxxx","xxxx");
$db = mysql_select_db("xxxx",$con);
$get = mysql_query("SELECT DISTINCT(timeStamp) FROM deliverys ORDER BY timeStamp DESC");
$timeStamp = '';
while($row = mysql_fetch_assoc($get))
{
$timeStamp .= '<option value ="'.$row['timeStamp'].'">'.$row['timeStamp'].'</option>';
}
<table width="100%" border="1">
<tr style="font-weight:bold;" valign="top" height="30px" bgcolor="#999999">
<td><form name="sorter0" action="index.php" method="get">
<label title="date">Date</label><br />
<select name="orderby" OnChange="document.sorter0.submit();"><br /><br />
<option>Sort by Date</option>
<option value=''>--?--</option><?php echo $timeStamp; ?></option>
</select>
</form></td>
I don't know if I'm adding the data to the drop down box correctly because, if I click, the site reloads and I can see it say:
/index.php?orderby=05-09-2015
If I use a "fixed" value like yes or no it works.
I am quite new to PHP and have been following tutorials and notes so far and have run into a problem.The goal is to create an e-commerce website. One function must be to search the database and display products which I can do. These results must then be able to be sorted (by price/name etc.) I can sort the products however I cannot sort the results f the search as I lose the searched term when reloading. Any help on fixing this or improving the work would be fantastic.
Here is what I have so far..
<div id = "form">
<form action="search3.php" method="get" enctype="multipart/form-data">
Search: <input type="text" name="term" /><br />
<input type="submit" name = "search" value="Submit" />
</form>
</div>
The form above stores "term" the searched term.
<?php
$term = pg_escape_string($_REQUEST['term']);?>
<p>You Searched for:</p><?php Print $term; ?>
<br><br>
Sort by
<form method=get style="display: inline;" name='orderby_form'>
<input type=hidden name='param1' value="<?php print $param1; ?>">
<input type=hidden name='param2' value="<?php print $param2; ?>">
<select name=orderby onChange="orderby_form.submit();">
<option value='none'>v-v-v</option>
<option value='title'>Title</option>
<option value='price_asc'>Price (Low - High)</option>
<option value='price_desc'>Price (High - Low)</option>
</select>
</form>
This gives options on how the user wants to sort the data
<?php
$selected = array();
$orderby = $_GET['orderby'];
if(!$orderby)
{ $orderby = ''; }
if($orderby == 'price_asc')
{
$orderby_query = "order by price asc";
}
else if($orderby == 'price_desc')
{
$orderby_query = "order by price desc";
}
else if($orderby == 'title')
{
$orderby_query = "order by title";
}
else { unset($orderby); }
// If $orderby was valid set the selected sort option for the form.
if($orderby)
{
$selected[$orderby] = 'selected';
}
// Now run your SQL query with the $orderby_query variable. Ex:
$query = "SELECT * FROM products WHERE (title LIKE '%$term%' OR description LIKE '%$term%') $orderby_query";
// SQL code goes here..
$result = pg_query($query);
if(pg_num_rows($result) == 0) {
echo "No records found";
}
else{
while ($row = pg_fetch_assoc($result)) {
?>
After the final bit here I just print the results into tables
These two pictures show the search results.
The first prints all products with "bass" in the title/description and is unsorted.
http://oi61.tinypic.com/28clzqs.jpg
The second is when I sort the data.The search 'term' is lost and ALL products become sorted.
http://oi59.tinypic.com/34g39tu.jpg
Simple enough work around. Thanks to Sean for showing me.
"add your $term to the 2nd form"
Which is
<input type=hidden name='param2' value="<?php print $param2; ?>">
//Added -VV-
<input type=hidden name='term' value="<?php print $term; ?>">
Works perfectly well now.
in my db i have 20+ columns. i added 19 columns throught input form and stored in db succesfully. i fetch few rows from db in my main page. in my main page 1 more column is there. that is status column, it is a combo box type. if i click status column it should show 4 values. i want to select one of the values and then when i click save button it must go to stored in db with that same ID. how to do that? i tried but its not updated in mysql db...
mainpage combo box coding:
echo "\t<td><form action=statusdb.php method=post>
<select name=update><option value=empty></option><option value=Confirm>Confirm</option><option value=Processing>Processing</option><option value=Pending>Pending</option><option value=Cancelled>Cancelled</option></select>
<input name=\"update[".$a_row['slno']."]\"; type=submit id=id value=Save></form>
</td>\n";
status db coding:
if (isset($_GET['id']))
{
$id = mysql_real_escape_string($_GET['id']);
$sql = mysql_query("UPDATE guestdetails SET status = '" . $_POST['update'] ."'");
if(!$sql)
{
die("Error" .mysql_error());
}
}
help me how to do that?
In your IF should be $_POST, not $_GET
Also, need to add WHERE clause, like this:
if (isset($_POST['id']))
{
$id = mysql_real_escape_string($_POST['id']);
$update= mysql_real_escape_string($_POST['update']);
$sql = mysql_query("UPDATE guestdetails SET status = '$update' WHERE id='$id'");
if(!$sql)
{
die("Error" .mysql_error());
}
}
Also, you used name update twice, once in <select> once in <input>, take that one from <input> out, make it hidden field with name id and value of your slno row:
echo "\t<td>
<form action=statusdb.php method=post>
<select name=update>
<option value=empty></option>
<option value=Confirm>Confirm</option>
<option value=Processing>Processing</option>
<option value=Pending>Pending</option>
<option value=Cancelled>Cancelled</option>
</select>
<input name='id' type='hidden' value='".$a_row['slno']."';>
<input type='submit'>Save</button>
</form>
</td>\n";
Try like below:
<form action="statusdb.php" method="post">
<?php
while($a_row = mysql_fetch_array($sql)) {
$sl_no = $a_row['slno'];
echo '<select name="update['.$sl_no.']"><option value=empty></option><option value=Confirm>Confirm</option><option value=Processing>Processing</option><option value=Pending>Pending</option><option value=Cancelled>Cancelled</option></select> ';
echo '<input type="hidden" name="sl_no[]" value="'.$sl_no.'" />';
}
?>
<input name="update_rows" type="submit" value="Save">
</form>
<?php
if(isset($_POST['update_rows'])) {
$nums = $_POST['sl_no'];
$update = $_POST['update'];
foreach($nums as $sl) {
$sl_no = $sl;
$val = $update[$sl_no];
$sql_update = mysql_query("UPDATE guestdetails SET status = '$val' WHERE sl_no='$sl_no'");
}
}
?>
you are not submitting any id to status db.
I am working on a login system and have a basic admin/level system implemented in to it. Right now the levels are just numbers (1-9). I do however have them labeled as different names or groups. For example, 1=member, 2=Supporter and so on. I also have a member list that displays the user id, level, and time of last activity. However it only shows the number for the id (as to be expected). My question is how would I set it up so the numbers end up displaying the name/group on the member list?
Thanks in Advanced, Josh
Here is the code for the table:
<?php
include("include/session.php");
function displayUsers(){
global $database;
$q = "SELECT username,userlevel,email,timestamp "
."FROM ".TBL_USERS." ORDER BY userlevel DESC,username";
$result = $database->query($q);
/* Error occurred, return given name by default */
$num_rows = mysql_numrows($result);
if(!$result || ($num_rows < 0)){
echo "Error displaying info";
return;
}
if($num_rows == 0){
echo "Database table empty";
return;
}
/* Display table contents */
echo "<table align=\"left\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n";
echo "<tr><td><b>Username</b></td><td><b>Level</b></td><td><b>Last Active</b></td></tr>\n";
for($i=0; $i<$num_rows; $i++){
$uname = mysql_result($result,$i,"username");
$ulevel = mysql_result($result,$i,"userlevel");
$time = mysql_result($result,$i,"timestamp");
echo "<tr><td>$uname</td><td>$ulevel</td><td>$time</td></tr>\n";
}
echo "</table><br>\n";
}
?>
my "array":
<?php echo $form->error("upduser"); ?>
<table>
<form action="adminprocess.php" method="POST">
<tr><td>
Username:<br>
<input type="text" name="upduser" maxlength="30" value="<?php echo $form->value("upduser"); ?>">
</td>
<td>
Level:<br>
<select name="updlevel">
<option value="1">Member
<option value="2">Supporter
<option value="3">Donor
<option value="4">VIP
<option value="5">Veteran
<option value="7">Co-Founder
<option value="8">Founder
<option value="9">Admin
</select>
</td>
I just need help setting up an array with the code
You should add a table in your database with the info for the levels (id and name) then use a join in your query to get the name with the user details.
The other solution, which in my opinion is less advisable, would be to hard-code your array and display the corresponding level name:
function displayUsers(){
$levels = array('1'=>'Member','2'=>'Supporter','3'=>...);
//do_query
//loop results
$ulevel = $levels[mysql_result($result,$i,"userlevel")];
//continue loop
}