Dynamic pages using php - php

I am trying to create dynamic pages on my website, but it fails.
When I try the code on Xampp it works perfect. There is another thing that I don't understand.
It will catch the id, but not the title or anything with characters from the database.
When I try $title = $_GET['title']; it won't work. It works only with $_GET['id'];
Any help?
Here is the code:
index.php
<?php
include('inc/code.inc.php');
$fetch = mysql_query("SELECT * FROM `star` ORDER BY `title`");
while ($output = mysql_fetch_assoc($fetch))
{
echo ''. $output['title'] .'<br />';
}
?>
run.inc.php
<?php
include_once('inc/code.inc.php');
$newID = $_GET['id'];
$fetch = mysql_query("SELECT * FROM `star` WHERE `id` = $newID");
while ($output = mysql_fetch_assoc($fetch))
{
echo $output['title'] . '<br />' . $output['explain'];
}
?>
Here is the code that won't work:
index.php
<?php
include('inc/code.inc.php');
$fetch = mysql_query("SELECT * FROM `star` ORDER BY `title`");
while ($output = mysql_fetch_assoc($fetch))
{
echo ''. $output['title'] .'<br />';
}
?>
run.inc.php
<?php
include_once('inc/code.inc.php');
$newID = $_GET['title'];
$fetch = mysql_query("SELECT * FROM `star` WHERE `title` = $newID");
while ($output = mysql_fetch_assoc($fetch))
{
echo $output['title'] . '<br />' . $output['explain'];
}
?>

Add title to your URL GET parameter please..Like you have added ID
<?php
include('inc/code.inc.php');
$fetch = mysql_query("SELECT * FROM `star` ORDER BY `title`");
while ($output = mysql_fetch_assoc($fetch))
{
echo '<a href = "run.inc.php?id='. $output['id'] .'"'.'?title='. $output['title'] .'</a><br />';
}
?>
Modify the url according to your needs please.

If you are comparing a string like $_GET['title'] to the column, you need to escape it (as opposed to a numeric ID, which does not need to be escaped).
Try this for run.inc.php
$newID = $_GET['title'];
$fetch = mysql_query("SELECT * FROM `star` WHERE `title` = '$newID'");
while ($output = mysql_fetch_assoc($fetch))
{
echo $output['title'] . '<br />' . $output['explain'];
}
?>

Related

I want to show all the images name in the browser . how will that be possible?

Database Screenshot
Query:
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
$qry = "SELECT `image` FROM product_images WHERE product_id = '".$id."';";
$res = mysqli_query($conn,$qry);
$row = mysqli_fetch_assoc($res);
Question:
I want to show all the images name in the browser . how will that be possible?
Try this:
<?php
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
$qry = 'SELECT image FROM product_images WHERE product_id = \'' . $conn->real_escape_string($id) . '\'';
$res = mysqli_query($qry, $conn);
while($row = mysqli_fetch_assoc($res))
echo $row . '<br />' . "\n";
}
<?php
// absolute or relative path
$base_path = 'http://domain.com/uploads/';
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
$qry = "SELECT image FROM product_images WHERE product_id = '".$id."'";
$res = mysqli_query($conn,$qry);
if (mysqli_num_rows($res) > 0)
{
while ($row = mysqli_fetch_assoc($res))
{
$image_name = $row['image'];
$image_path = $base_path.$image_name;
// To display image name
echo 'Image name = '.$image_name.'<br />';
//To display image
echo '<img src="'.$image_path.'" width="" height="" alt="" title="" />';
}
}
?>

PHP and mysqli to modify CSS

