I am creating PHP products view details page this script disaply like this URL
http://localhost/zblog/source.php?srcid=1 I want to get title URL from id
like this
http://localhost/zblog/source.php?title=some-title
Here is my code
<?php
include("config.php");
if(isset($_GET['srcid'])) {
$srcid = $_GET['title'];
if($stmt = $con->prepare("SELECT * from products WHERE srcid=?")){
$stmt->bind_param("s", $srcid);
$stmt->execute();
}
$result = $con->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
}
}
}
?>
Your logic is skewed, if the URL is:
http://localhost/zblog/source.php?srcid=1
then you aren't passing the title but if the URL like this:
http://localhost/zblog/source.php?title=some-title
then your if statement will never run because:
if(isset($_GET['srcid'])) {
it relies on $_GET['srcid'].
If you want to use the title then amend that if statement to look for $_GET['title'] instead of $_GET['srcid'] or, try passing both parameters in the URL together.
This would only work if your url included ?srcid=1&title=some-title
This is because of your if statement which only runs if srcid is set.
change
if(isset($_GET['srcid'])) {
to
if(isset($_GET['title'])) {
Related
So I am using this tutorial: https://www.simplifiedcoding.net/android-mysql-tutorial-to-perform-basic-crud-operation/ to try and get data from my local MYSQL server (using Wamp64). I had the undefined index error at first, which I fixed using the isset() statement.
But now it just returns:
{"result":[]}
I have, however, a lot of data in the set column of that database.
Here is the code:
<?php
//Getting the requested klas
$klas = isset($_GET['klas']) ? $_GET['klas'] : '';
//Importing database
require_once('dbConnect.php');
//Creating SQL query with where clause to get a specific klas
$sql = "SELECT * FROM lessen WHERE klas='$klas'";
//Getting result
$r = mysqli_query($con,$sql);
//Pushing result to an array
$result = array();
while ($row = mysqli_fetch_array($r)) {
array_push($result,array(
"id"=>$row['id'],
"klas"=>$row['klas'],
"dag"=>$row['dag'],
"lesuur"=>$row['lesuur'],
"les"=>$row['les'],
"lokaal"=>$row['lokaal']
));
}
//Displaying the array in JSON format
echo json_encode(array('result'=>$result));
mysqli_close($con);
?>
I tried out the
SELECT * FROM lessen WHERE klas='$klas'
statement in my database and it seems to return the correct data.
Any idea what is causing this?
Thanks in advance!
Point 1 is:
isset function only checks if klas is set in the $_GET global array. So if somehow $klas is blank - your query will return empty (without giving error).
So please check values in the $_GET and possibly from where it is accessed. Or you can add condition to avoid empty query like --
if (!empty($_GET['klas'])) {
// rest of the code block upto return
Point 2 is:
You have mentioned if you echo the sql it returns
SELECT * FROM lessen WHERE klas=''{"result":[]}
Here the second part (the JSON) is from echoing the result at the end of your code. So for the first part (i.e. echoing $sql) we see that klas=''. That actually goes to the Point 1 as mentioned above.
So finally you have to check why the value at $_GET is showing blank. That will solve your problem.
UPDATE:
From #GeeSplit's comment For the request
"GET /JSON-parsing/getKlas.php?=3ECA"
There will be nothing in $_GET['klas'] cause the querystring in the url doesn't contain any key.
So either you have to change the source from where the file is called. Or you can change how you are getting the value of klas.
Example:
$tmpKlas = $_SERVER['QUERY_STRING'];
$klas = ltrim($tmpKlas, '=');
Rest of your code will work.
Use this code
<?php
$klas ='';
if(isset($_GET['klas']) && !empty($_GET['klas']))
{
$klas = $_GET['klas'];
require_once('dbConnect.php');
$sql = 'SELECT * FROM lessen WHERE klas="'.$klas.'"';
$r = mysqli_query($con,$sql);
$result = array();
while ($row = mysqli_fetch_array($r)) {
array_push($result,array(
"id"=>$row['id'],
"klas"=>$row['klas'],
"dag"=>$row['dag'],
"lesuur"=>$row['lesuur'],
"les"=>$row['les'],
"lokaal"=>$row['lokaal']
));
}
echo json_encode(array('result'=>$result));
mysqli_close($con);
}
?>
I'm using $id = intval( $_REQUEST['id'] ); , to get ID of the news/posts on my website, and the link to the news is article.php?id=XXX. So when someone type ?id=1 and there's nothing it's empty, no posts, it should go to index page (index.php) instead showing blank page. Is it possible? I've tried via isset something but it didn't worked. Can anyone help me with this, please.
if($ArticleSQL = $mysqli->query("SELECT * FROM articles WHERE id='$id' ")){
...
}
You can check the number of results with $ArticleSQL->num_rows.
So your code will now be:
$ArticleSQL = $mysqli->query("SELECT * FROM articles WHERE id='$id' ");
if($ArticleSQL->num_rows > 0) {
$article = $ArticleSQL->fetch_array();
//show the article here
} else {
//redirection:
header("Location: index.php");
}
You need to check if the query found anything. Your code simply checks if the query succeeded. A query which returns no rows is NOT a failure. It's a perfectly valid result set that happens to have no rows in it.
$result = $mysqli->query(...);
if ($result->num_rows > 0) { ... found something ... }
I am creating a library for PHP scripts and I want to be able to show php code on a html webpage.
I have looked at using highlight_file(); but this will show the whole page
For example, If I have a page called code.php which has an sql query on ( select code from table where sequence = $_GET["id"] ) - example then I use
Highlight_file('code.php?id=123');
This will work but will also show the select query which I do not want to show. I would just want to show the code from the database (code column)
How can I display just the code from the database with the correct colours and formatting etc
UPDATE:
<?php
$conn=mysql_connect("localhost","charlie_library","Pathfinder0287");
mysql_select_db("charlie_library",$conn);
function highlight_code_with_id($id, $conn)
{
$query = "select * from library_php where sequence = '$id' ";
$rs = mysql_query($query,$conn);
$code = mysql_fetch_array($rs);
echo highlight_string($code["code"]);
}
// and, use it like this:
highlight_code_with_id($_GET['id'], $conn);
?>
I have tried the above code, which is just displaying the code in plain text
use highlight_string function, like this:
<?php
highlight_string($code);
?>
where $code is the code you have obtained from your SQL query.
You can create a function around this (something along the following lines):
<?php
function highlight_code_with_id($id, $mysqli) {
$query = $mysqli->query("select code from table where sequence = '$id'");
$code = current($query->fetch_assoc());
return highlight_string($code);
}
// and, use it like this:
echo highlight_code_with_id($_GET['id'], $mysqli);
UPDATE:
Your code is a bit incorrect, you can use:
<?php
$conn=mysql_connect("localhost","charlie_library","Pathfinder0287");
mysql_select_db("charlie_library",$conn);
function highlight_code_with_id($id)
{
$query = "select * from library_php where sequence = '$id' ";
$rs = mysql_query($query);
$code = mysql_fetch_assoc($rs); // change is in this line
echo highlight_string($code["code"]);
}
// and, use it like this:
highlight_code_with_id($_GET['id']);
?>
Note that you do not need to include $conn in your function, it can be ommitted. Also, note that you should use mysqli->* family of functions, since mysql_* family has been deprecated.
Perhaps this would work for you.
This post is originally for HTML, but the answer linked above shows an example using PHP.
I am trying to implement dashboard kind of system. And having problem with passing variable in navigation menu items.
At first user selects "Workspace Name" from list of workspaces.
That link redirects user to projects.php?workspaceid=xxxxxx
Now inside project.php , there is sidebar (static) with other options like
projectsettings.php , credentials.php.
I want to pass variable workspaceid in each sub menu items.
Like projectsettings.php?workspaceid=xxxxxx or credentials.php?workspaceid=xxxxxx
How can I do so ?
UPDATE :
I think i was quite unclear about question.
At first user comes to workspaces.php
It looks like
![workspace selection table]
http://i.stack.imgur.com/eBtSm.png
Then for example they select "internet explorer 7" from table.
Link will take them to project.php?workspaceid=IE7
It looks like
![Project dashboard]
http://i.stack.imgur.com/EfdK1.png
Here all menus (data menus, data variable, setting.php) should have variable of workspace.
You can do something like:
<?php
if(isset($_GET['workspaceid']))
$workspaceid = htmlspecialchars($_GET['workspaceid']);
else
$workspaceid = '';
$query = "SELECT * FROM table WHERE workspaceid = ?";
if($stmt = $mysqli->prepare($query)){
$stmt->bind_param('i',$workspaceid);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()){
$variablename = $row['columnname']; //put your own in here
}
$stmt->free_result();
$stmt->close();
}else die("Query failed to prepare");
The you can navigate by using credentials.php?workspaceid=xxxxxx"
EDITED ANSWER:
You need to set a session once the workspaceid is set
<?php
session_start();
if (isset($_SESSION['workspaceid'])){
//do what you need with this info
}
else if (isset($_GET['workspaceid'])){
//lets set the session
$_SESSION['workspaceid'] = trim(strip_tags($_GET['workspaceid']));
}
else {
//do nothing
}
//with this method there is no need to pass the workspaceid via url
?>
Original answer:
echo the variable in each link you need.
link text
With get variables you should be careful. I usually check and clean them in the header. So a more advanced way to do this is as follows
<?php
if (isset($_GET['workspaceid']){
$workspaceid = trim(strip_tags($_GET['workspaceid']));
}
?>
Then in the link check for isset as well
link text
I am trying to grab ad code from my database and echo it on to the page, but for some reason it is not showing up?
$getad = ("SELECT * FROM ads WHERE place='non-mobile' AND who='adbrite' ");
while($rows = mysql_fetch_array($getad))
{
$code = $rows['code'];
}
$ad1 = $code;
later down the page i print it like this.
<?php print $ad1 ?>
I think your problem is that you don't actually execute the query, you just have saved it in a variable ($getad) and then try to do a fetch af an array containing a string as I see it. If I remeber correctly you have to save you query in a variable, as you did, and then type
$getad = "SELECT * FROM ads WHERE place='non-mobile' AND who='adbrite' ";
$q = $db->query($getad);
// generate results:
while ($q->fetchInto($row)) {
//display or store
}
You should also include checks, for example that this code has extracted at least one row, or that database connection is working, etcetera.