update value from '0' to '1' on image click? - php

I'm new to php and mysql so sorry if i'm doing it wrong. i have a page on my site that lists the reviews that members give to other other users.
Basically i have approved and deleted in my database which means that after a user sends the review it has to be reviewed by the user before it gets displayed.
once the user clicks the approved image which is a tick it goes to approved_review.php and in their i have my sql code to update the value from 0 to 1 in my database.
It should work exactly the same for the delete but obviously instead of updating the approved column it will update deleted.
the code i have tried is not working i have been working on this for quite some time and can;t figure it out.
Can someone please tell me where i'm going wrong?
Heres the code:
<?php
$reviews_set = get_pending_reviews();
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<p> </p>
<div class="pending-review-content">
<?php
$date = $reviews['date_added'];
?>
<div class="prof-content-pend-reviews" id="reviews">
<div class="message_pic"><?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\">
<img width=\"50px\" height=\"50px\" src=\"data/photos/{$reviews['from_user_id']}/_default.jpg\" /></a>";?>
</div>
<div class="reviews-date"><? echo "$date"; ?></div>
<div class="reviews-from">
<?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\">{$reviews['display_name']}"; ?>
</a> Wrote:
</div>
<div class="reviews-content">
<?php echo "{$reviews['content']}"; ?>
</div>
</div>
<div class="reviews-approve">
<img src="assets/img/icons/tick.png" width="30" height="25" /></div>
<div class="reviews-delete">
<img src="assets/img/icons/cross.png" width="30" height="25" />
</div>
<? } ?>
approved_review.php function:
<?
$sql = "UPDATE `playtime`.`ptb_reviews` SET `approved` = '1' WHERE `ptb_reviews`.`id` =".$_SESSION['user_id']."";
echo "<div class=\"infobox1\">review approved.</div>";
?>

Your approach seems logical. After you loop through your reviews, you click on the tick or delete pngs to update or delete.
So, in approved_review.php
<?php
//you are missing the connection to your mysql database...
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$sql = "UPDATE `playtime`.`ptb_reviews` SET `approved` = '1' WHERE `ptb_reviews`.`id` =".$_SESSION['user_id']."";
//execute the mysql query
$r = mysql_query($sql);
if (!mysql_error())
{
echo "<div class=\"infobox1\">Review Approved.</div>";
}
?>

a little edit rrrfusco's post
// or die for details if mysql_query won't work correct
$r = mysql_query($sql) or die (mysql_error());

Related

Inserting PHP database into HTML table

Hey I've recently been making a website and want to display the data from my database in a grid format opposed to it just listing down the page.
Here is my code right now:
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
I was wondering how I would go about creating a for loop to allow the data from this database in conjunction with the image to span across the page with 7 columns and however many rows down until it reaches the end of the database.
Thanks!
<?php
$query = "Select * from tablename";
$bind = $conn->query($query);
if ($bind->num_rows > 0){
while ($row = $bind->fetch_assoc()){
?>
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
<?php
}
}
?>
Try this, I just add while loop until End Of file (EOF table)

PHP : $_GET is not retrieving data from MySQL database

