How do I stop my sql query from displaying the results twice? - php

How do I stop the results from getting displayed twice on the web page? What have I done wrong? Yes this is for a game but it helps me learn SQL and PHP. Please be genital it's my first time.
There is one database with two tables. The database is game_tittle the two tables are player_data and the second is character_data.
<?php
include('db_conn.php');
#for connecting to SQL
$sqlget = "SELECT player_data.PlayerName, Character_data.Duration, character_data.KillsZ, character_data.KillsB, character_data.KillsH, character_data.HeadshotsZ, character_data.Humanity, character_data.Generation, character_data.InstanceID FROM player_data, character_data";
$sqldata = mysqli_query($dbcon, $sqlget) or die('error getting data');
echo "<center><table>";
echo "<tr> <th>Player Name</th><th>play time</th><th>Total Kills</th><th>Bandit kills</th><th>Hero Kills</th><th>Head shots</th><th>Humanity</th><th>Alive count</th><th>Instance ID</tr>";
while ($row = mysqli_fetch_array($sqldata)) {
echo "<tr><td>";
echo $row['PlayerName'];
echo "</td><td>";
echo $row['Duration'];
echo "</td><td>";
echo $row['KillsZ'];
echo "</td><td>";
echo $row['KillsB'];
echo "</td><td>";
echo $row['KillsH'];
echo "</td><td>";
echo $row['HeadshotsZ'];
echo "</td><td>";
echo $row['Humanity'];
echo "</td><td>";
echo $row['Generation'];
echo "</td><td>";
echo $row['InstanceID'];
echo "</td></tr>";
echo "</center>";
}
echo "</table>";
#end of table
?>
Got it to work this way.
<?php
include('db_conn.php');
$sqlget = "SELECT player_data.PlayerUID, character_data.PlayerUID, player_data.PlayerName, character_data.HeadshotsZ, character_data.KillsZ, character_data.KillsH, character_data.KillsB, character_data.Alive, character_data.Generation, character_data.Humanity, character_data.InstanceID FROM player_data INNER JOIN character_data ON player_data.PlayerUID = character_data.PlayerUID";
$sqldata = mysqli_query($dbcon, $sqlget) or die('error getting data');
echo "<center><table>";
echo "<tr> <th>Player Name</th><th>Head shots</th><th>Total Kills</th><th>Hero kills</th><th>Bandit Kills</th><th>Live or dead</th><th>Lifes</th><th>Humanity</th><th>Instance ID</tr>";
while ($row = mysqli_fetch_assoc($sqldata)) {
echo "<tr><td>";
echo $row['PlayerName'];
echo "</td><td>";
echo $row['HeadshotsZ'];
echo "</td><td>";
echo $row['KillsZ'];
echo "</td><td>";
echo $row['KillsH'];
echo "</td><td>";
echo $row['KillsB'];
echo "</td><td>";
echo $row['Alive'];
echo "</td><td>";
echo $row['Generation'];
echo "</td><td>";
echo $row['Humanity'];
echo "</td><td>";
echo $row['InstanceID'];
echo "</td></tr>";
echo "</center>";
}
echo "</table>";
#end of table
?>

Bug in your SQL-query: You forgot to join the tables with a "where"-clause.

Related

PHP Button href not working

