How to extract field of data from SQL - php

I am a hobbyist programmer and stuck with php and sql
I have a website where I hope to allow different users to list their domains and websites for sale. I have an SQL database with the fields: id, domain and price. Now I added a column of data where "namer" is the field name.
I am trying to extract the field "namer" so it appears on the website but it won't work.
The problem starts at // PROBLEM AREA in the code and after that div it's okay.
<div class="domain_list_item_main_linear" style="font-family: 'Open Sans', Arial; width:98%;">
<div style="width:33%;float:left;">
<a href="<?php /*?><?php echo ConfigPeer::GetValue('website_folder'); ?>
<?php echo $domain->getCleanName(); ?><?php */?>https://www.afternic.com/domain/<?php echo urlencode($domain); ?>" class="moLPdomain" target="_blank">
<?php echo html_entity_decode($domain->getDivTipNameLinear(), ENT_QUOTES); ?></a>
<?php if(!$domain->getIsSold()): ?>
</div>
<div class="moLPprice" style="width:22%;float:left;margin-left:0px;">
<?php
// PROBLEM AREA
$eventid = $_GET['id'];
$namer = $_GET['namer'];
$result = mysql_query("SELECT $namer FROM `domain` WHERE `id` = '$eventid' ");
$row = mysql_fetch_array($result);
echo $row[$namer];
?> </div>

The problem is where you are using $namer.
The section after SELECT should contain the name of a column which contains the data you are trying to extract. Looking at your code, $namer isn't the name of the column, but some data that you are trying to match.
So the structure of the SQL should be like this:
SELECT column_name FROM table_name WHERE a_column_name = a_val;
I suggest you take a look at SQL SELECT statements, w3school's is a good place to start:
http://www.w3schools.com/php/php_mysql_select.asp
Edit:
You're SQL should look like this:
SELECT namer FROM domain WHERE id = $eventid;
If you want to get the 'namer' of the domain with that ID.
You're variable $namer must be storing any of the following words for your query to run:
id
domain
price

Related

How to Link PHP Sub Categories(Pages) Dynamically

I have There PHP File getting Data From MySQL Database called:
clubs.php
club.php
player.php
the clubs.php list all clubs from tbleclubs table and it looks like:
clubs.php
$database = new Database();
$res = $Db->query("SELECT * FROM tblclubs");
foreach ($res as $dataRow): ?>
<span><?php echo $dataRow['id']; ?></span>
<span><a hrer=""><?php echo $dataRow['name']; ?> </a></span>
<?php endforeach; ?>
and club.php listing all players in a club from tblclub table
club.php
$database = new Database();
$res = $Db->query("SELECT * FROM tblpclub");
foreach ($res as $dataRow): ?>
<span><?php echo $dataRow['id']; ?></span>
<span><a hrer=""><?php echo $dataRow['name']; ?> </a></span>
<?php endforeach; ?>
and finally the player.php is from tbleplayer:
player.php
$database = new Database();
$res = $Db->query("SELECT * FROM tblplayers");
foreach ($res as $dataRow): ?>
<span><?php echo $dataRow['id']; ?></span>
<span><a hrer=""><?php echo $dataRow['name']; ?> </a></span>
<?php endforeach; ?>
I have set up a Foreign Key(FK) for tblclub as clubs_id and for tblplayers as club_id.
Now, my question is, how can I dynamically navigate to each selected item in next page like:
clubs->club->player
Thanks
First of all you have some errors in your HTML (it should href and not hrer for links)
Secondly, using a select * type of query is bad form; you should generally be choosing specific keys from your table, and not just all of them. You will also need to be link your tables if you want this to work properly; it's difficult to tell what specific rows you have in your tables; some structure would be helpful.
That said you would need to structure your queries something like this. For example to get a list of players a specific club:
SELECT p.playerid, p.name,
FROM tblplayers p, tblclub c
WHERE p.club_id = c.club_id
this query will get a list of all players on a specific club.
As for linking you would could modify your links to be something like this. To link to a specific player in a specific club, this would be the link on club.php to player.php:
<?php echo $dataRow['name']; ?>
Then to get the info you could just get the playerid from the URL by using a GET variable, like this: $playerid = $_GET['playerid']
and then get your information from the query, to do what you like. You can use the data in any way you like from there.
This is just a start, but you should be able to extrapolate from this for the rest of your questions. Hope this helps.
Ok, you are now rendering static lists.
To make the navigation dynamic you would basically need 3 things:
create links with ids
fetch this id from the URL
and query the db using the WHERE statement to select that specific id
You have already started with links: <a hrer="">, but it's href.
clubs.php - would render the overview of all clubs with links to each club
Place this in your foreach to construct the links by attaching the ids.
foreach ($res as $dataRow) {
$id = $dataRow['id'];
$name = $dataRow['name'];
$link = 'club.php?id=' . $id;
echo '<span>' . $id . '</span>';
echo '<span>' . $name. '</span>';
}
Now you can click for instance club.php?id=2.
But club.php would need to handle the incoming id, right?
club.php - renders the list of players with links to each player
You basically repeat the pattern from above, but with a different anchor base, this time it's player.php. You should get a list with player.php?id=x links.
How to handle the id in each of the scripts?
The id is incoming via $_GET.
You can use var_dump($_GET['id']) to see the value. Use a variable, like so $id = $_GET['id'].
(Later, when everything works: do not forget to secure and escape the incoming data properly.)
Then use $id in your database query:
SELECT * FROM tblplayers WHERE player_id = ' . $id;

