[SOLVED] PHP database code issue

This is a discussion on "[SOLVED] PHP database code issue" within the PHP Forum section. This forum, and the thread "[SOLVED] PHP database code issue are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Mar 22nd, 2008, 15:17
acrikey's Avatar
SuperMember

SuperMember
Join Date: Mar 2007
Location: UK
Posts: 305
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] PHP database code issue

I am struggling to get a bit of code to work, have i got it right ? I want to access the database once logged in to display customer details.

PHP: Select all

// issue the query
$sql "SELECT f_name, 1_name FROM users WHERE
        username = '"
.$_POST["username"]."' AND
        password = PASSWORD('"
.$_POST["password"]."')";
$result mysqli_query($mysqli$sql) or die(mysqli_error($mysqli));

    
        
//display string
    
$display_block "
    <p>"
.$f_name." <p>
    <p>"
.$l_name." <p>
    <p>"
.$h_name." <p>
    <p>"
.$2l_address." <p>
    <p>"
.$vill_address." <p>
    <p>"
.$ar_address." <p>
    <p>"
.$cout_address." <p>
    <p>"
.$pc_address." <p>
    <p>"
.$hc_phone." <p>
    <p>"
.$wc_phone." <p>
    <p>"
.$mc_phone." <p>
    <p>"
.$email." <p> 

Last edited by Rob; Mar 29th, 2008 at 01:15. Reason: PHP tags instead of code tags, please
Reply With Quote

  #2 (permalink)  
Old Mar 22nd, 2008, 16:00
CloudedVision's Avatar
Nerdy Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 942
Blog Entries: 8
Thanks: 2
Thanked 21 Times in 21 Posts
Send a message via AIM to CloudedVision Send a message via MSN to CloudedVision Send a message via Skype™ to CloudedVision
Re: PHP database code issue

Use this:

PHP: Select all

m// issue the query
$sql "SELECT * FROM users WHERE
        username = '"
.$_POST["username"]."' AND
        password = PASSWORD('"
.$_POST["password"]."')";
$result mysqli_query($mysqli$sql) or die(mysqli_error($mysqli));
$info mysqli_fetch_array($result);
    
        
//display string
    
$display_block "
    <p>"
.$info['f_name']." <p>
    <p>"
.$info['l_name']." <p>";

etc
__________________
echo "Take it easy, ".$CloudedVision;
.links { site: other-road-design; blog: only-nerds-allowed; project: resource-fish; organization: ARMIES6; }
<quote>&quot;I think it's wrong that only one company makes the game Monopoly&quot; - <name>Steven Wright</name></quote>
Last Blog Entry: More Cheat Sheets (Jul 12th, 2008)
Reply With Quote
  #3 (permalink)  
Old Mar 22nd, 2008, 16:26
acrikey's Avatar
SuperMember

SuperMember
Join Date: Mar 2007
Location: UK
Posts: 305
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP database code issue

Ok just getting a blank screen on test.
Reply With Quote
  #4 (permalink)  
Old Mar 22nd, 2008, 16:32
Reputable Member
Join Date: Nov 2007
Location: India
Posts: 150
Blog Entries: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP database code issue

^Did you echo $display_block ?
PHP: Select all

echo $display_block
add this at the end of the code.
Last Blog Entry: Cross browser nuisance (Feb 11th, 2008)
Reply With Quote
  #5 (permalink)  
Old Mar 22nd, 2008, 16:32
CloudedVision's Avatar
Nerdy Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 942
Blog Entries: 8
Thanks: 2
Thanked 21 Times in 21 Posts
Send a message via AIM to CloudedVision Send a message via MSN to CloudedVision Send a message via Skype™ to CloudedVision
Re: PHP database code issue

You have to print it! add this to the end.

PHP: Select all

echo $display_block
__________________
echo "Take it easy, ".$CloudedVision;
.links { site: other-road-design; blog: only-nerds-allowed; project: resource-fish; organization: ARMIES6; }
<quote>&quot;I think it's wrong that only one company makes the game Monopoly&quot; - <name>Steven Wright</name></quote>
Last Blog Entry: More Cheat Sheets (Jul 12th, 2008)
Reply With Quote
  #6 (permalink)  
Old Mar 22nd, 2008, 16:35
acrikey's Avatar
SuperMember

SuperMember
Join Date: Mar 2007
Location: UK
Posts: 305
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP database code issue

no, echo it below?
Reply With Quote
  #7 (permalink)  
Old Mar 22nd, 2008, 16:38
CloudedVision's Avatar
Nerdy Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 942
Blog Entries: 8
Thanks: 2
Thanked 21 Times in 21 Posts
Send a message via AIM to CloudedVision Send a message via MSN to CloudedVision Send a message via Skype™ to CloudedVision
Re: PHP database code issue

This should be the entire code

PHP: Select all

$sql "SELECT * FROM users WHERE
        username = '"
.$_POST["username"]."' AND
        password = PASSWORD('"
.$_POST["password"]."')";
$result mysqli_query($mysqli$sql) or die(mysqli_error($mysqli));
$info mysqli_fetch_array($result);
    
        
//display string
    
$display_block "
    <p>"
.$info['f_name']." <p>
    <p>"
.$info['l_name']." <p>";

echo 
$display_block
__________________
echo "Take it easy, ".$CloudedVision;
.links { site: other-road-design; blog: only-nerds-allowed; project: resource-fish; organization: ARMIES6; }
<quote>&quot;I think it's wrong that only one company makes the game Monopoly&quot; - <name>Steven Wright</name></quote>
Last Blog Entry: More Cheat Sheets (Jul 12th, 2008)

Last edited by Rob; Mar 29th, 2008 at 01:15.
Reply With Quote
  #8 (permalink)  
Old Mar 22nd, 2008, 16:45
acrikey's Avatar
SuperMember

SuperMember
Join Date: Mar 2007
Location: UK
Posts: 305
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP database code issue

ok still nothing
Reply With Quote
  #9 (permalink)  
Old Mar 22nd, 2008, 16:49
CloudedVision's Avatar
Nerdy Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 942
Blog Entries: 8
Thanks: 2
Thanked 21 Times in 21 Posts
Send a message via AIM to CloudedVision Send a message via MSN to CloudedVision Send a message via Skype™ to CloudedVision
Re: PHP database code issue

PHP: Select all

$sql "SELECT * FROM users WHERE
        username = '"
.$_POST["username"]."' AND
        password = PASSWORD('"
.$_POST["password"]."')";
$result mysqli_query($mysqli$sql) or die(mysqli_error($mysqli));
if(
$info mysqli_fetch_array($result)) { 
        
//display string
    
$display_block "
    <p>"
.$info['f_name']." <p>
    <p>"
.$info['l_name']." <p>";

echo 
$display_block
} else {
   echo 
'<p>Wrong username and password!</p>';

__________________
echo "Take it easy, ".$CloudedVision;
.links { site: other-road-design; blog: only-nerds-allowed; project: resource-fish; organization: ARMIES6; }
<quote>&quot;I think it's wrong that only one company makes the game Monopoly&quot; - <name>Steven Wright</name></quote>
Last Blog Entry: More Cheat Sheets (Jul 12th, 2008)
Reply With Quote
  #10 (permalink)  
Old Mar 22nd, 2008, 16:49
acrikey's Avatar
SuperMember

SuperMember
Join Date: Mar 2007
Location: UK
Posts: 305
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP database code issue

As the page has got to look up the db details again, I am right in saying I need to remind the page of the db data again or just remind it to look at the db page again
Reply With Quote
  #11 (permalink)  
Old Mar 22nd, 2008, 16:53
acrikey's Avatar
SuperMember

SuperMember
Join Date: Mar 2007
Location: UK
Posts: 305
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP database code issue

Ok this is what I have got on the new page:

Code: Select all
<?php
ob_start();
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name

m// issue the query
$sql = "SELECT * FROM users WHERE
        username = '".$_POST["username"]."' AND
        password = PASSWORD('".$_POST["password"]."')";
$result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
if($info = mysqli_fetch_array($result)) { 
        //display string
    $display_block = "
    <p>".$info['f_name']." <p>
    <p>".$info['l_name']." <p>";

echo $display_block; 
} else {
   echo '<p>Wrong username and password!</p>';
} 
?>
Reply With Quote
  #12 (permalink)  
Old Mar 22nd, 2008, 16:58
CloudedVision's Avatar
Nerdy Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 942
Blog Entries: 8
Thanks: 2
Thanked 21 Times in 21 Posts
Send a message via AIM to CloudedVision Send a message via MSN to CloudedVision Send a message via Skype™ to CloudedVision
Re: PHP database code issue

you gotta connect to the database!
__________________
echo "Take it easy, ".$CloudedVision;
.links { site: other-road-design; blog: only-nerds-allowed; project: resource-fish; organization: ARMIES6; }
<quote>&quot;I think it's wrong that only one company makes the game Monopoly&quot; - <name>Steven Wright</name></quote>
Last Blog Entry: More Cheat Sheets (Jul 12th, 2008)
Reply With Quote
  #13 (permalink)  
Old Mar 22nd, 2008, 17:03
acrikey's Avatar
SuperMember

SuperMember
Join Date: Mar 2007
Location: UK
Posts: 305
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP database code issue

Ok, just to make sure we all know what I am trying to do.
Member logs in to the login screen and gets to the members area, from there they have a new menu, if they select account details, the new page is created with their personal details on it from the database. The new page must cross reference the username and password so as to display the correct details.
Reply With Quote
  #14 (permalink)  
Old Mar 22nd, 2008, 17:05
acrikey's Avatar
SuperMember

SuperMember
Join Date: Mar 2007
Location: UK
Posts: 305
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP database code issue

Code: Select all
<?php
ob_start();
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

m// issue the query
$sql = "SELECT * FROM members WHERE
        username = '".$_POST["username"]."' AND
        password = PASSWORD('".$_POST["password"]."')";
$result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
if($info = mysqli_fetch_array($result)) { 
        //display string
    $display_block = "
    <p>".$info['f_name']." <p>
    <p>".$info['l_name']." <p>";

echo $display_block; 
} else {
   echo '<p>Wrong username and password!</p>';
} 
?>
Reply With Quote
  #15 (permalink)  
Old Mar 22nd, 2008, 17:05
CloudedVision's Avatar
Nerdy Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 942
Blog Entries: 8
Thanks: 2
Thanked 21 Times in 21 Posts
Send a message via AIM to CloudedVision Send a message via MSN to CloudedVision Send a message via Skype™ to CloudedVision
Re: PHP database code issue

i understand

you still gotta connect to the database.
__________________
echo "Take it easy, ".$CloudedVision;
.links { site: other-road-design; blog: only-nerds-allowed; project: resource-fish; organization: ARMIES6; }
<quote>&quot;I think it's wrong that only one company makes the game Monopoly&quot; - <name>Steven Wright</name></quote>
Last Blog Entry: More Cheat Sheets (Jul 12th, 2008)
Reply With Quote
  #16 (permalink)  
Old Mar 22nd, 2008, 17:05
Jack Franklin's Avatar
Resources Administrator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,268
Blog Entries: 7
Thanks: 10
Thanked 4 Times in 4 Posts
Re: PHP database code issue

In your code you posted a couple of posts up, you havent actually connected to the database.

(Okay you have now)
Last Blog Entry: My Latest Project - Grilling Gurus... (Jun 11th, 2008)
Reply With Quote
  #17 (permalink)  
Old Mar 22nd, 2008, 17:19
acrikey's Avatar
SuperMember

SuperMember
Join Date: Mar 2007
Location: UK
Posts: 305
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP database code issue

still getting a blank screen though
Reply With Quote
  #18 (permalink)  
Old Mar 22nd, 2008, 17:26
CloudedVision's Avatar
Nerdy Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 942
Blog Entries: 8
Thanks: 2
Thanked 21 Times in 21 Posts
Send a message via AIM to CloudedVision Send a message via MSN to CloudedVision Send a message via Skype™ to CloudedVision
Re: PHP database code issue

try deleting the ob_start();
__________________
echo "Take it easy, ".$CloudedVision;
.links { site: other-road-design; blog: only-nerds-allowed; project: resource-fish; organization: ARMIES6; }
<quote>&quot;I think it's wrong that only one company makes the game Monopoly&quot; - <name>Steven Wright</name></quote>
Last Blog Entry: More Cheat Sheets (Jul 12th, 2008)
Reply With Quote
  #19 (permalink)  
Old Mar 22nd, 2008, 17:36
acrikey's Avatar
SuperMember

SuperMember
Join Date: Mar 2007
Location: UK
Posts: 305
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP database code issue

nothing
Reply With Quote
  #20 (permalink)  
Old Mar 22nd, 2008, 17:58
CloudedVision's Avatar
Nerdy Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 942
Blog Entries: 8
Thanks: 2
Thanked 21 Times in 21 Posts
Send a message via AIM to CloudedVision Send a message via MSN to CloudedVision Send a message via Skype™ to CloudedVision
Re: PHP database code issue

can you show the entire script? or is the script above the entire script?
__________________
echo "Take it easy, ".$CloudedVision;
.links { site: other-road-design; blog: only-nerds-allowed; project: resource-fish; organization: ARMIES6; }
<quote>&quot;I think it's wrong that only one company makes the game Monopoly&quot; - <name>Steven Wright</name></quote>
Last Blog Entry: More Cheat Sheets (Jul 12th, 2008)
Reply With Quote