How do i get make session id from a loop of mysqli? - php

I am learning php and i need some help in one of my project.
How can i get id of a specific row while displaying the value of other column (in my case title) from a loop so that i can make it $SESSION and forward it to next page.
For now what i want to do is get an id of the row when i click the title and show the post in the next page and work further on it. I think i can use javascript for but don't know how to do so.
My codes (by which i am currently fetching data from database)
<div class="list-group">
<?php while($row = mysqli_fetch_array($search_result)):?>
<?php echo $row['title'];?>
<?php endwhile;?>
</div>
Thanks in Advance

Assume view_page.php is the page you want to view content of that row on when you click the row and you will use get method to extract id to go fetch content of a specific row in your database.
" class="list-group-item list-group-item-action list-group-item-dark">
on url address you will see something like after view_page.php?id=1
use this coode to get that row id: $id = $_GET['id'];

Related

Performing mysql update based on passed variable from link?

I've built a list of links based off of an array from a select query.
Linking to the page in question works fine, but I've added a link option to each to delete the page in question. The only issue is, I'm wondering the best way to pass the page ID of the given page into the query that deletes (deactivates) the page.
I build my page links like this:
<?php foreach($result as $page): ?>
<div class="col-lg-3 col-sm-6 d-flex" style="padding-bottom: 20px;">
<div class="card text-center flex-fill">
<h2><?php echo $page["title"] ?></h2>
<p><?php echo $page["Name"]?></p>
<a target="_blank" href="showpage.php?pageid=<?php echo $page['id'] ?>">View Page</a>
Edit Page
Delete Page <!--this is where I need to pass $page['id'] and use it in the query below-->
</div>
</div>
<?php endforeach?>
The query to delete/deactivate:
$deletePage = "
UPDATE pages
set active = 0
where id = /*this is page ID from previous link*/
";
$performDelete = $mysqlConn->query($deletePage);
What is the best practice for me to pass the pageID of the link into this query?
You're passing the target page through as a URL parameter on the first page, so it will be available on the second page via $_GET, so the standard structure would be:
$deletePage = "UPDATE pages set active=0 where id=$_GET['pageid']";
However, note that the above is open to attacks; it's trivial for a user to simply modify the value in their address bar, and thus update the wrong row. In fact, with a bit of SQL injection, it's even possible for a user to completely delete your entire database with the above PHP / SQL.
The best way to avoid this is paramaterised queries (I assume MySQLi in the following):
$stmt = $mysqlConn->prepare("UPDATE pages set active=0 where id=?");
$stmt->bind_param('i', $_GET['pageid']);
$stmt->execute();
Note that you'll still want to assure that the user viewing the page is authorised to make the UPDATE command, most likely by checking against their $_SESSION.
Pass it in link and get it in second page using $_GET['id']
Delete Page

Using PHP hyperlinks to extract data from mysql table

I am currently stuck on how to use $_GET from a hyperlink and send it to another php page for processing.
I have 2 php pages. The first page, we'll call it page1.php, gets user input (customerID) from a HTML page through a textbox and according to that id, goes into the database and finds relative information such as the Customer Name and the Address etc using logical joins.
$id = $_GET['custID']; //This is to obtain the input from the HTML page.
I have that working perfectly.
My next task is to create another php page, we'll call it page2.php, in where i am required to output only the customerID and the customerName, and from there i am required to hyperlink every record in the Customer ID row which references to page1.php and when the user clicks any of those links, it should only show information according to that customerID in a table.
So far i can make each record in the Customer ID row link to page1.php but i am unable to output any results.
<?php
while ($row = mysql_fetch_array($rs)) { ?>
<tr>
<td><?php echo $row["customerName"]?></td>
<td><?php echo "<a href='page1.php?'>{$row["custID"]}</a>"?></td>
</tr>
<?php }
Any help would be much appreciated.
Change the link to following
<?php echo ''.$row["custID"].'';?>

Print multiple PHP Files whitout opening them