For a school project I am trying to create a shoppingcart with php only with MySQLi. For this I have a catalogue called index.php. In this is a table with the product and after every product there is a button which should add the item to the shoppingcart.
The only problem is that I cannot get the link working properly.
<?php
session_start();
include 'connect.php';
$qry = "select * from products";
$result = mysqli_query($connect, $qry);
echo "<table class='catalogue'>";
echo "<tr><th>ID</th><th>Code</th><th>Name</th><th>Description</th><th>Image</th></th><th>Price</th><th>Buy</th></tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['product_code'];
echo "</td><td>";
echo $row['product_name'];
echo "</td><td>";
echo $row['product_desc'];
echo "</td><td>";
echo $row['product_img_name'];
echo "</td><td>";
echo $row['price'];
echo "</td><td>";
echo "<input type='submit' value='Add' href='cart.php?id=['id']'/>";
echo "</td></tr>";
}
echo "</table>";
?>
The cart.php looks like this.
<?php
session_start();
require 'connect.php';
require 'item.php';
$result = mysqli_query($connect, 'select * from products where id='.$_GET['id']);
$product = mysqli_fetch_object($result);
if(isset($_GET['id'])){
$item = new Item();
$item->id = $product->id;
$item->name = $product->product_name;
$item->price = $product->price;
$item->quantity = 1;
$_SESSION['cart'][] = $item;
}
echo "<table class='cart'>";
echo "<tr><th>ID</th><th>Name</th><th>Price</th><th>Quantity</th><th>Sub Total</th></tr>";
$cart = unserialize(serialize($_SESSION['cart']));
for($i=0; $i<count($cart); $i++){
echo "<tr><td>";
echo $cart[$i]->id;
echo "</td><td>";
echo $cart[$i]->product_name;
echo "</td><td>";
echo $cart[$i]->price;
echo "</td><td>";
echo $cart[$i]->quantity;
echo "</td><td>";
echo $cart[$i]->price * $cart[$i]->quantity;
echo "</td></tr>";
}
?>
Please forgive any other mistakes you might see, I am rather new to PHP.
Buttons don't have hrefs, anchors(<a>) do, so using an anchor it would be
echo "<a href='cart.php?id=$row[id]'/>Add</a>";
you could always style it like a button if you want it to look like one.

Skip row with particular id

I want to skip one record from mysql using php when that record match with a variable value.
The $row['uni_id'] is id in a row of my database table and $_SESSION['uid'] stores a id in session. When the ids match, that record should be skipped. But it's not working. Do you see the issue?
while($row = mysql_fetch_array($res,MYSQL_ASSOC )){
if ($row['uni_id'] == $_SESSION['uid']){
continue;
}
$_SESSION['new_img']=$row['pic_add'];enter code here
$image= $row['pic_add'];
echo "<tr><td>";
echo "{$row['fname']} {$row['lname']}";
echo "</td><td>";
echo $row['street'];
echo "</td><td>";
echo $row['city'];
echo "</td><td>";
}
mysql_free_result($res);
Can you try the below code ?
<?php
while($row = mysql_fetch_array($res,MYSQL_ASSOC )){
if ($row['uni_id'] != $_SESSION['uid']){
$_SESSION['new_img'] = $row['pic_add'];
$image= $row['pic_add'];
echo "<tr><td>";
echo "{$row['fname']} {$row['lname']}";
echo "</td><td>";
echo $row['street'];
echo "</td><td>";
echo $row['city'];
echo "</td><td>";
}
}
mysql_free_result($res);
?>
I think you are using condition in a wrong manner.
Below code might help you.
while($row = mysql_fetch_array($res,MYSQL_ASSOC )){
if ($row['uni_id'] == $_SESSION['uid']){
// do nothing
}
else{
$_SESSION['new_img']=$row['pic_add'];
$image= $row['pic_add'];
echo "<tr><td>";
echo "{$row['fname']} {$row['lname']}";
echo "</td><td>";
echo $row['street'];
echo "</td><td>";
echo $row['city'];
echo "</td><td>";
}
}
mysql_free_result($res);
You should adjust your MySQL query before you start processing the result in PHP.
The solution will depend on your query. If your query is something like this:
select * from table
You should update it to:
select * from table WHERE uid <> "$_SESSION['uid']"
If you cannot control the MySQL query, then you can change your PHP code:
while($row = mysql_fetch_array($res,MYSQL_ASSOC )){
if ($row['uni_id'] != $_SESSION['uid']){
$_SESSION['new_img']=$row['pic_add'];enter code here
$image= $row['pic_add'];
echo "<tr><td>";
echo "{$row['fname']} {$row['lname']}";
echo "</td><td>";
echo $row['street'];
echo "</td><td>";
echo $row['city'];
echo "</td><td>";
}
}
mysql_free_result($res);
Try this
while($row = mysql_fetch_array($res,MYSQL_ASSOC )){
if ($row['uni_id'] != $_SESSION['uid']){
$_SESSION['new_img']=$row['pic_add'];
$image= $row['pic_add'];
echo "<tr><td>";
echo "{$row['fname']} {$row['lname']}";
echo "</td><td>";
echo $row['street'];
echo "</td><td>";
echo $row['city'];
echo "</td><td>";
}
}
mysql_free_result($res);
while($row = mysql_fetch_array($res,MYSQL_ASSOC )){
if ($row['uni_id'] !== $_SESSION['uid']){
$_SESSION['new_img']=$row['pic_add'];
$image= $row['pic_add'];
echo "<tr><td>";
echo "{$row['fname']} {$row['lname']}";
echo "</td><td>";
echo $row['street'];
echo "</td><td>";
echo $row['city'];
echo "</td><td>";
}
}
mysql_free_result($res);
NOTE: Do not use mysql. Its deprecated. Use PDO.
Try this code fragment
while($row = mysql_fetch_array($res))
{
$t='';
if($row['uni_id'] != $_SESSION['uid'])
{
$_SESSION['new_img']=$row['pic_add'];//enter code here
$image= $row['pic_add'];
$t.="<tr>";
$t.="<td>{$row['fname']} {$row['lname']}</td>";
$t.="<td>".$row['street']."</td>";
$t.="<td>".$row['city']."</td>";
$t.="</tr>";
}
echo $t;
}
mysql_free_result($res);