I was experimenting if I could use a mySQL database to store CSS settings. I set up a simple database "colors" with one table "color" that had simple structure tag and color columns. In that, one row is h1 => red.
<?php
//function to dynamically change CSS
$tag = 'h1';
$q = "SELECT * FROM `colors` WHERE `tag`='" . $tag . "'" ;
echo $q . "<br>";
$query = mysqli_query($link, $q);
if ($row = mysqli_fetch_assoc($query))
{
echo $row['color'];
} else
{
echo "error - no such tag";
}
?>
When I tried to convert to a function, the code does not work at all.
<?php
//function to dynamically change CSS
function getCSS($tag)
{
$tag = 'h1';
$q = "SELECT * FROM `colors` WHERE `tag`='" . $tag . "'" ;
echo $q . "<br>";
$query = mysqli_query($link, $q);
if ($row = mysqli_fetch_assoc($query))
{
echo $row['color'];
} else
{
echo "error - no such tag";
}
}
getCSS('h1');
?>
Help please?
My guess is that in
$query = mysqli_query($link, $q);
$link goes out of scope and is empty. You should pass it to the function as well.
For the record: using $tag without escaping could be an sql injection attack possibility.
in function, there is no $link, you shoud define it as a global variable.
At the start of your function add a reference to your global DB link:
function getCSS($tag) {
global $link;
...
This should work:
<?php
$link = mysqli_connect('server_host', 'user', 'password', 'database') OR die('Could not connect because: '.mysqli_connect_error());
//function to dynamically change CSS
function getCSS($link, $tag){
$q = 'SELECT * FROM colors WHERE tag = "' . $tag . '"' ;
$r = mysqli_query($link, $q);
if(mysqli_num_rows($r)>0){ // check if there are results
while($row = mysqli_fetch_assoc($r)){
//echo '<pre>';
//print_r($row); // print the result array for debugging
//echo '</pre>';
echo $row['color'] . '<br />';
}
return $row;
} else { // if no result is found
echo 'No such tag';
}
}
// test it:
echo '<br />if tag is h1<br />';
getCSS($link, 'h1');
echo '<br />if tag is h2<br />';
getCSS($link, 'h2');
?>

mysqli_fetch_array reading result as null?

Any idea why this is showing a result of null? I'm guessing it has to do with where I put $link before I did the query, but it has to be done that way, correct?
function getcatposts($cat_id) {
$qc = #mysqli_query($link, "SELECT * FROM topics WHERE topic_cat='$cat_id'");
while($row = mysqli_fetch_array($qc)) {
$topic_title=$row['topic_subject'];
$topic_id=$row['topic_id'];
}
$qc2 = #mysqli_query($link, "SELECT * FROM categories WHERE cat_id='$cat_id'");
while($row2 = mysqli_fetch_array($qc2)) {
$cat_name=$row2['cat_name'];
}
Updated code:
function getcatposts($cat_id) {
$link = mysqli_connect("localhost", "lunar_lunar", "", "lunar_users");
$qc = mysqli_query($link, "SELECT * FROM topics WHERE topic_cat='$cat_id'");
while($row = mysqli_fetch_array($qc)) {
$topic_title=$row['topic_subject'];
$topic_id=$row['topic_id'];
}
$qc2 = mysqli_query($link, "SELECT * FROM categories WHERE cat_id='$cat_id'");
while($row2 = mysqli_fetch_array($qc2)) {
$cat_name=$row2['cat_name'];
}
echo $cat_name;
echo '<br />';
echo $topic_title;
echo '<br />';
echo $topic_id;
}
New issue is that its displaying like this:
http://gyazo.com/43e8a91b9e0cf4f5e413536907891dcf.png
When the DB looks like this:
http://gyazo.com/1ead8bd0f150838dae3ee4a476419679.png
It should be displaying all three of them and this is a function meaning it will keep redoing all the code until it can't query anymore data. Any ideas?
The problem here is that you're trying to echo the values outside your loop. The variables inside the loop will get overwritten on each iteration and at the end of looping, the variable will hold the value of the last iteration.
If you want to display all the values, move the echo statement inside your loop, like so:
while($row = mysqli_fetch_array($qc))
{
$topic_title = $row['topic_subject'];
$topic_id = $row['topic_id'];
echo $topic_title.'<br/>';
echo $topic_id.'<br/>';
}
$qc2 = mysqli_query($link, "SELECT * FROM categories WHERE cat_id='$cat_id'");
while($row2 = mysqli_fetch_array($qc2))
{
$cat_name = $row2['cat_name'];
echo $cat_name.'<br/>';
}
If you care about the order, you could store the titles, ids and cat_names in arrays like so:
while($row = mysqli_fetch_array($qc))
{
$topic_title[] =$row['topic_subject'];
$topic_id[] = $row['topic_id'];
}
$qc2 = mysqli_query($link, "SELECT * FROM categories WHERE cat_id='$cat_id'");
while($row2 = mysqli_fetch_array($qc2))
{
$cat_name[] =$row2['cat_name'];
}
And then loop through them:
for ($i=0; $i < count($topic_id); $i++) {
if( isset($topic_id[$i], $topic_title[$i], $cat_name[$i]) )
{
echo $cat_name[$i].'<br/>';
echo $topic_title[$i].'<br/>';
echo $topic_id[$i].'<br/>';
}
}
Your function displays only one result because your echo is outside the while loop...
Put the Echo statements inside the loop, or you will print only the last result!
while($row = mysqli_fetch_array($qc)) {
$topic_title=$row['topic_subject'];
$topic_id=$row['topic_id'];
echo $topic_title;
echo '<br />';
echo $topic_id;
}
$qc2 = mysqli_query($link, "SELECT * FROM categories WHERE cat_id='$cat_id'");
while($row2 = mysqli_fetch_array($qc2)) {
$cat_name=$row2['cat_name'];
echo $cat_name;
echo '<br />';
}

php full message show

After few word i put a read more button when i click read more it's show full message, it's Ok, but It's show all my post's full message. I need individual post's full message, Can you please tell me what is the wrong in my code bellow:
This is a function i used in my code in different file, name: "func.php"
<?php
function truncate($mytext,$link,$var,$id) {
//Number of characters to show
$chars = 200;
$mytext = substr($mytext,0,$chars);
$mytext = substr($mytext,0,strrpos($mytext,' '));
$mytext = $mytext." <a href='$link?$var=$id'>read more...</a>";
return $mytext;
}
?>
This is index.php page code:
<?php
include "db/db.php";
$upload_path = "secure/content/blogpostimg";
$sql= mysql_query("SELECT * FROM blog_post ORDER BY post_id DESC");
while ($rel = mysql_fetch_assoc($sql))
{
$id = $rel['post_id'];
$sub = $rel['subject'];
$imgname = $rel['img_name'];
$img = $rel ['image'];
$msg = $rel['message'];
$date = $rel['date'];
$poster = $rel['poster'];
$cat_name = $rel['cat_name'];
echo "<h1>". "$sub" ."</h1>". "<br/>";
echo '<img src="' . $upload_path . '/' . $imgname . '" width="200" /> ';
include_once("func.php");
echo truncate($rel['message'],"index.php","post_id",$rel['post_id']);
}
?>
You need to do a check on index.php to see if the $_GET['post_id'] is set, and if so, display a different MySQL select
if (isset($_GET['post_id']) && $_GET['post_id'] != '') {
$p_id = (int) $_GET['post_id'];
$sql= mysql_query("SELECT * FROM blog_post WHERE post_id = '{$p_id}' ORDER BY post_id DESC");
} else {
$sql= mysql_query("SELECT * FROM blog_post ORDER BY post_id DESC");
}
// Your original code. Only at the end, if $p_id is set, display a full post view instead of the truncate() function

Function not returning data as expected

I've got a function that should return me a set of links based on an user id. What the function does momentarily is that it returns me just one link instead a set of links based on an user id. The function looks like this:
function retrieve_image_link($user_id)
{
$query = mysql_query("SELECT `image_link` FROM `imgit_images` WHERE user_id = '" . intval($user_id) . "'");
while ($row = mysql_fetch_assoc($query))
{
$link = $row['image_link'] . '<br />';
}
mysql_free_result($query);
return $link;
}
So this is the code that should get me multiple links instead of just one. Where is the problem that the query returns a string instead of an array?
Please help!
you need to apply links to an array like $array[] = "link";
function retrieve_image_link($user_id)
{
$query = mysql_query("SELECT `image_link` FROM `imgit_images` WHERE user_id = '" . intval($user_id) . "'");
while ($row = mysql_fetch_assoc($query))
{
//add bracktes to $link to return an array
$link[] = $row['image_link'] . '<br />';
}
mysql_free_result($query);
return $link;
}
or use .= opperator if you want all the links in one big string
function retrieve_image_link($user_id)
{
$query = mysql_query("SELECT `image_link` FROM `imgit_images` WHERE user_id = '" . intval($user_id) . "'");
while ($row = mysql_fetch_assoc($query))
{
//add . to = to append string to $link
$link .= $row['image_link'] . '<br />';
}
mysql_free_result($query);
return $link;
}
It seems the problem is here
$link = $row['image_link'] . '<br />';
At the end of the loop, $link should have last value instead of all the values. You have to append each link or create an array to push each link.
$link = $row['image_link'] . '<br />';
You're overwriting the value of $link in the loop. Add [] to make it an array:
function retrieve_image_link($user_id)
{
$query = mysql_query("SELECT `image_link` FROM `imgit_images` WHERE user_id = '" . intval($user_id) . "'");
$link = array( );
while ($row = mysql_fetch_assoc($query))
{
$link[] = $row['image_link'] . '<br />';
}
mysql_free_result($query);
return $link;
}
use .=
function retrieve_image_link($user_id)
{
$query = mysql_query("SELECT `image_link` FROM `imgit_images` WHERE user_id = '" . intval($user_id) . "'");
while ($row = mysql_fetch_assoc($query))
{
$link .= $row['image_link'] . '<br />';
}
mysql_free_result($query);
return $link;
}

Categories