I have a PHP system that have orders, and items related to orders,
So I want to print every item related to specific order, without open the file.
I'm using a SQL query to show the pages:
SELECT * FROM items WHERE order_id = ".$oid.";
to collect items from this order and for each one, I write
<a href="view.php?iid=<?php echo $theResult['id'];?>" target="_blank">
Item <?php echo $theResult;?>
</a>
So the page view.php is a default that receive item_id in $_GET and build the page as I want.
There's no problem until here, BUT:
I want to print any result for view.php, for each item in order, without have to open and click to print everyone.
If you insist on still using the single view file view.php, a bit of a hacky solution could look like this:
<?php
foreach ($items as $iid) {
$_GET["iid"] = $iid;
require("view.php"); //This will print whatever the view.php page does
}
?>
Here $items specifies an array full of the ID's from the database.
By setting $_GET["iid"] to the id which we'd get from our database ($iid) and then including view.php, you can emulate a call to the file (and hence we'd print the view.php page for every item in the database).
If this is not what you want, it'd be awesome with some more info so we can help you along the way.

delete a particular row from session array

I am a beginner in php.
I want to know how a particular row in a php session array can be deleted(not from the database, but from the page only).
I have a table namely, issues having columns issue_id & issue_descrp.
The page is displayed with a table in which each row contains issue and its corresponding id. Each row contain a delete button too. What I want is to delete the corresponding row from the page when I click the button.
This is my php code:
<?php
foreach($_SESSION['meeting_issues'] as $meeting_issues)
{
$query="select issue_id,issue from issues where issue_id='$meeting_issues'";
$result=$_SESSION['connection']->query($query) or die (mysql_error());
while($row = $result->fetch_assoc())
{?>
<?php $issue_id=$row['issue_id']; ?>
<tr><td><?php echo $row['issue_id']; ?></td><td><?php echo $row['issue']; ?></td><td><input type="button" name="<?php echo $row['issue_id']; ?>" id="button" value="Remove"/></td>
</tr>
<?php
}
}
?>
Hope my question is clear. Please help me. Thanks in advance.
use unset to delete array elements such as those in $_SESSION
http://php.net/manual/en/function.unset.php
do not delete the whole session this way, use this instead
http://php.net/manual/en/function.session-unset.php
To remove a row on the page itself, you will need Javascript or jQuery. jQuery is advised because of all the possibilities it gives and it is easier to use than normal Javascript.
jQuery:
$("#button").parents("tr:closest").remove();
Javascript:
document.getElementById('button').parentNode.parentNode.parentNode.removeChild(document.getElementById('button').parentNode.parentNode);
As you can see, jQuery is alot faster and more easy to type.
You are using an ID for the buttons, but the ID is always the same. I recommend using classes for this, because an ID should be unique on a page.
jQuery website

How to implement a "save" feature with dynamic content using cookies

I would like to implement a "save" feature, which allows my users to save a particular entry that I have presented to them from my database. For example, if they see something they like, they can press the "save" button and it will save to a "bookmarks" page.
Here is my code:
<?php
// Get all the data from the example table
$result = mysql_query("SELECT * FROM table WHERE item <> 0 ORDER BY id")
or die(mysql_error());
// keeps getting the next row until there are no more to get
$i = 10;
while ($i > 0) {
$i--;
$row = mysql_fetch_array( $result );
if ($row['id'] != "" ) {
?>
<!-- Some content in this div-->
<div class="content">
<img src="<?php echo $row['logo']; ?>"/>
<?php echo $row['id']; ?>
</div>
<!-- This div will have the save icon that users can press -->
<div class="save">
<img src="images/saveIcon.png" />
</div>
<?php }} ?>
<div class="bookmarked">
Every single div that is labled as "content" that has been saved, will be displayed here.
</div>
Question: How would I implement a feature to allow a user to add the particular ID from SQL (of the dynamically created content) to a cookie variable, so that it can be reproduced at a later time.
If I have understood your question correctly, you want a button which marks the related DIV, and stores this relation to cookies.
First of all, I would add the id of entry as class to the DIV. Then you'll have the possibility to get the id easily via javascript (for further implementations).
To set the favorites via php, you should add a link to a new php script. This script sets the cookie, via the setcookie function in PHP.
In your bookmarked-DIV you would make the same while-loop as above, but check the ids with the values in the cookies. You can access cookies with $_COOKIE. Just show items, which are containing in the cookies.
This is a possible way of implementation.
You would probably want to use PHP to store it in it's session rather than using Javascript.
You could achieve this by simply pushing the ID into a session variable:
<?php
session_start();
if(isset($_GET['id']) && is_numeric($_GET['id'])){
$_SESSION['bookmarks'][] = intval($_GET['id']);
}
Using AJAX you could then hit this script with an the ID of the entity to bookmark and it'll get saved into the session.

Categories