Sorting query results with column head

I have a typical SQL query that returns results like this:
$result_acc_man = "SELECT * FROM mgap_management WHERE account_manager_id = '" . $_SESSION['account_manager_id'] . "' ORDER BY mgap_sales_pres";
$stmt = $pdo->prepare($result_acc_man);
$stmt->execute();
while($row_acc_man = $stmt->fetch(PDO::FETCH_ASSOC))
{
$salespres = $row_acc_man['mgap_sales_pres'];
$regvp = $row_acc_man['mgap_regional_vp'];
$areasales = $row_acc_man['mgap_area_sales_manager']
?>
<p class="asminfo"><span>Your ASM: <?php echo $areasales;?></span></p>
<p class="asminfo"><span >Your Regional VP: <?php echo $regvp; ?></span></p>
<p class="asminfo"><span >Your Sales President: <?php echo $salespres; ?></span></p>
<?php
}
?>
here are the column headers
<div id="viewheadaccept">
<span class="namecustaccept1">ACCOUNT NAME </span>
<span class="custaccept">ACCOUNT TYPE</span>
<span class="recoverycustaccept">OPPORTUNITY SIZE</span>
</div>
I need to add the ability to click on the column names and sort the data. Is there an easier way to accomplish this other than creating multiple linked pages with different queries that contain the sort?
Thanks!
You could provide a parameter to replace mgap_sales_pres. So send a orderBy parameter via the url stirng.
You would need to escape the variable for security. Normally this wouldn't be recommended, but since prepared statements can't inject variables into the ORDER BY clause, it's the only option for now.
For example mysql_real_escape_char($_GET['orderBy'])
Or if you are really paranoid you could use a switch case statement to check for a validate column name.

View page based on a record

