[PHP] Grabbing row data via ID
Hello peeps
Well im working on a project at work to help monitor domain names - ive got a script live and its working fine but now im advancing it with a cookie/session based login system with user access levels
well enough about that im trying to obtain domain informaiton from an id but it keeps firing an error at me
I also forgot to mention - when a domain name is entered there username is attached to the domain so i can make it show domain names inputted by each user so if the user's username is admin it will be
PHP Code:
username = '$session->username'
admin = admin
Here is the code im working on atm
PHP Code:
<?
include("include/session.php");
?>
<html>
<title>MMD - Alpha Build</title>
<body>
<link href="simple.css" rel="stylesheet" type="text/css" />
<table>
<tr><td>
<?
if($session->logged_in){
echo "<div align=right>Welcome <b>$session->username</b>!</div><br><br>"
."[<a href=\"userinfo.php?user=$session->username\">My Account</a>] "
."[<a href=\"useredit.php\">Edit Account</a>] "
."[<a href=\"insertdomain.php\">Insert Domain</a>] ";
if($session->isAdmin()){
echo "[<a href=\"admin/admin.php\">Admin Center</a>] ";
}
echo "[<a href=\"process.php\">Logout</a>]";
echo "<BR><BR><center><b>User Control Panel</b></center><BR><BR>";
echo "$session->username's Domain List<BR><BR>";
}
$result = mysql_query("SELECT id, dname, exp_date, note FROM domain WHERE id=\"$id\" AND username = '$session->username'");
if(mysql_num_rows($result)){
while($row=mysql_fetch_array($result))
{
$id=$row["id"];
$dname=$row["dname"];
$notes=$row["notes"];
$exp_date=$row["exp_date"];
$exp_date = date("d-m-Y", strtotime($exp_date) );
echo "
<h1>$ddname Details</h1> <BR>
<table width='350' border='0'>
<tr>
<td class='style21'>Domain Name:</td>
<td class='style19'><a href='http://$dname' target='_blank'>$dname</a></td>
</tr>
<tr>
<td class='style21'>Notes:</td>
<td class='style19'>$notes
</td>
</tr>
<tr>
<td class='style21'>Expires:</td>
<td class='style19'>$exp_date
</td>
</tr>
</table>
";
}
}
else{
echo "<BR>No Record corrosponds to this ID - Otherwise don't go where you are not supposed to! :-)<BR><BR>";
}
?>
Local whois check:<BR><BR>
<? if($site = "$dname") { exec("whois $site", $output, $error); while (list(,$line) = each($output)){ echo $line, "<BR>"; } } ?>
and the error is coming from line 37 which is
PHP Code:
if(mysql_num_rows($result)){
Any help will be greatly appreciated
Re: [PHP] Grabbing row data via ID
Your query is most likely wrong. Try this:
PHP Code:
$result = mysql_query("SELECT 'id', 'dname', 'exp_date', 'note' FROM `domain` WHERE id='$id' AND username = '$session->username'");
Re: [PHP] Grabbing row data via ID
Yeah it was the query! But it doesnt seem to echo the result... hmm
Re: [PHP] Grabbing row data via ID
Replace:
while($row=mysql_fetch_array($result))
With:
while($row=mysql_fetch_assoc($result))
Or:
while($row=mysql_fetch_array($result, MYSQL_ASSOC))
http://nl2.php.net/manual/en/functio...etch-array.php
http://nl2.php.net/manual/en/functio...etch-assoc.php
Re: [PHP] Grabbing row data via ID
Neither seem to work it just echo's
PHP Code:
No Record corrosponds to this ID - Otherwise don't go where you are not supposed to! :-)
Re: [PHP] Grabbing row data via ID
Try this code,,
PS: Remember to insert the MySQL info at the top!
PHP Code:
<?
$server=""; //enter the link til the MySQL server
$db_user=""; //enter the database username
$db_pass=""; //enter the database password
$database=""; //enter the name of the database it will connect with
include("include/session.php");
?>
<html>
<title>MMD - Alpha Build</title>
<body>
<link href="simple.css" rel="stylesheet" type="text/css" />
<table>
<tr><td>
<?
if($session->logged_in){
echo "<div align=right>Welcome <b>$session->username</b>!</div><br><br>"
."[<a href=\"userinfo.php?user=$session->username\">My Account</a>] "
."[<a href=\"useredit.php\">Edit Account</a>] "
."[<a href=\"insertdomain.php\">Insert Domain</a>] ";
if($session->isAdmin()){
echo "[<a href=\"admin/admin.php\">Admin Center</a>] ";
}
echo "[<a href=\"process.php\">Logout</a>]";
echo "<BR><BR><center><b>User Control Panel</b></center><BR><BR>";
echo "$session->username's Domain List<BR><BR>";
}
// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());
// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());
$result = mysql_query($database, "SELECT 'id', 'dname', 'exp_date', 'note' FROM `domain` WHERE id='$id' AND username = '$session->username' LIMIT 1");
if(mysql_num_rows($result)){
while($row=mysql_fetch_array($result))
{
$id=$row["id"];
$dname=$row["dname"];
$notes=$row["notes"];
$exp_date=$row["exp_date"];
$exp_date = date("d-m-Y", strtotime($exp_date) );
echo "
<h1>$dname Details</h1> <BR>
<table width='350' border='0'>
<tr>
<td class='style21'>Domain Name:</td>
<td class='style19'><a href='http://$dname' target='_blank'>$dname</a></td>
</tr>
<tr>
<td class='style21'>Notes:</td>
<td class='style19'>$notes
</td>
</tr>
<tr>
<td class='style21'>Expires:</td>
<td class='style19'>$exp_date
</td>
</tr>
</table>
";
}
}
else{
echo "<BR>No Record corrosponds to this ID - Otherwise don't go where you are not supposed to! :-)<BR><BR>";
}
?>
Local whois check:<BR><BR>
<? if($site = "$dname") { exec("whois $site", $output, $error); while (list(,$line) = each($output)){ echo $line, "<BR>"; } } ?>
Re: [PHP] Grabbing row data via ID
That is because it cannot find any matches in the DB.
Replace:
$result = mysql_query("SELECT id, dname, exp_date, note FROM domain WHERE id=\"$id\" AND username = '$session->username'");
With:
$result = mysql_query("SELECT id, dname, exp_date, note FROM domain WHERE id='".$id."' AND username = '".$session->username."'");
If that still doesn't work, see what $id and $session->username contain...and if it CAN match any row in the DB.
Re: [PHP] Grabbing row data via ID
Both methods didnt work
what i want to achieve is when some 1 logs in there session will be under there username and i want to match any domains they enter with there session name
for example when they enter a domain into the database it also has there username attached to it so matching the username they signed up with the domains they entered to prevent other users seeing there domain list
i developed a script for me to monitor domains that works fine but i want to incorparate a user system when different users can monitor there domains
i currently use the following code and it grabs the domain date via the id stored in the database and im tryin to get it working on the new system i plan to use in the future
PHP Code:
<?php
//connect to mysql
mysql_connect("localhost","root","");
mysql_select_db("domains") or die( "Unable to select database");
$result = mysql_query("SELECT id, dname, notes, exp_date FROM domain WHERE id=\"$id\"");
if(mysql_num_rows($result)){
while($row=mysql_fetch_array($result))
{
$id=$row["id"];
$dname=$row["dname"];
$notes=$row["notes"];
$exp_date=$row["exp_date"];
$exp_date = date("d-m-Y", strtotime($exp_date) );
echo "
<h1>$dname Details</h1> <BR>
<table width='350' border='0'>
<tr>
<td class='style21'>Domain Name:</td>
<td class='style19'><a href='http://$dname' target='_blank'>$dname</a></td>
</tr>
<tr>
<td class='style21'>Notes:</td>
<td class='style19'>$notes
</td>
</tr>
<tr>
<td class='style21'>Expires:</td>
<td class='style19'>$exp_date
</td>
</tr>
</table>
";
}
}
else{
echo "<BR><BR>No Record corrosponds to this ID - Otherwise don't go where you are not supposed to! :-)<BR><BR>";
}
?>
Re: [PHP] Grabbing row data via ID
where did you define $id in the querry.....
try this an post here the error
PHP Code:
$result = mysql_query("SELECT * FROM domain WHERE id='$id'") or die(mysql_error());
Re: [PHP] Grabbing row data via ID
PHP Code:
$result = mysql_query("SELECT 'id', 'dname', 'exp_date', 'note' FROM `domain` WHERE id='".$id."' AND username = '".$session->username."'"); or die(mysql_error());
Re: [PHP] Grabbing row data via ID
Quote:
Originally Posted by
z3r0_barat
where did you define $id in the querry.....
try this an post here the error
PHP Code:
$result = mysql_query("SELECT * FROM domain WHERE id='$id'") or die(mysql_error());
This gave me no error - just says
No Record corrosponds to this ID - Otherwise don't go where you are not supposed to! :-)
Quote:
Originally Posted by
Kreain
PHP Code:
$result = mysql_query("SELECT 'id', 'dname', 'exp_date', 'note' FROM `domain` WHERE id='".$id."' AND username = '".$session->username."'"); or die(mysql_error());
this returned an error
Parse error: syntax error, unexpected T_LOGICAL_OR in C:\xampplite\htdocs\domains\domain.php on line 35
Line 35 is where the query is
Re: [PHP] Grabbing row data via ID
I'm sorry for double posting but i have more to add - maybe this will help provide a solution to my problem
The aim of the game is when a user logs in he will see a list of all the domains on his account - this works - its fetched from the sql database based on his session username
for example if the Admin logged in it will only show the domains the Admin inserted
The code below is the first page they reach - it also has pagenavigation incorparated to prevent clutter (for example if there was 1000 domains in a list it spans it across pages with 25 domains on each)
This lists the domains they have with a link attached (The above code thats not working)
Wht is displayed is the Domain name and the Expiry date - its retreaving data from the DB without any problems so when you press the link it then should link all the details of the domain expiry date - notes - whois etc but that part isnt working for some reason
PHP Code:
$max = 25;
$p = $_GET['p'];
if(empty($p))
{
$p = 1;
}
$limits = ($p - 1) * $max;
if(isset($_GET['act']) && $_GET['act'] == "view")
{
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM domain WHERE id = '$id' AND '$username' = '$session->username' ORDER BY dname ASC");
while($r = mysql_fetch_array($sql))
{
$id = $r['id'];
$dname = $r['dname'];
$exp_date = $r['exp_date'];
$notes = $r['notes'];
$exp_date = $r['exp_date'];
$exp_date = date("l dS F Y", strtotime($exp_date) );
echo "<div><p>$dname</p></div>";
}
}else{
$sql = mysql_query("SELECT * FROM domain WHERE username = '$session->username' ORDER BY dname ASC LIMIT ".$limits.",$max") or die(mysql_error());
$totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM domain"),0);
$totalpages = ceil($totalres / $max);
echo "
<center><table width='320' border='0'>
<tr>
<td class='style21'><div align='center'><b>Domain</b></div></td>
<td class='style21'><div align='center'><b>Expires</b></div></td>
</tr>";
while($r = mysql_fetch_array($sql))
{
$id = $r['id'];
$dname = $r['dname'];
$exp_date = $r['exp_date'];
$notes = $r['notes'];
$exp_date = $r['exp_date'];
$exp_date= date("d-m-Y", strtotime($exp_date) );
echo "<tr>
<td class='style19'><a href=domain.php?id=$id>$dname</a></td>
<td class='style19'><div align='center'>$exp_date</div</td>
</tr>";
}
//close up the table
echo "</tr></table><BR>";
for($i = 1; $i <= $totalpages; $i++){
//this is the pagination link
echo "<span class='style19'><a href='main2.php?p=$i'>$i</a> - </span></center>";
}
}
ill attach a screenshot of what the main page looks like - its basic atm till its fully working - got a sexy design a good friend made to place in
http://i31.tinypic.com/33oqj2s.jpg
when the domain name is pressed like i mentioned above it displays all the information but it doesnt seem to want to work
Re: [PHP] Grabbing row data via ID
Just gonna bump as i've still not come across a solution =.
Re: [PHP] Grabbing row data via ID
What is ID?
Also, print the ID and username on screen of the user in question, then check manually in the database if that pair exists. Likely not, if you still get the
<BR>No Record corrosponds to this ID - Otherwise don't go where you are not supposed to! :-)<BR><BR>
message.
Re: [PHP] Grabbing row data via ID
I've solved it!
Forgot to put
PHP Code:
$id = $_GET['id'];
Before the query - now when some 1 tries to access an id that wasnt input by them results them in an error :)
Thanks everyone - Wouldnt of figured it out without you
Ta Daevius for making me tripple check everything
Re: [PHP] Grabbing row data via ID
Quote:
$id = $_GET['id'];
Escaping user input makes you look cool. No, really.
Also, escape both the domain and username, because someone may get smart and put some SQL stuff as a username ;]
Slowpoke is slow, but
Code:
$result = mysql_query("SELECT 'id', 'dname', 'exp_date', 'note' FROM `domain` WHERE id='".$id."' AND username = '".$session->username."'"); or die(mysql_error());
Didn't work because of the ;
Etc.
Hope this helps, somehow ;P
[Fd]
Re: [PHP] Grabbing row data via ID
PHP Code:
$id = $_GET['id'];
on my old system it worked without the above - thats why i didnt think of it as a problem