Hiding the particular value in php - php

I am getting a string value in php. It shows all the related lists from the db, but my question here is about the sidebar. I want to display the related list item from the same table, but it is getting the whole list, even the row which is already displayed on the main body.
So, could you please help me out, to hide the row already shown.
$queryy = "SELECT * FROM cate_websites WHERE s = 'approved' ORDER BY rand()";
$result = mysqli_query($con1,$queryy);
$allrows = mysqli_num_rows($result);
if ($allrows > 0){
while ($row_result =mysqli_fetch_assoc($result)) {
$eid = $row_result['cid'];
$etitle =$row_result['title'];
$eimage = $row_result['img1'];
$edesc = substr($row_result['meta'],0,100);
$epermalink=$row_result['permalink'];
if($epermalink == $_GET['articles']) {
echo" here it should not display the get value but it show all the rest of the values";
}
}
}

I think that you wish to skip the particular link from the list. Correct me if I am wrong.
The query should be like this.
$queryy = "SELECT * FROM cate_websites
WHERE s = 'approved' and permalink!='".$_GET['articles']."'
ORDER BY rand()";
Comment if you feel this is not the answer expected.(Also pls add filters and all as this will be pretty injection prone if used like this)

Related

how can I select and display all the rows from the same user, underneath each other with php

Users can add their workouts into a database. There will have more than one workout per user. How can i display the workout names for the logged in user, underneath each other? I have no problem adding the workouts. When I echo the names of the added workouts, it display right next to each other like "workout1workout2". I want to be able to display the workouts underneath each other. I have no idea how to do it. It would be best if I could display each workout as a button.
$query = "SELECT * FROM info WHERE username='$_SESSION[username]' ";
$results = mysqli_query($db, $query);
while($data = mysqli_fetch_array($results)) {
echo $data['workout'];
}
Firstly, you shouldn't put that username in query like that. Look into SQL Injection and how to avoid that in PHP.
As for displaying buttons/workouts. You can embed HTML code inside the echo statement like below. You can add any html like that and it will render HTML.
$query = "SELECT * FROM info WHERE username='$_SESSION[username]' ";
$results = mysqli_query($db, $query);
while($data = mysqli_fetch_array($results)) {
echo "<button>$data['workout']</button></br>";
}
$query = "SELECT * FROM info WHERE username='$_SESSION[username]' Group By username";

mysql_query function only works with mysql_num_rows

I have a very strange problem today. I have a section of code which queries a table based on a GET variable passed from a user input (pretty standard).
However for this to work I have to include a redundant mysql_num_rows variable....
This Works;
<?php
1. $cat = strval($_GET['c']);
2. $query = "SELECT * FROM stock WHERE Category = '$cat'";
3. $num_rows = mysql_num_rows(mysql_query($query));
4. $values = mysql_query($query);
?>
For some reason without line 3 it doesn't work.
By the way this is ultimately used to create an array passed to a google chart which is all fine.
What am I doing wrong?
All code for reference;
<?php
$cat = strval($_GET['c']);
$query = "SELECT * FROM stock WHERE Category = '$cat'";
LINE UNDER QUESTION --> $num_rows = mysql_num_rows(mysql_query($query));
$values = mysql_query($query);
$material = array();
$quantity = array();
$colour = array();
while ($row = mysql_fetch_array($values)) {
array_push($material, $row['Material']);
array_push($quantity, $row['Quantity']);
array_push($colour, $row['Colour']);
}
...Then all the google chart stuff....
?>
Specify columns in the select and use an index.
And make sure the $_GET value is validated.
You probably already have done this, but try the query in e.g. phpMyAdmin.
Maybe all this will help.
SELECT
material,
quantity,
colour
FROM
stock
WHERE
Category = ...
ORDER BY ..
;

Show Newer Data first in mysqli

How would I go about displaying data from new to old? Right now it shows the oldest posts on top and each new post is placed underneath it.
<?php
require_once('connectimage.php');
$sql ="SELECT * FROM table WHERE id=1";
$res = mysqli_query($conn,$sql);
if(mysqli_num_rows($res) > 0){
while($row = mysqli_fetch_assoc($res)){
echo " ".$row['data']." ";
//newest post
//oldest post
}
}
else{
echo "fail";
}
mysqli_close($conn);
?>
Posting this as a community wiki; I've nothing to gain from this.
Read the manual on how to "order by", it's MySQL 101 stuff.
http://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html
It's all in there.
See also http://dev.mysql.com/doc/refman/5.7/en/sorting-rows.html
Btw, community wikis have no rep gain.
As you said:- How would I go about displaying data from new to old?
So you need to go for ORDER BY clause with sort order DESC like below:-
$sql ="SELECT * FROM table ORDER BY id DESC";
Note:-
I removed WHERE id = 1 part because it will give you only one record not all records.Thanks

Mysqli query using $_get where a value is missing [duplicate]

