View cart issue - php

All the other parts of my application are working but my users view cart is not working I do not know whether if it is about the indexing
I have tried using outer php
<!DOCTYPE html>
<html>
<head>
<title>View table</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs /popper.js/1.14.7/umd/popper.min.js"></script>
</head>
<body>
<table class="table">
<thead>
<tr>
<th scope="col">Item</th>
<th scope="col">brand</th>
<th scope="col">flavour/type</th>
<th scope="col">quantity</th>
<th scope="col">Number of units</th>
<th scope="col">Number of packets</th>
<th scope="col">price</th>
<th scope="col">Cost</th>
<th scope="col">Picture</th>
<th scope="col">D</th>
</tr>
</thead>
<tbody>
<?php
require_once("config.php");
error_reporting(0);
$email_address=$_SESSION['email_address'];
$sql="SELECT product_name ,brand,flavour_type,quantity,number_of_units,price ,units,image_path
FROM gokcen.product NATURAL JOIN gokcen.cart
WHERE cart.email_address=:email_addres";
$stmt=$db->prepare($sql);
$stmt->bindParam(':email_address',$email_address);
$stmt->execute();
$result=$stmt->fetchAll();
foreach($result as $product)
{
?> <tr>
<td><?php echo $product['product_name'];?></td>
<td><?php echo $product['brand'];?></td>
<td><?php echo $product['flavour_type'];?></td>
<td><?php echo $product['quantity'];?></td>
<td><?php echo $product['number_of_units'];?></td>
<td><?php echo $product['units'];?></td>
<td><?php echo $product['price'];?></td>
<td><?php echo $product['price']* $prouct['units'];?></td>
<td> <img src="pics/<?php echo $product['image_path'];?>" width="80" height="80"/></td>
<td><a href="deletefromcart.php?item=<?echo product['product_name'];?>"> delete <a></td>
</tr>
<?php }?>
</tbody>
</table>
<a href="#" class="btn btn-primary" >Buy</a>
</body>
</html>
There are no results It does not give any errors it shows the header part only and Im not sure about the indexing.

there are several typos in your code.
When I am right there should be another "s" at the end? cart.email_address=:email_addres
There is a "d" missing at line <?php echo $product['price']* $prouct['units'];?>
You also missed the "$" sign at this point as well as the "php" after the question mark: <?echo product['product_name'];?>
You should set display_errors to On in your php.ini and comment out the error_reporting(0); for debugging purposes :)
Greetings

You have a typo email_addres email_address.
WHERE cart.email_address=:email_addres";
$stmt->bindParam(':email_address',$email_address);

Thank you guys you are the best you rock my coding life It was the typos and also that
I had not put a
session_start()
To initialize the value of $email_address on
$email_address=$_SESSION['email_address'];
All the best!

Related

How do i display data from API using the ID?

