I need to set two variable in a url, $id and $job. The data should come from mysql select statement, [id] and [job_number_id]. My page displays a client job proposal and if there is more than one all proposals are displayed but the [id] and the [job_number_id] determines what is displayed on the webpage. I dont have a clue as to how this done. Any help would be greatly appreciated. Here's the code:
<?php
$url = 'http://localhost/estimate_lar/homepage.php';
$id = $_SESSION['id'];
$query = "SELECT id, client_job_name, job_number_id
FROM `job_name`
WHERE `id`='$id'";
$allJobs = $db->query($query);
?>
<?php foreach ($allJobs as $site_title) : ?>
<p>
<tr><?php echo ''.$site_title['client_job_name'],$site_title['job_number_id']. '<br />'.''; ?>
<td></td>
</tr>
</p>
<?php endforeach; ?>
If you want the variables to be avaialble in the URL you need to read them with $_GET.
Getting the arguements from a url such as index.php?id=1&job_number_id=3 will look like that:
if (isset($_GET['id']) && isset($_GET['job_number_id'])) {//make sure both arguments are set
$id = $_GET['id'];
$job_number_id = $_GET['job_number_id'];
}
To set it in your foreach statement:
<?php foreach ($allJobs as $site_title) : ?>
<p>
<tr><?php
$url = "http://localhost/estimate_lar/homepage.php?id=" . $site_title['id'] . "&job_number_id=" . $site_title['job_number_id'];
echo ''.$site_title['client_job_name'],$site_title['job_number_id']. '<br />'.'';
?>
<td></td>
</tr>
</p>
<?php endforeach; ?>
PLEASE remember to read about SQL injection and making sure you are escaping your inputs. Or even better - use a prepared statement.
Currently your script is volunerable, since everyone could just alter the URL and manipluate your DB.
Hope this helps!
Try this.
<?php
session_start();
$id = $_SESSION['id'];
$url = 'http://localhost/estimate_lar/homepage.php';
if($id){
$query = "SELECT id, client_job_name, job_number_id FROM `job_name` WHERE `id`='$id'";
$allJobs = $db->query($query);
}else{
echo "Id not in session";
}
?>
<table>
<?php if ($allJobs) { foreach ($allJobs as $site_title) : ?>
<tr>
<td>
<?php echo $site_title['job_number_id']. " ".$site_title['job_number_id']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php }else{ echo 'No results Found' ;} ?>
This may help you.
Related
im facing a problem for displaying edit,delete buttons for the comments that belongs to that user ,,,this is my code and dont get it where is the problem and how to solve it (because like this: edit and delete buttons will show up for the all comments !
$results = mysqli_query($c, "SELECT * from comments where idpub=".$_GET['com']."and iduser=".$_SESSION['id']);
while ($rows = mysqli_fetch_assoc($results)) {
?>
<?php echo $rows['username']; ?>
<?php echo $rows['textcom']; ?>
<a href="comment2.php?edit=<?php echo $rows['id']; ?>" class="edit_btn" >Edit</a>
Delete
<?php } ?>```
You have to check logged in User's Id and the User id of Comment who add that comment. If Both ids are the same then display those buttons.
$results = mysqli_query($c, "SELECT * from comments where idpub=".$_GET['com']."and iduser=".$_SESSION['id']);
based on the above code I think you are fetching all the comments of user id which is store in a session, so it shows edit delete buttons with each comment. So You have to change your code like below.
$results = mysqli_query($c, "SELECT * from comments where idpub=".$_GET['com']);
while ($rows = mysqli_fetch_assoc($results)) {
?>
<?php echo $rows['username']; ?>
<?php echo $rows['textcom']; ?>
<?php if($_SESSION['id'] == $rows['userid']){ ?>
<a href="comment2.php?edit=<?php echo $rows['id']; ?>" class="edit_btn"
>Edit</a>
Delete
<?php } }?>
I am unable to display data from a table from a MYSQL database using PHP OOP. However, I'm not sure if I'm unable to display the data because I'm not actually fetching the data in the first place.
I've tried fetching and displaying the data using only a PHP array and no HTML in my method and I figured this wouldn't be working because I wasn't using HTML list tags to format the data from the database. I've considered using a HTML table but I have seen displays from databases using lists work a few times before and I want to know why this doesn't work how it should.
I've tested for MYSQL connection and it does exist.
* M_PRODUCTS.PHP *
<?php
class Products
{
private $Conn;
private $db_table = "toys";
function __construct() {
// here we're making sure that the connection from $conn in "init.php" is transferred
// into our own private $conn property for usage in this object
global $Conn;
$this->Conn = $Conn;
}
// fetches and displays all products from db
public function fetch_all_products($id = NULL)
{
if ($id == NULL)
{
$data = [];
if ($result = $this->Conn->query("SELECT * FROM " . $this->db_table . " ORDER BY name"))
{
if ($result->num_rows > 0)
{
while ($row = $result->fetch_array())
{
$data = array(
"id" => $row["product_id"],
"name" => $row["name"],
"price" => $row["price"],
"image" => $row["image"]
);
}
return $data;
}
else
{
return "<h1>Oops... Something went wrong!</h1>";
}
}
}
}
}
* INDEX.PHP *
<?php include("init.php");
include("models/m_products.php");
?>
<body>
<div id="whitespace">
<?php
$products = fetch_all_products();
?>
<?php foreach($products as $row) { ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->price; ?></td>
<td><img src="<?php echo $row->image_path(); ?>" alt="<?php echo $row->name; ?>" width="100" /></td>
</tr>
<?php } ?>
</div>
</body>
I expect the images of my products to be displaying in my index.html file. However, nothing appears.
I also get this error message in the JavaScript console: Failed to load resource: the server responded with a status of 404 (Not Found). This would explain a lot but as I said I tested my database connection and it works. I'm not sure how or why this message is coming from the JavaScript console if I'm not using JavaScript. I thought it would be worth mentioning anyway.
According to your code the function returns some data, but the thing is that you have not printed it, so instead of return in the function you can use echo or just put
echo $Products->fetch_all_products();
Strange code, I would do something like that.
1. First the function find all()
2. index.php echo - output
<?php
$products = find_all_products();
?>
<?php foreach($products as $row) { ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->price; ?></td>
<td><img src="<?php echo $row->image_path(); ?>" width="200" /></td>
</tr>
<?php } ?>
There are 2 major issues I can see in your code
1) you need to update your comparison operator from
if ($id = NULL)
to
if ($id == NULL)
2) you need to update your code in index.php from
<body>
<div id="whitespace">
<h1><?php echo shop_name ?></h1>
<?php
echo $Products->fetch_all_products();
?>
</div>
</body>
to
<body>
<div id="whitespace">
<?php
$products = find_all_products();
?>
<?php foreach($products as $row) { ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->price; ?></td>
<td><img src="<?php echo $row->image_path(); ?>" alt="<?php echo $row->name; ?>" width="100" /></td>
</tr>
<?php } ?>
</div>
</body>
I hope it will sort out your issue
Correct your comparison operator it should be.
if ($id == NULL)
instead of
if ($id = NULL)
in your function fetch_all_products().
correct the code in index.php to print an array use print_r() or loop through the result array.
Brothers your help needed.
i am trying to develop a chat application in which i am displaying friends at the right top but the following error occurs. i have tried again and again but cant solve the problem.
my code is:
function is:
function get_sessionId() {
$session_id = $this->session->userdata('logged_in');
$data['usernames'] = $this->chat_model->show_user($session_id['id']);
$this->load->view('chat_view',$data);
}
foreach loop is:
<table>
<?php foreach($users as $row): ?>
<tr>
<td>
<a href="javascript:void(0);" onclick="set_user(<?php echo $row->id; ?>)">
<?php echo $row->username;?>
</a>
</td>
</tr>
<?php endforeach; ?>
</table>
and the query that i have write in model is:
function show_user($sessionId) {
$query = $this->db->query("select id, username from users where id <>'$sessionId'");
return $query->result();
}
In your Controller you have assigned the users to:
$data['usernames'] = $this->chat_model->show_user($session_id['id']);
$this->load->view('chat_view', $data);
So in View Section it should be like this:
<?php foreach($usernames as $row): ?>
not
<?php foreach($users as $row): ?>
Having a little trouble displaying data from a table. Been looking at my code for the past few hours and can't seem to see the problem. What I am trying to do is when a team is clicked on it will go into the players table and display any player that has that team name on the team page. I keep getting a blank page:
index.php
This is the case that launches the team_view.php
case 'view_team':
$team = $_GET['name'];
$teams = get_players_by_team($team);
include('team_view.php');
break;
team_view.php
<?php include '../../view/header.php'; ?>
<?php include '../../view/sidebar_admin.php'; ?>
<div id="content">
<h1>Team Roster</h1>
<!-- display product -->
<?php include '../../view/team.php'; ?>
<!-- display buttons -->
</div>
<?php include '../../view/footer.php'; ?>
team.php
<?php
$team = $team['name'];
$first = $player['first'];
$last = $player['last'];
$age = $player['age'];
$position = $player['position'];
$team = $player['team'];
?>
<table>
<?php foreach ($players as $player) :
?>
<tr>
<td id="product_image_column" >
<img src="images/<?php echo $player['player_id']; ?>_s.png"
alt=" ">
</td>
<td>
<p>
<a href="?action=view_player&player_id=<?php echo
$player['player_id']; ?>">
<?php echo $player['first']; ?>
<?php echo $player['last']; ?>
</a>
</p>
</td>
</tr>
<?php endforeach; ?>
</table>
product_db.php
<?php
function get_players_by_team($team) {
global $db;
$query = 'SELECT * FROM players
WHERE team = :team';
try {
$statement = $db->prepare($query);
$statement->bindValue(':team', $team);
$statement->execute();
$result = $statement->fetch();
$statement->closeCursor();
return $result;
} catch (PDOException $e) {
display_db_error($e->getMessage());
}
}
You never define $players it looks like what you are expecting to be $players is actually $teams.
$teams = get_players_by_team($team);
Additionally youre using $player at the top of the script instead of inside the loop which makes no sense.
I've read all the posts I could find about this issue, but, to date, none of the solutions have worked for me. Obviously, I'm overlooking something important. I also don't know how to debug sessions. I read one article, PHP session Debugging, but it was over my head.
So, much like the other issues, when I navigate to another page in my app, whether through a link or a form submit, my session disappears. I have no idea why my session vanishes. If someone has the time to help me investigate, it would be greatly appreciated.
These are my php.ini settings
; Name of the session (used as cookie name).
session.name = PHPSESSID
; The path for which the cookie is valid.
session.cookie_path = /
This is the first view to display
<?php
session_start();
if (!isset($_SESSION['session_id'])) {
$_SESSION['session_id'] = session_id();
}
if (!isset($_SESSION['invoices'])) {
$_SESSION['invoices'] = $invoices;
}
if (isset($_SESSION['session_id'])) {
print_r($_SESSION['session_id'] . " in invoiceList.<br />");
} else {
echo 'No session ID set in invoiceList <br />';
}
?>
<div>
<table>
<tr>
<th>Customer Name</th>
<th>Invoice Date</th>
<th>Invoice Number</th>
</tr>
<tr>
<?php
include_once 'form/editInvoice.php';
if (isset($invoices)) {
foreach ($invoices as $invoice) {
?>
<tr>
<td><?php echo $invoice['customer_name'] ?></td>
<td><?php echo $invoice['invoice_date'] ?></td>
<td><?php echo $invoice['invoice_number'] ?></td>
<td><a href='<?php echo $_SERVER['SCRIPT_NAME']; ?>/retrieve?class=InvoiceLineItems&id=<?php echo $invoice['invoice_id']; ?>'><?php echo $invoice['invoice_id']; ?></a></td>
</tr>
<?php
}
} else {
echo 'No invoices retrieved.';
}
?>
</tr>
</table>
</div>
Here is the included form:
<?php
session_start();
if (isset($_SESSION['session_id'])) {
print_r($_SESSION['session_id'] . "in editForm<br />");
} else {
echo 'No session ID set in editForm <br />';
}
if (!$_POST) {
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<fieldset>
<legend>Enter Updated PO Number</legend>
<li>PO Number: <input type="text" name="po_number"/></li>
</fieldset>
<input type="submit" value="Submit" />
<input type="button" onclick="alert('Changes Canceled.')" value="Cancel"/>
</form>
<?php }
?>
And finally, the detail page for when the user clicks a link in the main page.
<?php
session_start();
if (isset($_SESSION['session_id'])) {
print_r($_SESSION['session_id'] . "<br />");
} else {
echo 'No session ID set invoice<br />';
}
?>
<h1>Invoice Detail</h1>
<div>
<?php
foreach ($partnerInfo as $info) {
switch ($info['role_indicator']) {
case 'remit_to':
?>
<div id="remit">
<ul>
<li>PLEASE REMIT TO:</li>
<li><?php echo $info['partner_name']; ?></li>
<li><?php echo $info['street_name']; ?></li>
<li><?php echo $info['city_name']; ?>, <?php echo $info['state']; ?> <?php echo $info['postal_code']; ?></li>
</ul>
</div>
<?php break; ?>
<?php case 'seller': ?>
<div id = "seller" >
<ul>
<li>Service Site:</li>
<li><?php echo $info['partner_name']; ?></li>
<?php
if ($info['partner_aux_info'] !== NULL) {
?><li><?php echo $info['partner_aux_info']; ?>
<?php }
?>
</li>
<li><?php echo $info['street_name']; ?></li>
<li><?php echo $info['city_name']; ?>, <?php echo $info['state']; ?> <?php echo $info['postal_code']; ?></li>
<li>(405)677-0221</li>
</ul>
</div>
<?php break; ?>
<?php case 'sold_to': ?>
<div id="buyer">
<ul>
<li>Bill To: </li>
<li><?php echo $info['partner_name']; ?></li>
<li><?php echo $info['street_name']; ?></li>
<?php
if ($info['suite_info'] !== NULL) {
?><li><?php echo $info['suite_info']; ?>
<?php }
?>
</li>
<li><?php echo $info['city_name']; ?>, <?php echo $info['state']; ?> <?php echo $info['postal_code']; ?></li>
</ul>
</div>
<?php break; ?>
<?php
}
}
?>
<h1>Line Items</h1>
<table>
<th>PO Number</th>
<th>PO Issued Date</th>
<th>Description</th>
<th>Service Start Date</th>
<th>Service End Date</th>
<th>Shipped Date</th>
<?php foreach ($invoiceLineItems as $lineItem) { ?>
<tr>
<td><?php echo $lineItem['po_number']; ?></td>
<td><?php echo $lineItem['po_issued_date']; ?></td>
<td><?php echo $lineItem['line_item_name']; ?></td>
<td><?php echo $lineItem['service_period_start']; ?></td>
<td><?php echo $lineItem['service_period_end']; ?></td>
<td><?php echo $lineItem['request_for_delivery']; ?></td>
</tr>
<?php
}
?>
</table>
</div>
Edit: I've removed the session checks and updated the code sample. I've added session_start() before my <head> tag in index.php. I've verified that I can write to the session temp folder.
When i execute this code in my controller to update the invoices with the new PO number, I reach the model's function, but the session is gone.
//If form is posted, update line items with PO number and date.
if (isset($_POST['po_number'])) {
$this->invoice->update();
}
By the time I reach the session variable assignment, I have no session data:
public function update() {
$con = $this->_getLocalConn();
$invoices = $_SESSION['invoices'];
try {
$sqlUpdate = $con->prepare("UPDATE invoices
SET po_number = ?, po_issued_date = ?
WHERE invoice_id = ?");
foreach ($invoices as $record) {
$sqlUpdate->execute(array(
$_POST['po_number'],
getdate(),
$record['invoice_id']
));
}
} catch (PDOException $e) {
print $e->getMessage();
}
//get the PO number being used to update the records
//perform db update where po_number = input
//notify user of success and display updated records.
}
Each PHP file should start with session_start(); regardless of $_SESSION being set or not. This function will create a new session OR take up the existing one.
No $_SESSION is started to begin with when you check it with your first if. Therefore, it will always FAIL. You must call session_start() PRIOR to doing anything with a $_SESSION variable. Correct your code.
First page:
<?php
session_start();
/* Don't need this unless you really need the debugging
Previously you where assigning variables that did not
exist to the the $_SESSION variables. Not what you want
I imagine.
if (!isset($_SESSION)) {
var_dump($_SESSION);
}
*/
...
Include form:
<?php
session_start();
if (isset($_SESSION['session_id'])) {
print_r($_SESSION['session_id'] . "in editForm<br />");
} else {
echo 'No session ID set in editForm <br />';
}
...
Detail page:
<?php
session_start(); //Notice a pattern here??
if (isset($_SESSION['session_id'])) {
print_r($_SESSION['session_id'] . "<br />");
} else {
echo 'No session ID set invoice<br />';
}
?>
All of your code that needs the session information should start with session_start(). session_start() needs to happen before any headers or other output would be written.
Setup and teardown are then handled for you.
I do this:
session_start();
$s = &$_SESSION;
Then you can use read/write $s just like it was $_SESSION
If you are doing self referencing image downloads or other code that may end up wanting to execute in parallel, NOT starting a session or closing it as soon as possible with session_write_close() will give you a significant performance boost.
Without this, sessions essentially make your code run single threaded.
Edit: Saying single threaded was perhaps a bad choice of words.
Lets say you had a page with three iframes in it, each one loading a different (or the same) php script. If you are using sessions, the result would be the iframes loading one at a time instead of all at once. Each one would get a lock on the session and the others would wait at session_start() until the session was available again.