This question already has answers here:
Search Form with One or More (Multiple) Parameters
(2 answers)
Closed 6 years ago.
I have a drop-down box where a user can select a location. Then, there is a text box where they can input a maximum rental price (there will be a few more options but to keep things simple just these in the example). Then this will go to a results.php page and using the $_GET array extract the values and query the database
This works fine if both fields are complete, but if they only wanted to search by location and leave the rent field blank it doesn't work and displays results.php?loc=york&rent= in the URL, which then as I have used the AND function displays no results?
I'm very new to PHP and would very much appreciate anyone who can point me in the right direction or what the correct term to search in google for?
<?php
$location = $_GET['loc'];
$rent=$_GET['rent'];
$result = $mysqli->query("SELECT * FROM dbc_posts WHERE '$location'=city &&'$rent'>rent_price ORDER BY ID ASC");
?>
try this
<?php
// you can check for sql injection
$location = $_GET['loc'];
$rent=$_GET['rent'];
// check if $_GET['rent'] is provided and has a value
if( isset( $_GET['rent'] ) && $_GET['rent'] ) {
$result = $mysqli->query("SELECT * FROM dbc_posts WHERE city='$location' AND rent_price < '$rent' ORDER BY ID ASC" );
// do remaining stuff
} else {
// rent is not provided
$result = $mysqli->query("SELECT * FROM dbc_posts WHERE city='$location' ORDER BY ID ASC");
// do other stuff
}
?>
You can either create 2 queries, or just one with some variables.
$rent = $_GET['rent'];
$rent_options = "";
if(isset($rent)) //add condition
{
$rent_options .= "&& \'rent\'>rent_price";
}
$mysqli->query("SELECT * FROM dbc_posts WHERE '$location'=city".$rent_options." ORDER BY ID ASC");
This way, assuming they chose a rent option, it will be added to the query. If not, it will simply be blank space and be ignored.
If the $rent is empty the you have to check it first before querying to database.
if(!empty($rent))
{
$result = $mysqli->query("SELECT * FROM dbc_posts WHERE city='$location' and rent_price<'$rent' ORDER BY ID ASC");
} else {
$result = $mysqli->query("SELECT * FROM dbc_posts WHERE city='$location' ORDER BY ID ASC");
}

how to filter given value from being fetched from sql in php

I'm having a hard time trying to do this.
I have this code here:
function submit_category_list()
{
$sql_query2 = "SELECT * FROM categories WHERE status = 1 ORDER by title ASC";
$result2 = mysql_query($sql_query2);
if (mysql_num_rows($result2))
{
while ($row = mysql_fetch_array($result2))
{
$catid = $row['id'];
$catname = $row['title'];
$catname = stripslashes($catname);
$content = $content . "<option value=\"$catid\">$catname</option>";
}
}
return $content;
}
This will fetch and display all the entries in the table categories, but theres one entry that I don't want to be displayed or even fetched, because I don't want users be able to submit content to that categorie, if that categorie appear in the submit form they will be able to submit content there...
Edit:
I found this code in the form validation file wich is the best approach to prevent the category submission, but i just dont know where to start or what to do:
// Check to see if the URL is banned.
$array=parse_url($contenturl2);
$domaincheck = $array['host'];
$domaincheck1 = str_replace("www.", "", $domaincheck);
$domaincheck2 = "http://".trim($domaincheck1);
$domaincheck3 = "http://www.".$domaincheck2;
$query = "select id from banned where (url = '$contenturl' || url = '$contenturl2' || url = '$domaincheck' || url = '$domaincheck1' || url = '$domaincheck2' || url = '$domaincheck3') && url != ''";
$result = mysql_query($query);
if (mysql_num_rows($result)>0)
{
$_SESSION['submitstatus'] = "Cannot Approve Submission";
header('Location: '.$frompage.'');
exit;
}
I appreciate any help with this
Update your query to exclude that specific category (assuming your table has a cat_id column):
SELECT * FROM categories
WHERE status = 1 AND cat_id <> the_id_to_exclude
ORDER by title ASC
But make sure you check on submit too! Users can and will fake a GET or POST to specify whatever they want, so a check before insert is mandatory. (Along with all the usual SQL injection protections.)
You'll need to do more than just not displaying that category to the user. A malicious (ie. evil) user might send that type of data to your submit handler anyhow, and therefor you should also add a security check when content is submitted to any category.
Though there are a few different solutions to your questioned problem, among them are:
Change the SQL where claus to filter off on some column value of categories that you do not want.
For example, filtering off all categories with the title "hello world"; WHERE status = 1 AND title <> 'hello world'
filtering off by id: WHERE STATUS = 1 AND id <> 42 (where 42 is the relevant id)
Do the filtering part in your PHP (not really recommended, mysql is better at handling these kind of issues).
.
while ($row = mysql_fetch_array($result2))
{
$catid = $row['id'];
$catname = $row['title'];
$catname = stripslashes($catname);
if ($row['title'] != 'hello world')
$content = $content . "<option value=\"$catid\">$catname</option>";
}

Categories