[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.


 Subscribe in a reader

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

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Mar 22nd, 2008, 15:17
Reputable Member
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Mar 22nd, 2008, 16:00
CloudedVision's Avatar
Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 1,257
Blog Entries: 9
Thanks: 2
Thanked 40 Times in 40 Posts
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
__________________
Web Design And Development: Other Road Design | Problems with IE6?: KApp | My Blog: Only Nerds Allowed | Learning PHP? Lessons
Last Blog Entry: Hilarious Rapper (Jul 29th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Mar 22nd, 2008, 16:26
Reputable Member
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Mar 22nd, 2008, 16:32
CloudedVision's Avatar
Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 1,257
Blog Entries: 9
Thanks: 2
Thanked 40 Times in 40 Posts
Re: PHP database code issue

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

PHP: Select all

echo $display_block
__________________
Web Design And Development: Other Road Design | Problems with IE6?: KApp | My Blog: Only Nerds Allowed | Learning PHP? Lessons
Last Blog Entry: Hilarious Rapper (Jul 29th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Mar 22nd, 2008, 16:35
Reputable Member
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Mar 22nd, 2008, 16:38
CloudedVision's Avatar
Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 1,257
Blog Entries: 9
Thanks: 2
Thanked 40 Times in 40 Posts
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
__________________
Web Design And Development: Other Road Design | Problems with IE6?: KApp | My Blog: Only Nerds Allowed | Learning PHP? Lessons
Last Blog Entry: Hilarious Rapper (Jul 29th, 2008)

Last edited by Rob; Mar 29th, 2008 at 01:15.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Mar 22nd, 2008, 16:45
Reputable Member
Join Date: Mar 2007
Location: UK
Posts: 305
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP database code issue

ok still nothing
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Mar 22nd, 2008, 16:49
CloudedVision's Avatar
Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 1,257
Blog Entries: 9
Thanks: 2
Thanked 40 Times in 40 Posts
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>';

__________________
Web Design And Development: Other Road Design | Problems with IE6?: KApp | My Blog: Only Nerds Allowed | Learning PHP? Lessons
Last Blog Entry: Hilarious Rapper (Jul 29th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Mar 22nd, 2008, 16:49
Reputable Member
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Mar 22nd, 2008, 16:53
Reputable Member
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>';
} 
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Mar 22nd, 2008, 16:58
CloudedVision's Avatar
Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 1,257
Blog Entries: 9
Thanks: 2
Thanked 40 Times in 40 Posts
Re: PHP database code issue

you gotta connect to the database!
__________________
Web Design And Development: Other Road Design | Problems with IE6?: KApp | My Blog: Only Nerds Allowed | Learning PHP? Lessons
Last Blog Entry: Hilarious Rapper (Jul 29th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #13  
Old Mar 22nd, 2008, 17:03
Reputable Member
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #14  
Old Mar 22nd, 2008, 17:05
Reputable Member
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>';
} 
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #15  
Old Mar 22nd, 2008, 17:05
CloudedVision's Avatar
Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 1,257
Blog Entries: 9
Thanks: 2
Thanked 40 Times in 40 Posts
Re: PHP database code issue

i understand

you still gotta connect to the database.
__________________
Web Design And Development: Other Road Design | Problems with IE6?: KApp | My Blog: Only Nerds Allowed | Learning PHP? Lessons
Last Blog Entry: Hilarious Rapper (Jul 29th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #16  
Old Mar 22nd, 2008, 17:05
Jack Franklin's Avatar
Moderator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,405
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 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)
__________________
Jack Franklin - Webforumz Moderator
(x)HTML | CSS | PHP | MySQL | JQuery (Javascript)
Contact: My Blog | Twitter | Delicious
Want Lessons? PM me.
If you think I've helped, please press the 'Thanks' Button.
Last Blog Entry: A Week with VBulletin (Aug 28th, 2008)