Use data from mysql in HTML row

I have a web page where I am pulling data from a mysql database and I want to be able to check a checkbox and change information within the table. The way I am displaying the information is as follows:
<?php
$get_tickets = mysql_query("SELECT * FROM assignment
JOIN technician
ON assignment.TechID = technician.TechID
JOIN employee
ON assignment.EID = employee.EID
JOIN ticket
ON assignment.TickID = ticket.TickID
WHERE assignment.TechID = '$current_id'");
echo "<table border='1' width=\"100%\" style=\"margin: 0px;\">";
echo "<tr><td>Ticket Number</td>";
echo "<td>First Name</td>";
echo "<td>Last Name</td>";
echo "<td>Phone</td>";
echo "<td>Device</td>";
echo "<td>Location</td>";
echo "<td>Problem</td>";
echo "<td>Time Stamp</td>";
echo "<td>Completed</td>";
echo '<td><form><input type="checkbox"/></form></td></tr>';
while($row2 = mysql_fetch_array($get_tickets))
{
$count = 1;
$checkbox = "checkbox" . $count;
echo "<tr><td>";
echo $row2["TickID"];
echo "</td><td>";
echo $row2["fname"];
echo "</td><td>";
echo $row2["lname"];
echo "</td><td>";
echo $row2["phone"];
echo "</td><td>";
echo $row2["device"];
echo "</td><td>";
echo $row2["location"];
echo "</td><td>";
echo $row2["problem"];
echo "</td><td>";
echo $row2["time_date"];
echo "</td><td>";
echo $row2["completed"];
echo "</td><td>";
echo '<form><input type="checkbox" name=' . $row2["TickID"] . '></form>';
echo "</td></tr>";
}
?>
There may be better ways to ouput the information, but I just want to be able to click the checkbox and take the data from the row associated with the checkbox and change part of it. Any help is greatly appreciated.
try to change this:
echo '<form><input type="checkbox" name=' . $row2["TickID"] . '></form>';
for this:
echo ''.$row2["fname"].'';
on the file process.php you will get the variable like this
<?php
echo $_GET['id'];
?>

Show the Oracle Datetime format in PHP

So, I'm new to my company and I have the code that developped by our past developer and I have to modify a bit. Here's the code:
<html>
<head>
<title> PAYMENT LIST </title>
<script language="javascript" type="text/javascript">
function urlorder(a)
{
var urlorder="order.php?on="+a;
var prmpt=window.open(urlorder,"test","left=200,top=150,scrollbars=yes,resizable=no,width=640,height=480");
}
function printorder(a,b)
{
var urlorder="printorder.php?on="+a+"&nu=2&tgl="+b;
//var prmpt=window.open(urlorder,"test","left=200,top=150,scrollbars=yes,resizable=no,width=640,height=480");
window.location = urlorder;
//alert (urlorder);
}
</script>
</head>
<body>
<?
//include "ceksession.php";
include 'connect.php';
//ambil tgl val
$q = "select tgl_val from para_info_kntr";
$s = OCIParse($c,$q);
OCIBindByName($s,":bind1",$ltid);
OCIExecute($s,OCI_DEFAULT);
while (OCIFetch($s))
{
$tgl=ociresult($s,"TGL_VAL");
echo $tgl."<br>";
}
$tgl= date('d-m-Y', strtotime($tgl));
$tgl= date('d-m-Y');
//echo $tgl."<br>";
$tgl = $_GET["tgl"];
echo "<div align=\"right\">";
echo "<input type=\"button\" onclick=\"window.location='month.php'\") value=\"HOME\">";
echo "</div>";
echo "<div align=\"center\">";
echo "<table border=\"1\">";
echo "<tr><td colspan=\"44\">";
echo "<div align=\"center\">PAYMENT LIST</div>";
echo "</td></tr>";
$q = "Select * from payment_final where bt_number=".$bt_number." and to_char(tgl_val,'DD-MM-YYYY')='".$tgl."' order by bill_no";
//echo $q;
$s2=OCIParse($c_slave,$q);
OCIBindByName($s2,":bind1",$ordernumber);
OCIExecute($s2,OCI_DEFAULT);
echo "<tr><td>";
echo "PAYMENT_NUMBER";
echo "</td><td>";
echo "TABLE_ID";
echo "</td><td>";
echo "ORDER_NUMBER";
echo "</td><td>";
echo "TGL_VAL";
echo "</td><td>";
echo "PAY_DATE";
echo "</td><td>";
echo "AMOUNT";
echo "</td><td>";
echo "DISCOUNT_AMOUNT";
echo "</td><td>";
echo "SERVICES";
echo "</td><td>";
echo "TAX";
echo "</td><td>";
echo "TOTAL_AMOUNT";
echo "</td><td>";
echo "BILL_NO";
echo "</td><td>";
echo "PRINT";
echo "</td></tr>";
while (OCIFetch($s2))
{
$V_GUEST_FOLIO = oci_result($s2,'GUEST_FOLIO');
$V_PAYMENT_NUMBER = oci_result($s2,'PAYMENT_NUMBER');
$V_TABLE_ID = oci_result($s2,'TABLE_ID');
$V_ORDER_NUMBER = oci_result($s2,'ORDER_NUMBER');
$V_TGL_VAL = oci_result($s2,'TGL_VAL');
$V_PAY_DATE = oci_result($s2,'PAY_DATE');
$V_AMOUNT = oci_result($s2,'AMOUNT');
$V_DISCOUNT_MK = oci_result($s2,'DISCOUNT_MK');
$V_DISCOUNT_MN = oci_result($s2,'DISCOUNT_MN');
$V_DISCOUNT_KM = oci_result($s2,'DISCOUNT_KM');
$V_DISCOUNT_SN = oci_result($s2,'DISCOUNT_SN');
$V_DISCOUNT_EA = oci_result($s2,'DISCOUNT_EA');
$V_DISCOUNT_EB = oci_result($s2,'DISCOUNT_EB');
$V_DISCOUNT_AMOUNT = oci_result($s2,'DISCOUNT_AMOUNT');
$V_SERVICES = oci_result($s2,'SERVICES');
$V_TAX = oci_result($s2,'TAX');
$V_TOTAL_AMOUNT = oci_result($s2,'TOTAL_AMOUNT');
$V_CASH_PAID = oci_result($s2,'CASH_PAID');
$V_CASHIER_NUMBER = oci_result($s2,'CASHIER_NUMBER');
$V_WAITER_NUMBER = oci_result($s2,'WAITER_NUMBER');
$V_CUSTOMER_NUMBER = oci_result($s2,'CUSTOMER_NUMBER');
$V_PRINT_DATE = oci_result($s2,'PRINT_DATE');
$V_DP = oci_result($s2,'DP');
$V_CARD_PAID1 = oci_result($s2,'CARD_PAID1');
$V_CARD_PAID2 = oci_result($s2,'CARD_PAID2');
$V_CARD_TYPE1 = oci_result($s2,'CARD_TYPE1');
$V_CARD_NUMBER1 = oci_result($s2,'CARD_NUMBER1');
$V_CARD_NAME_HOLDER1 = oci_result($s2,'CARD_NAME_HOLDER1');
$V_CARD_TYPE2 = oci_result($s2,'CARD_TYPE2');
$V_CARD_NUMBER2 = oci_result($s2,'CARD_NUMBER2');
$V_CARD_NAME_HOLDER2 = oci_result($s2,'CARD_NAME_HOLDER2');
$V_PAYMENT_METHOD = oci_result($s2,'PAYMENT_METHOD');
$V_AR_AMOUNT = oci_result($s2,'AR_AMOUNT');
$V_BT_NUMBER = oci_result($s2,'BT_NUMBER');
$V_FLAG_CETAK = oci_result($s2,'FLAG_CETAK');
$V_REC_ID = oci_result($s2,'REC_ID');
$V_FLAG_CTR = oci_result($s2,'FLAG_CTR');
$V_CHECK_NO = oci_result($s2,'CHECK_NO');
$V_VOUCHER = oci_result($s2,'VOUCHER');
$V_VOUCHER_NO = oci_result($s2,'VOUCHER_NO');
$V_CPL_AMOUNT = oci_result($s2,'CPL_AMOUNT');
$V_BILL_NO = oci_result($s2,'BILL_NO');
$V_AR_NAME = oci_result($s2,'AR_NAME');
$V_ENT_AMOUNT = oci_result($s2,'ENT_AMOUNT');
echo "</td><td>";
/*
<input type=\"button\" value=\"".$V_PAYMENT_NUMBER."\" onclick=\"urlorder(".$V_ORDER_NUMBER.")\")\" style=\"height: 25px; width: 70px;\">
*/
echo "<a href=\"order.php?on=".$V_ORDER_NUMBER."\">";
echo $V_PAYMENT_NUMBER;
echo "</a>";
echo "</td><td>";
echo $V_TABLE_ID;
echo "</td><td>";
echo $V_ORDER_NUMBER;
echo "</td><td>";
echo $V_TGL_VAL;
echo "</td><td>";
echo $V_PAY_DATE;
echo "</td><td>";
echo $V_AMOUNT;
echo "</td><td>";
echo $V_DISCOUNT_AMOUNT;
echo "</td><td>";
echo $V_SERVICES;
echo "</td><td>";
echo $V_TAX;
echo "</td><td>";
echo $V_TOTAL_AMOUNT;
echo "</td><td>";
echo $V_BILL_NO;
echo "</td><td>";
echo "<input type=\"button\" onclick=\"printorder(".$V_ORDER_NUMBER.",'".$tgl."')\") value=\"PRINT\">";
echo "</td></tr>";
}
?>
</body>
</html>
The result is shown here:
in my DB Oracle, the field PAY_DATE's format like this : 02/01/2015 08:35:58
But the result now it's just showing the Date only. What I'm trying to do is I want to show the date and time like this 02/01/2015 08:35:58 in the PAY_DATE column. Can you guys help me with this?
SELECT TO_CHAR(PAY_DATE, 'DD/MM/YYYY:HH24:MI:SS') FROM "payment_final" where TO_CHAR(PAY_DATE, 'DD/MM/YYYY:HH24:MI:SS') = = '02/01/2015 08:35:58'
OR
SELECT TO_CHAR(PAY_DATE, 'DD/MM/YYYY:HH24:MI:SS') FROM "payment_final"
WHERE PAY_DATE = to_timestamp('02/01/2015 08:35:58','MM/DD/YYYY HH24:MI:SS')
see Oracle Dates and Times format
you can use to_char function in query to get the data from table.
e.g.
Select to_char("PAY_DATE","YYYY-MM-DD HH:MI:SS") as "PAY_DATE" FROM table
You can use PHP date function for the respective as
$V_PAY_DATE; = date('d/m/Y H:i:s',strtotime($V_PAY_DATE));
echo $V_PAY_DATE;

Displaying information from a table then showing related information from another table

Hello I have 2 tables.
One is a table (nameinfo) that consists of contact information (account, name, email, address, city, state, zip, tags)
Table Two is an events tables (events) (email, event_type, event_name, date, keywords)
Here is what I am trying to do...
I would like to show a list of all the events that the person went to based on the matching email address. I would like to return the results like this...
nameinfo person 1
events person 1 went to
nameinfo person 2
events person 2 went to
nameinfo person 3
events person 3 went to
$query = mysql_query("SELECT * FROM nameinfo WHERE account='$a'");
while($row = mysql_fetch_array($query))
{
$i++;
echo "<div>";
echo "<table><tr style='font-weight:bold;color:green;font-size:9pt;text-align:left;'><td style='text-align:left;'
>";
echo "<button class='toggles' id='".$i."'/>"; echo $row['fullname']."</button>";
echo "</td>";
echo "<td >";
echo $row['target'];
echo "</td><td >";
echo $row['lname'];
echo "</td>";
echo "<td >";
echo $row['fname'];
echo "</td><td >";
echo $row['title'];
echo "</td>";
echo "<td >";
echo $row['company'];
echo "</td><td >";
echo $row['address'];
echo "</td>";
echo "<td >";
echo $row['address2'];
echo "</td><td>";
echo $row['city'];
echo "</td>";
echo "<td>";
echo $row['state'];
echo "</td><td>";
echo $row['zip'];
echo "</td>";
echo "<td>";
echo $row['email'];
echo "</td><td>";
echo $row['officenum'];
echo "</td>";
echo "<td>";
echo $row['cellnum'];
echo "</td>";
echo "<td>";
echo $row['date'];
echo "</td><td>";
echo $row['rep'];
echo "</td></tr></table></div>";
echo "<div style='width:100%;background:#000;color:#fff;display:none;' id='show-".$i."'>";
**echo THIS IS WHERE I WOULD LIKE TO PUT THE EVENT INFORMATION BUT HAVE NO IDEA HOW TO DO IT.;**
echo "</div>";
}
<script>
$("button.toggles").click(function() {
var value = $(this).attr('id');
$("#show-" + value).toggle();
});
</script>
Thank you for any assistance.
Run another query inside the loop. Here I am calling a function to do the job:
$i=0;
$link = ""\\your database connection
$query = mysql_query("SELECT * FROM nameinfo WHERE account='$a'");
while($row = mysql_fetch_array($query)){
$i++;
echo "<div>";
echo "<table><tr style='font-weight:bold;color:green;font-size:9pt;text-align:left;'><td style='text-align:left;'>";
echo "<button class='toggles' id='".$i."'/>"; echo $row['fullname']."</button>";
echo "</td>";
echo "<td >";
echo $row['target'];
echo "</td><td >";
echo $row['lname'];
echo "</td>";
echo "<td >";
echo $row['fname'];
echo "</td><td >";
echo $row['title'];
echo "</td>";
echo "<td >";
echo $row['company'];
echo "</td><td >";
echo $row['address'];
echo "</td>";
echo "<td >";
echo $row['address2'];
echo "</td><td>";
echo $row['city'];
echo "</td>";
echo "<td>";
echo $row['state'];
echo "</td><td>";
echo $row['zip'];
echo "</td>";
echo "<td>";
echo $row['email'];
echo "</td><td>";
echo $row['officenum'];
echo "</td>";
echo "<td>";
echo $row['cellnum'];
echo "</td>";
echo "<td>";
echo $row['date'];
echo "</td><td>";
echo $row['rep'];
echo "</td></tr></table></div>";
echo "<div style='width:100%;background:#000;color:#fff;display:none;' id='show-".$i."'>";
getEvent($row['email']);//Pass the email to the function here
echo "</div>";
}
function getEvent($email){
global $link;
$evant = "";
$query = mysql_query("SELECT * FROM events WHERE email='$email'", $link);
while($row = mysql_fetch_array($query)){
$event = $event . $row["event_name"] . "<br />";
//Format this however you wish to format
}
return $event;
}
<script>
$("button.toggles").click(function() {
var value = $(this).attr('id');
$("#show-" + value).toggle();
});
</script>

Categories