I can now display the correct ID for each row of data in my URL. But now i am looking to display it in another screen.
I am struggling to figure out how to pull the data such as "player" or "Team" from the ID initially retrieved.
I am looking to display this data in a table in the GameInfo page.
Any help is appreciated. Thanks
For example:
My Get for match results
==========================
public function getMatchInfo() {
$url = $this->buildUrl('scores/events.json?
key=Ka88B6jZrxO8dDQt&secret=L233yXpNCWQDZyxJIGSkNbjeI8nWLdqw&id=1"');
return $this->makeRequest($url);
}
Here i have to specify the ID which is annoying because the IDs change from game to game.
Is there a way i can edit to pull the ID from my index page? From the index page it displays the current "Live Games", and i click a link which opens the "Game Info" page, which is where i want to pull data from the API.
Index.php
======================
<?php
include 'config.php';
include 'functions.php';
include 'myapp.php';
$Api = new LivescoreApi();
$data = $Api->getLiveScores();
$timezone = 'Europe/Istanbul';
?>
<html>
<head>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-3">
</div>
<div class="col-md-6">
<table class="table table-bordered">
<tr class="table-info">
<th>KO</th>
<th>Time</th>
<th>Home</th>
<th>Score</th>
<th>Away</th>
</tr>
<?php foreach ($data['data']['match'] as $_match) { ?>
<tr>
<td><?= convert($_match['scheduled'], $timezone) ?></td>
<td><?= $_match['time'] ?></td>
<td style="text-align: right;"><?= $_match['home_name'] ?></td>
<td style="text-align: center;"><?= $_match['score'] ?></td>
<td><?= $_match['away_name'] ?></td>
<td style="text-align: right;"><a href="gameInfo.php"> Game
information </a><?= $_match['id'] ?></td>
</tr>
<?php } ?>
</table>
</div>
<div class="col-md-3">
</div>
</div>
</div>
</body>
</html>
GameInfo.php
=================
<tr class="table-info">
<h2 style="text-align: center;"> Game Info </h2>
<th>Player Name</th>
<th>Event</th>
<th>Time</th>
</tr>
<?php foreach ($data['data']['event'] as
$_event){ ?>
<tr>
<td style="text-align: center;"><?=
$_event['player'] ?></td>
<td style="text-align: center;"><?=
$_event['event'] ?></td>
<td style="text-align: center;"><?=
$_event['time'] ?></td>
Quite possible that I don't understand your question. Create a form so that the id is sent in the $_GET["id"] variable?
I think you just want to pass the ID you already have to the function?
public function getMatchInfo() {
if(isset($_GET['id']){
$id = $_GET['id']
} //this check entirely optional, but I'd do it
else{
$id = 1; //or some other default you want, or error handling here
}
$url = $this->buildUrl('scores/events.json?
key=Ka88B6jZrxO8dDQt&secret=L233yXpNCWQDZyxJIGSkNbjeI8nWLdqw&id='.$id);
return $this->makeRequest($url);
}
Index.php
<?php
include 'config.php';
include 'functions.php';
include 'myapp.php';
$Api = new LivescoreApi();
$data = $Api->getLiveScores();
$timezone = 'Europe/Istanbul';
?>
<html>
<head>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-3">
</div>
<div class="col-md-6">
<table class="table table-bordered">
<tr class="table-info">
<th>KO</th>
<th>Time</th>
<th>Home</th>
<th>Score</th>
<th>Away</th>
</tr>
<?php foreach ($data['data']['match'] as $_match) { ?>
<tr>
<td><?= convert($_match['scheduled'], $timezone) ?></td>
<td><?= $_match['time'] ?></td>
<td style="text-align: right;"><?= $_match['home_name'] ?></td>
<td style="text-align: center;"><?= $_match['score'] ?></td>
<td><?= $_match['away_name'] ?></td>
<td style="text-align: right;"><a href="gameInfo.php?id=<?= $_match['id'] ?>"> Game
information </a><?= $_match['id'] ?></td>
</tr>
<?php } ?>
</table>
</div>
<div class="col-md-3">
</div>
</div>
</div>
</body>
</html>
So i figured out the answer to my updated question.
I didnt add the "isSet()" check originally.
I did that now and I also was missing braces.
The joys of not using an IDE for code I guess.

Why this is Used <?php }?>

From a database we are getting data, but my question is that after tbody starting tag and before tbody ending tag, why we are ending php in this way using <?php }?>
Why is it used in these lines?
<div class="container">
<div class="table-responsive">
<table class="table table-striped" >
<thead>
<tr>
<th scope="col">CNIC</th>
<th scope="col" >Name</th>
<th scope="col" >DOB</th>
<th scope="col" >Address</th>
<th scope="col" >City</th>
<th scope="col" >Degree Program</th>
<th scope="col" >Gender</th>
<th scope="col" >Email</th>
<th scope="col" >Mobile</th>
<th scope="col" ></th>
<th scope="col" ></th>
</tr>
</thead>
<tbody>
<?php while($student = mysqli_fetch_assoc($resultSet)){?>
<tr>
<td scope="row" ><?php echo $student['cnic']; ?></td>
<td><?php echo $student['fname'] . " ". $student['lname']; ?></td>
<td><?php echo $student['dateofbirth']; ?></td>
<td><?php echo $student['address']; ?></td>
<td><?php echo $student['city']; ?></td>
<td><?php echo $student['degree']; ?></td>
<td><?php echo $student['gender']; ?></td>
<td><?php echo $student['email']; ?></td>
<td><?php echo $student['mobile']; ?></td>
<td> <a class="btn btn-primary" href=<?php echo "update_student.php?u_id=".$student['u_id']; ?> >Update</a> </td>
<td> <a class="btn btn-primary" href=<?php echo "delete_student.php?u_id=".$student['u_id']; ?> >Delete</a> </td>
</tr>
<?php }?>
</tbody>
</table>
</div>
Here,
like your code snippet, we can decide what is to be displayed in the DOM (html page).in your code the whole contents in
<tr>...</tr>
will print on the html page only till the while condition satisfies.if the while loop runs for ten times there will be ten rows in
<tbody>...</tbody>
here the while loop will continue till fetching the last record from the specified table.
This:
<?php }?>
Is actually closing your while loop that starts at the begining:
<?php while($student = mysqli_fetch_assoc($resultSet)){?>.
If that would not be present, the code would throw you an error.
Loops in PHP (should) start and end with curly brackets. So at the begining you have something like:
for($i; $i< 10, $i++) {
or
foreach($a as $b) {
or
while($someCondition){
And at the end always a closing curly bracket:
}
BR
With this <?php ..... ?> inside html code, you are (probably) showing html code inside php file. So basically you have (for example) a HTML template in php file, hence the php opening/closing tags. So while you are tehnically in a PHP script, you are actually showing something like:
<!DOCTYPE html>
<html>
<head>
<title><?php echo "Some title" ?></title>
</head>
<body>
<h1><?php echo "Hello World" ?></h1>
</body>
</html>
If you can, I'd suggest to use a templating engine for this, like twig.
So in your case, you are fetching result from DB, looping over it and shwoing it in HTML table.
BR
PHP has a cool way of deciding to show content. Instead of echo'ing HTML based on a variable, you can use the PHP <?php if (...): ?> html here <?php endif; ?> and the HTML inside will show only if that if condition is met. It's the same thing here, except with a while instead of an if
A PHP while loop works like this:
while($x === true){
//execute some code
}
This will execute the code inside the while loop an infinite number of times, or until the $x variable is changed (within the loop) to equal true. The opening and closing brackets, { and } are what determine where the while loop starts and ends. In your code, PHP is printing out all of those <tr> and <td> tags until mysqli_fetch_assoc($resultSet) no longer returns a result. The line <?php }?> just tells the program, "the loop stops here, go back to the start"

my records are not displaying

in my project i try to display data from database and this is my code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="prac.css">
<title>PHP Practical</title>
</head>
<body>
<div class="main">
<table class="table">
<tr>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Employee Contact</th>
<th>Employee Salary</th>
<th>Employee Phone</th>
<th>Employee City</th>
<th>Join Date</th>
<th>Action</th>
</tr>
<?php
require 'config.php';
$query = mysql_query("SELECT * FROM emp");
while ($row = mysql_fetch_array($query)) {
?>
<tr>
<td><?php echo $row['emp_id']; ?></td>
<td><?php echo $row['emp_name']; ?></td>
<td><?php echo $row['emp_contact']; ?></td>
<td><?php echo $row['emp_salary']; ?></td>
<td><?php echo $row['emp_phone']; ?></td>
<td><?php echo $row['emp_city']; ?></td>
<td><?php echo $row['join_date']; ?></td>
<td>Update | Delete</td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>
i checked my config.php file and it is working fine and in database i have 6 records but it is not displaying any. i try also run query to database and it is working fine in database but not here. please help me.
**Use this**
$query = mysql_query("SELECT * FROM emp") or die(mysql_error());
to display mysql error
The mysql_ extension is deprecated. There is no point in using it.
Switch to mysqli or PDO
The issue you are currently facing is not caused because of mysql_ being deprecated but there's no worth in investigating it. It's not used anymore

Echo HTML with PHP in the middle

I am new to PHP and am running into a small problem with this code. I am trying to make a layout for a page that uses more PHP to fill in the blanks.
When I view the source <? include $navbar ?> is commented out but <?=$pagetitle?> works, why is that?
For reference:
$navbar = "navbar.php";
and navbar.php:
<?php echo "Select Car Change Profile"; ?>
Layout.php:
<?php
echo "
<html>
<head>
<title>Race Data. <?=$pagetitle?></title>
</head>
<body>
<div id='page'>
<table border='1'>
<tbody>
<tr>
<td colspan='3'>Banner goes here.<?=$pagetitle?></td>
</tr>
<tr>
<td rowspan='2'>Left menu</td>
<td colspan='2'><? include $navbar; ?></td>
</tr>
<tr>
<td>Content</td>
<td>Right menu</td>
</tr>
<tr>
<td colspan='3'>Footer</td>
</tr>
</tbody>
</table>
</div>
</html>
";
?>
I'm sure knowing this will help amny future problems I run into.
Also, what are the diferences in using <? ?> vs <?php ?>?
In php, you always need to use <?php /*code*/ ?>
The shorthand version to echo something is <?= /*string*/ ?>, but to run code, such as an include you would need to start with <?php. In your example, this would be:
<tr>
<td rowspan='2'>Left menu</td>
<td colspan='2'><?php include $navbar; ?></td>
</tr>
Correction
I only just noticed that you placed the PHP tags inside another set of PHP tags. You're kind of doing it the hard way. In a PHP file, anything is regarded as an echo, except for content inside <?php ?> tags. So this should work perfectly for you:
<html>
<head>
<title>Race Data. <?=$pagetitle?></title>
</head>
<body>
<div id='page'>
<table border='1'>
<tbody>
<tr>
<td colspan='3'>Banner goes here.<?=$pagetitle?></td>
</tr>
<tr>
<td rowspan='2'>Left menu</td>
<td colspan='2'><?php include $navbar; ?></td>
</tr>
<tr>
<td>Content</td>
<td>Right menu</td>
</tr>
<tr>
<td colspan='3'>Footer</td>
</tr>
</tbody>
</table>
</div>
</html>
The difference is that I didn't put <?php ?> tags around the whole thing.
Try
short_open_tag=On;
in php.ini
And restart your Apache server.

PHP & Oracle database edit/update data

I'm using PHP in combination with an Oracle database. What I want is the following: On the first form I want to select a name out of a table in the database and when you push the button open I want the users to see a html form where the fields are filled in with the information from the person you've selected on the first screen. You can edit this information and when you push the update button the table has to be updated. I don't know who to do this whole process in PHP in combination with Oracle. Can someone please help me? This is an important part of a project I'm doing and I can't find any information anywhere!
I really hope someone can help me.
PHP & Oracle database edit/update data.
geting error Undefined variable: objResult on <<<-----here
<?
$objConnect = oci_connect("myuser", "mypassword", "TCDB");
$strSQL = "SELECT * FROM CUSTOMER";
$objParse = oci_parse($objConnect, $strSQL);
oci_execute($objParse, OCI_DEFAULT);
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<th width="71"> <div align="center">Used </div></th>
<th width="30"> <div align="center">Edit </div></th>
</tr>
<?
while ($objResult = oci_fetch_array($objParse, OCI_BOTH))
{
?>
<tr>
<td><div align="center"><?= $objResult["CUSTOMERID"]; ?></div></td> <<---here
<td><?= $objResult["NAME"]; ?></td> <<---here
<td><?= $objResult["EMAIL"]; ?></td> <<---here
<td><div align="center"><?= $objResult["COUNTRYCODE"]; ?></div></td>
<td align="right"><?= $objResult["BUDGET"]; ?></td> <<---here
<td align="right"><?= $objResult["USED"]; ?></td>
<td align="center">Edit</td>
</tr>
<?
}
?>
</table>
<?
oci_close($objConnect);
?>
Your while loop will probably only work for the first line. Make sure you use { } for every loop, be it one line or many.
This error could mean that short_open_tag isn't enabled in your php.ini, and due to that PHP actually sees no code in your template. Only the <?= ... ?> tags are working, while the <? ... ?> are not.
Your options:
Set short_open_tag = On in your php.ini. Or,
use full <?php ... ?> tags instead
Also, the while loop isn't complete. Try:
<?
while ($objResult = oci_fetch_array($objParse, OCI_BOTH)):
?>
// ...
<?
endwhile;
?>

Categories