Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Another PHP and Mysql question

Options
  • 04-09-2005 11:52pm
    #1
    Registered Users Posts: 148 ✭✭


    Can you please help me on this, I am trying to create a user register form, after field validation, I want to check if the user exist from my file and I am having problem wiith this, what I want if the user exist, display an error message and let the user key in again. Again, I am new to this just followed the code from the book,

    My form fileds are; emailid and password
    My database is Register
    My table is Passpf

    here is my code;


    <?php

    $user = "root";
    $host = "localhost";
    $password = "pass";
    $database = "register";
    $connection = mysql_connect($host, $user, $password)
    or die ("Couldn't connect to server, please try again later");
    $db = mysql_select_db($database, $connection)
    or die ("Couldn't connect to database, please try again later");

    foreach ($HTTP_POST_VARS as $key => $value )
    {
    If ($key == "email")
    {
    $query = "Select email from passpf where email = '$value'";
    $result = mysql_query($query)
    or die ("Couldn't connect to database, please try again later");
    $row = mysql_fetch_array($result);
    }
    }
    ?>

    My first problem is, how to retrieve value from emaild, it seems my code is not working.
    Second, how do I check if record exist or not
    Third, how do I display my form again if record exist.

    Thank you very much . . .


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    1) Try changing

    [php]$query = "Select email from passpf where email = '$value'";[/php]

    to

    [php]$query = "Select email from passpf where email = " . $value;[/php]

    2) The record should exist if mysql_fetch_array returns an array. This array corresponds to a row in the MySQL database, and will have the same column layout. It will return false otherwise.

    3) If mysql_fetch_array doesn't return false, layout the form using PHP echos, setting the values of the elements to the values you have pulled from the array.


  • Registered Users Posts: 148 ✭✭maco


    Thanks for that, I think this statement does not work for me,

    foreach ($HTTP_POST_VARS as $key => $value )
    {
    If ($key == "email")
    {

    is this ocrrect or did I miss something here . . .


  • Banned (with Prison Access) Posts: 16,659 ✭✭✭✭dahamsta


    If it's a new version of PHP the legacy superglobals might be disabled. Try $_POST instead of $HTTP_POST_VARS (and use $_POST, $_GET, etc in future, the legacy ones will disappear completely at some stage).

    EDIT: Actually they don't match up, you're using emailid in the form but looking for email in the input. And there's no need to loop through the POST array, you just need to look for $_POST. Replace this...

    [PHP]foreach ($HTTP_POST_VARS as $key => $value )
    {
    If ($key == "email")
    {
    $query = "Select email from passpf where email = '$value'";
    $result = mysql_query($query)
    or die ("Couldn't connect to database, please try again later");
    $row = mysql_fetch_array($result);
    }
    }[/PHP]...with this...

    [PHP]if ($emailid = $_POST) {
    $query = "Select email from passpf where email='$emailid'";
    $result = mysql_query($query) or die ("Couldn't connect to database, please try again later");
    $row = mysql_fetch_array($result);
    }[/PHP]Also look into using PEAR or ADODB for database abstraction, it simplifies things immensely. I recommend PEAR because it's almost universally available and you won't need the advanced features of ADODB, but it's pretty much six of one and half a dozen of the other for yourself.

    adam


  • Registered Users Posts: 148 ✭✭maco


    Thanks Dahamsta, but still did not work, for testing purposes I have modified the code just to check if its work;

    foreach ($HTTP_POST_VARS as $key => $value )
    {
    echo "test 1";
    }

    if ($email = $_POST)
    {
    echo "test 2";
    }

    None of the two codes above work, on the second code ($_POST) there is an error message saying Undefined index; email in line 208 . . .

    Any idea ?


  • Banned (with Prison Access) Posts: 16,659 ✭✭✭✭dahamsta


    For the first one, print out the entire contents of HTTP_POST_VARS with print_r($HTTP_POST_VARS), however I already suggested that you shouldn't be using that.

    For the latter, I already told you that your vars don't match up, you're using emailid in the form and email in the script.

    Read my post again.

    adam


  • Advertisement
  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    maco wrote:
    My first problem is, how to retrieve value from emaild, it seems my code is not working.
    Dahamsta has responded to this.
    Second, how do I check if record exist or not
    Try:
    [PHP]$query = "Select COUNT(email) from passpf where email = '$value'";
    $result = mysql_query($query)
    or die ("Couldn't connect to database, please try again later");
    $row = mysql_fetch_array($result);
    if ($row[0] == 0) {
    // Record corresponding to email does not exist.
    } else {
    // Record corresponding to email exists.
    }[/PHP]
    Third, how do I display my form again if record exist.
    If your form is on another script, redirecting back to it would do it using something like:
    [PHP]header ("Location: myform.php");[/PHP]
    Alternatively you could combine the two on the same script, so that when you’ve finished processing any POST data, you can display the form.


  • Registered Users Posts: 148 ✭✭maco


    Sorry Dahamsta, that's a typo error, email is the correct field not the emailid. I will try to check it again . . . it's bit frustrating now, not sure where is the problem.

    Just tried to issue print_r($HTTP_POST_VARS); the output says Aray(), it seems to be it did not manage to pick any fields at all. I have used Dreamweaver to create a form will this matter ?

    Thanks The Corinthian, I will do that after I fix my first problem.


  • Registered Users Posts: 5,335 ✭✭✭Cake Fiend


    Print the contents of $_POST and see if it picks it up. Maybe post up the html code for your form page as well. Have you double-checked that your email input field is named 'email'?


  • Banned (with Prison Access) Posts: 16,659 ✭✭✭✭dahamsta


    He'd be better off with print_r($_POST), then we could see everything coming in. (Which I suggested in post #6.)

    adam


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    SQL Injection Warning. I know you're just starting, but it would be a mistake to get into (very) bad habits. addslashes and stripslashes are your friends.


  • Advertisement
  • Registered Users Posts: 148 ✭✭maco


    Thanks for all your replies, I did try what the print_r, they both give me Array(), Is there anything that you need to set up before issueing this comand ? Is this the only way to retrieve data from form ?

    I think at this point I will give it a break first, a day or two maybe. Need to read my book all over again. Maybe all I need is a fresh mind.

    Anyway, thanks to all.


  • Registered Users Posts: 5,335 ✭✭✭Cake Fiend


    Show us the html you're using for the form. If $HTTP_POST_VARS and $_POST are both empty, your form mightn't be sending the data properly.

    Maybe try manually setting variables and printing the contents of $_GET, as in example.php?email=whatever&randomvar=12345 . If that works, use method="GET" in your form and see if that works, to narrow down where the problem lies.


  • Registered Users Posts: 148 ✭✭maco


    Sico, thank you very much but I have given up that route, managed to trap the error while doing the insert statements and manipulate the sql error code from there.

    Guys, thanks a lot again . . . In this case this thread is now closed.


Advertisement