I have a blog-type php site with mysql database. This blog have some lessons. There's a table "lessons", which contain id, title, text of lesson, etc.
When I display the last lessons on the main page, it works just right.
I connect to db like this:
<?php
$db = mysql_connect ("localhost", "root", "");
mysql_select_db("kursach", $db);
mysql_query("SET NAMES utf8");
$result = mysql_query ("SELECT title, meta_k, meta_d, text FROM settings
WHERE page='index' ", $db);
$myrow = mysql_fetch_array($result);
?>
and display them using loop:
<?php
$result = mysql_query("SELECT id, title, date, description FROM lessons", $db);
$myrow = mysql_fetch_array($result);
do {
printf ("<div class='right-column-content'>
<div class='right-column-content-heading'>
<a href='lesson_view.php?%s'><h1>%s</h1></a>
<h2>%s </h2>
</div>
<div class='right-column-content-content'>
<p>%s</p>
<div class='button'><a href='lesson_view.php?%s' >Читати далі</a></div>
</div>
</div>", $myrow['id'], $myrow["title"], $myrow["date"], $myrow["description"], $myrow["id"]);
}
while($myrow = mysql_fetch_array($result))
?>
I also have a file for full content of the lesson - lesson_view.php. As you can see at the code above, i send the id of lesson to link to this lesson:
lesson_view.php?%s
$myrow["id"]
In the lesson_view.php I connect to db and get the id like this:
<?php
$db = mysql_connect ("localhost", "root", "");
mysql_select_db("kursach", $db);
mysql_query("SET NAMES utf8");
if (isset($_GET['id'])) {$id = $_GET['id'];}
$result = mysql_query("SELECT * FROM lessons WHERE id = '$id' ", $db);
$myrow = mysql_fetch_array($result);
?>
And use this code to display the data:
<div class="right-column-content">
<div class="right-column-content-heading">
<h1><?php echo $myrow['title'] ?></h1>
<h2><?php echo $myrow['date'] ?> </h2>
<h2><?php echo $myrow['author'] ?> </h2>
</div>
<div class='right-column-content-content'>
<p><?php echo $myrow["text"] ?></p>
</div>
</div>
The problem is, when I try to look the full content of lesson (for exaple, /lesson_view.php?1), it doesn't display any data: no title, no text, nothing. I've tried this query directly at MySQL and it works, so, maybe there's some error in php code that I can't find. Will be thankful for any help.
P.S. I'm a beginner at php.
if you want to have in $_GET id then your link should be instead lesson_view.php?%s -> lesson_view.php?id=%s
for example lesson_view.php?id=5 mean that $id = $_GET['id'] will give 5, $id = 5;
<a href='lesson_view.php?%s'><h1>%s</h1></a>
You did not name the 'id' variable.
Change the links to
<a href='lesson_view.php?id=%s'><h1>%s</h1></a>
The only issue is that your printf() have 5 arguments and you are using only four and fourth one is description that you are using here
<div class='button'><a href='lesson_view.php?%s' >Читати далі</a></div>
Solution is that:
printf ("<div class='right-column-content'>
<div class='right-column-content-heading'>
<a href='lesson_view.php?%s'><h1>%s</h1></a>
<h2>%s </h2>
</div>
<div class='right-column-content-content'>
<p>%s</p>
<div class='button'><a href='lesson_view.php?id=%s' >Читати далі</a></div>
</div>
</div>", $myrow['id'], $myrow["title"], $myrow["date"], $myrow["id"], $myrow["description"]);
Move $myrow["id"] in fourth position you will get the ID as query string.
And also add the id in query string.

how to retrieve data from mysql database and populate in list view in jquery mobile

please someone help me to find the solution for how to retrieve data from mysql database and populate in list view in jquery mobile.
am having the php code as follows
<? php
include('libraries/config.php');
$result = mysql_query("SELECT * from category") or die(mysql_error());
while ($obj = mysql_fetch_object($result)) {
$arr[] = $obj;
}
echo json_encode($data);
echo '{"Deals":'.json_encode($arr).'}';
?>
here am getting the data from mysql in json format but i was not known how to populate this in listview, my html page is as follows
<div id="content-area" style="height:auto;">
<br/>
<ul data-role="listview">
<li>
<a href="comfort_list.html">
<div class="content-home-tab1">
<div class="img-content">
<img id="ic_home" src="images/next_btn.png" style="margin-top:37px; margin-left:448px;" width="22" height="28" />
</div>
<div class="content-home-p">
<b>Comfort</b>
</div>
</div>
</a>
</li>
<li>
<a href="#">
<div class="content-home-tab1">
<div class="img-content">
<img id="ic_home" src="images/next_btn.png" style="margin-top:37px; margin-left:448px;" width="22" height="28" />
</div>
<div class="content-home-p">
<b>Handling Your Lenses</b>
</div>
</div>
</a>
</li>
</ul>
</div>
Here in my code am giving static data to list that is comfort and handling your lens, am having this data in db and i need to get that and to be placed here.
in the page i am posting in a form like this
<form method="post" action="help.html">
<ul data-role="listview" >
<?php while ($row = $stmt->fetch()){
?>
<li>
<a href="help1.php?id=<?php echo $row['categoryID']; ?>">
<div class="content-home-tab1">
<div class="img-content">
<img id="ic_home" src="images/next_btn.png" style="margin-top:37px; margin-left:448px;" width="22" height="28" />
</div>
<div class="content-home-p">
<b><?php echo $row['title'];?>
</b>
</div></div></a>
</li>
<?php }?>
</ul>
</form>
and in another page am trying to get the id as follows
<?php
include('libraries/config.php');
$getID = $_GET['id'];
echo $getID;
$stmt = $db->prepare("SELECT * from category where categoryID ='$_GET['id']'");
$stmt->execute();
?>
but when going to that page it is showing nothing and when refreshed then it is showing so that only am asking how to get it to fetch data and print it when the page is loaded. thanks.
As C.S. says, you don't need to use JSON, just loop through your array and add your list items.
Edited to show basic use of PDO, and demo how to access query results.
The mysql_query extension is deprecated as of PHP 5.5.0, so you I would suggest you use PDO_MySQL
First set up your database connection:
$dbName = "your_database_name_here";
$dbUserName = "your_username_here";
$dbPassword = "your_password_here";
$db = new PDO( 'mysql:host=localhost;dbname='.$dbName, $dbUserName, $dbPassword );
Then run your query, and echo your list (or whatever you need from the database by accessing the $row array within the while loop):
$stmt = $db->prepare("SELECT * from category");
$stmt->execute();
while ($row = $stmt->fetch()){
echo "<li>". $row['title'] ."</li>";
}
p.s. you should get rid of that inline css, it's a bad habit to get into.

A log out issue (PHP)

I am not an expert in PHP, and all that I know comes from tuts. I try anyway to do the best I can by myself, but now I have a problem and cannot find what is causing the issue.
I made a bolg using this tutorial. The tutorial is great, easy to understand and everything, the only BUT is that they don't explain how to make a control panel/admin system. So, I made one by myself! I created a simple php/html5 file with icones for the functionalities that exist in the blog: "Add a new blog entry", "Edit an existing blog entry", "Add/manage categories" and "Log out". For the log in mechanism I used this other tutorial. Everything is working fine except for one thing:
After one has logged in the control panel and presses in one of the functions (let's say "Add a new blog entry") and then presses on the button "Back to the control panel", the system automatically logs out and forces you to log in again.
Anybody can explain me why? Bellow is the code of my control panel and the check.php which is included on the control panel (I cut off unnecessary code for other functions like slide shows, css sheets and others):
Control Panel:
<?php require('autent/check.php'); ?>
<p style="background:#48c248; line-height:30px; vertical-align:middle; color:#fff; font-weight:bold;">If you can see this, you're logged in</p>
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title></title>
<!-- Rich text editor -->
<script src="ckeditor/ckeditor.js"></script>
</head>
<body>
<div class="row">
<div class="twelve columns">
<h4>Useful links</h4>
<h5>Archive</h5>
<p>
<?php
mysql_connect ('localhost', 'dbuser', 'dbpass') ;
mysql_select_db ('tablename');
$result = mysql_query("SELECT FROM_UNIXTIME(timestamp, '%Y') AS get_year, COUNT(*) AS entries FROM php_blog GROUP BY get_year");
while ($row = mysql_fetch_array($result)) {
$get_year = $row['get_year'];
$entries = $row['entries'];
echo "Entries from " . $get_year . " (" . $entries . ")<br />";
}
?>
</p>
<h5>Category Archive</h5>
<p>
<?php
mysql_connect ('localhost', 'dbuser', 'dbpass') ;
mysql_select_db ('tablename');
$result1 = mysql_query("SELECT * FROM php_blog_categories ORDER BY category_name ASC");
while($row = mysql_fetch_array($result1)) {
$result2 = mysql_query("SELECT COUNT(`id`) AS entries FROM php_blog WHERE category = $row[category_id]");
$num_entries = mysql_fetch_array($result2);
echo '' . $row['category_name'] . ' (' . $num_entries['entries'] . ')<br />';
}
?>
</p>
</div>
<h4>Control panel - Manage your blog</h4>
<img src="../images/new_blog.png" title="Add a new blog entry" alt="Add a new blog entry"/><br>
<p>Add a new blog entry</p>
</div>
<div class="four columns">
<img src="../images/edit_blog.png" title="Edit a blog entry" alt="Edit a blog entry"/><br>
<p>Edit an existing blog entry</p>
</div>
<div class="four columns">
<img src="../images/cat_blog.png" title="Add/manage categories" alt="Add/manage categories"/><br>
<p>Add/manage categories</p>
</div>
<div class="four columns">
<p> </p>
</div>
</div>
<div class="four columns">
<img src="../images/logout.png" title="End your session" alt="End your session"/><br>
<p>End your session</p>
</div>
<!-- other html and footer follows -->
</body>
</html>
check.php
<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
header("Location: login.php");
exit;
} else {
// the session variable exists, let's check it's valid:
require('autent/config.php');
$userexists = false;
foreach($users as $username => $password) {
if (md5($username.$password.$salt) == $_SESSION['loggedin'])
$userexists = true;
}
if ($userexists !== true) {
exit('<p style="background:#fd0000; line-height:30px; vertical-align:middle; color:#fff; font-weight:bold;">Invalid session: please login.</p>');
}
}
?>
It may because of session timeout problem.try to increase the session time by referring the following url.
How do I expire a PHP session after 30 minutes?
Session variables are stored on your server, not on the users computer like a cookie. So the user can't ever modify $_SESSION variables. It is helpful to create a boolean variable in your session that can be used as a quick flag to tell you if the user is still signed in.
When you create the session for the user, you could create a session variable like this:
$_SESSION['valid'] = TRUE;
From here on out, all you have to do is check if the session is still set to true:
session_start();
if (!$_SESSION['valid']) {
header("Location: login.php");
exit;
}
That code checks if the session is not true and if it is not, send them to login.php
When you sign them out, you can unset the session variable or just set it to false.

Echoing forum Id to new page to display content?

can someone please help me i am having problems creating my forum.
At the moment users can create posts, the post title is listed down the page and then the user is suppose to be able to click the title link and be taken to read_post.php and then this should take the user to another page where the post content can be viewed, i am trying to do this by echoing the forum post id but it doesnt seem to want to work, instead i get this error:
Database query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
can someonee please show me where im going wrong.
here is my sql function:
function read_forum() {
global $connection;
global $forum_id;
$query = "SELECT *
FROM ptb_forum, ptb_profiles
WHERE ptb_forum.id = $forum_id ";
$forum_set = mysql_query($query, $connection);
confirm_query($forum_set);
return $forum_set;
}
here is the link code that takes the user to read_post.php which suppose to echo the forum id and display the content for each individual post.
<?
$forum_set = get_forum();
while ($forum = mysql_fetch_array($forum_set)) {
?>
<div class="forumcase" id="forumcase">
<div class="pend-forum-content">
<?php echo "<strong>{$forum['title']}</strong> - Posted by {$user['first_name']}"; ?>
</div>
here's my code for read_post.php:
<?php
$page_title = "Read Post";
include('includes/header.php');
include ('includes/mod_login/login_form2.php'); ?>
<?php
confirm_logged_in();
if (isset ($_GET['frm'])) {
$forum_id = $_GET['frm'];
}
?>
<?php include('includes/copyrightbar.php'); ?>
<div class="modtitle">
<div class="modtitle-text">Messages Between <?php echo "{$forum['display_name']}"; ?> & You</div>
</div>
<div class="modcontent57">
<br /><br /><br/><br/>
<div class="forum">
<div class="forum-pic"><?php echo "<img src=\"data/photos/{$_SESSION['user_id']}/_default.jpg\" width=\"100\" height=\"100\" border=\"0\" align=\"right\" class=\"img-with-border-forum\" />";?>
</div>
<div class="message-links">
<strong><< Back to Forum
</div>
<br /><br /><br/><br/>
<?php
$datesent1 = $inbox['date_sent']; ?>
<?php
$forum_set = read_forum();
while ($forum = mysql_fetch_array($forum_set)) {
$prof_photo = "data/photos/{$message['user_id']}/_default.jpg";
$result = mysql_query("UPDATE ptb_forum SET ptb_forum.read_forum='1' WHERE ptb_forum.id='$forum_id'")
or die(mysql_error());
?>
<div class="message-date">
<?php echo "".date('D M jS, Y - g:ia', strtotime($message['date_sent'])).""; ?></div>
<div class="img-with-border-msg-read"><?php echo "<img width=\"60px\" height=\"60px\" src=\"{$prof_photo}\"><br />"; ?></div>
<div class="conversation-text">
<?php echo "<i>Conversations between you and </i>{$forum['display_name']}.<br /> "; ?></div>
<div class="message-content">
<?php echo "<strong>Message Subject: </strong><i>{$forum['subject']}</i>"; ?>
<br/>
<br/>
<br/>
<br/>
<?php echo "<strong>Message:<br/></strong></br ><i>{$forum['content']}</i>"; ?>
</div>
<div class="reply-box">
<? include ('message_reply.php'); ?>
</div>
<?php
}
?>
<br/>
<br/>
<br/>
</div>
</div>
<?php include('includes/footer.php'); ?>
</div>
You have an error in your query... Your parameter is not quoted...
$query = "SELECT *
FROM ptb_forum, ptb_profiles
WHERE ptb_forum.id = '$forum_id'";
However... I suggest that you refrain from using the mysql_ family of functions. They are deprecated and due to be removed from PHP in a future release. You should be using parameterized queries using MySQLi or PDO.
Also, global is evil. I've never had a need to use it in 10 years of PHP programming. Neither should you.

Categories