I'm new to PHP and pardon me for asking this very basic question. What I want to do is to display or view a page based on a specific record. For example, I have a home.php page which lists records of lessons. And when I click on a specific record, it will go a page named lesson.php . I have to view the relevant information/data from my dB of that specific lesson. I tried to use GET but I think it's not going to meet the requirement of my system.
This is what I've tried so far:
$qry1stQuarter = $conn->prepare("SELECT l.lesson_title FROM tbllessons as l
JOIN tblstudents as s
ON l.grade_level = s.grade_level
WHERE quarter_code = '1st'
AND s.grade_level=:grade_level");
$qry1stQuarter->execute(array(':grade_level' => $grade_level));
<div id="tabs-2">
<div id="accordion">
<h3><strong>Yunit 1</strong></h3>
<div>
<?php
for($i=0; $row = $qry1stQuarter->fetch(); $i++){
$lesson_title = $row['lesson_title'];
?>
<div id = "lessons">
<?php
echo "<a href = 'lesson_view.php'>$lesson_title </a>";?>
</div>
<?php
} // end of for loop
?>
</div> <!-- end of Yunit 1 -->
What is the best way to do this? Your help is pretty much appreciated. Thanks.
In your database, I assume you have an ID column. A typical way to do what you are asking is to use that ID as a GET parameter on a link, and then include that in your WHERE clause in your SQL statement.
Eg:
echo "<a href='lesson_view.php?id=$lesson_id'>$lesson_title</a>";?>
And then on your lesson_view.php page, your SQL has something like this:
SELECT * FROM tbllessons WHERE id = mysql_real_escape_string($_GET['id'])

I need to pull from two tables into a loop and match the data

Please forgive me if I'm going about this the wrong way I don't have much experience in PHP development.
I need to list some real estate listings on a page, which is easy enough, but where I'm having trouble is trying to match them with the proper image. The images are stored on my server and I have the file path saved in one table and all the property info stored in another table.
This is what I have right now and it kind of works.. except for it is showing each listing twice. Once with pic once with out.. I know there has to be a much better way then this..
<?php
$sql="SELECT * FROM $tbl_name, documents_main WHERE show_listing='1' and user_type='1'";
$result=mysql_query($sql);
// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
$payment = $rows['price'] * .09 / 12;
$path = $rows['path'];
$id = $rows['id'];
$note_id = $rows['note_id'];
?>
<div class="row clearfix">
<div style="width: 120px; float: left;">
<?php
if($note_id == $id){
echo "<img src='$path'>";
} else {
echo "";
}
?>
</div>
<div style="float: left;">
<? echo $rows['prop_city']; ?>, $<? echo $rows['price']; ?>$<? echo round($payment); ?>
</div>
</div>
<?
// close while loop
}
?>
I can post the tables if that is helpful.
If you are using two tables in your query, there should be a column in the second table linking the row to a row in the first column
Once you identified this field, write your query like this :
$sql="SELECT * FROM $tbl_name, documents_main WHERE show_listing='1' and user_type='1' and $tbl_name.FIELD_TO_BE_DEFINED = documents_main.ANOTHER_FIELD_TO_BE_DEFINED";
Or :
$sql="SELECT * FROM $tbl_name inner join documents_main on $tbl_name.FIELD_TO_BE_DEFINED = documents_main.ANOTHER_FIELD_TO_BE_DEFINED WHERE show_listing='1' and user_type='1'";

Using PHP (and maybe AJAX?) with jQTouch

I am having some difficulty using PHP with jQTouch. I am fairly
confident with JavaScript however my PHP skills are little to none.
I am creating an application for my final year project at University
what displays football rumours posted by different users. My problem
is as follows:
I have one screen that displays each individual rumour, using a while
loop in PHP I am able to get each rumour from the database and display
them correctly. However I want to be able to click on one rumour which
then displays this rumour in a different screen, along with options to
reply/share etc. However I do not know how to tell which rumour has
been clicked on.
Snippets of my code:
All rumours page:
<?php
$q1 = "SELECT * FROM tblrumours;";
$r1 = mysql_query($q1);
while( $row1 = mysql_fetch_assoc($r1) ){
?>
<a class="rumourTag submit" id="<?php echo $row1['rumourID']; ?>">
<div class='oneRumour'>
<div class='standardBubble'>
<p>
<?php
$userID = $row1['userID'];
$q2 = "SELECT * FROM tblusers WHERE userID = $userID;";
$r2 = mysql_query($q2);
while( $row2 = mysql_fetch_array($r2) ){
$username = $row2['username'];
$teamID = $row2['teamID'];
}
$q5 = "SELECT * FROM tblteams WHERE teamID = $teamID;";
$r5 = mysql_query($q5);
while( $row5 = mysql_fetch_array($r5) ){
echo "<img src='img/".$row5['teamPicture']."' alt=''
class='teamImg' />";
}
?>
<span class='username'>
<?php
echo $username;
?>
</span>
<br/>
<span class='rumourMsg'><?php echo $row1['rumourText']; ?></
span>
</p>
</div>
</a>
SINGLE RUMOURS PAGE:
<?php
$q1 = "SELECT * FROM tblrumours WHERE rumourID = 1;"; /* NEED
TO SELECT WHERE RUMOUR ID IS THE ONE THAT IS CLICKED */
$r1 = mysql_query($q1);
while( $row1 = mysql_fetch_array($r1) ){
?>..........
I have tried using Session variables, storing the ID's in an array,
creating a separate php file for the single rumour page, and all to no
avail. I am guessing I have to use AJAX in some way, but I have no
idea where to even begin. Any help is greatly appreciated!
Thanks!
If you need to click on a rumour to see more details about it, you could always output in the HTML a unique value used to reference that rumour in the DB.
e.g. have <span class='rumourMsg' id='rumourName'> where rumourName is a unique value stored in your database to reference that rumour. Then when a user clicks to see more details, you can make a request to the PHP page with that value and return the content.
e.g. rumourDetails?rumourName=uniqueRumourName
(make sure to escape all your data properly to avoid SQL injection vulnerabilities.)

Categories