Jump to content

My First Script


airbrushtutor

Recommended Posts

Hi Freaks!

I'm writing my first script from scratch.. best way to learn from everyone's advice i've seen.

My goal is to create a form whereby an individual inputs a username and email (these are then put into the variables $username and $email using the $_POST superglobal.

The part i'm stuck on is trying to sort through an existing database and check if the username exists, then i'd like to check if the email matches. All i'm trying to do is validate that they already exist in an existing table. If they don't, then i want to echo a message that says 'you must already be a member'.

 

The following is only a snippet of the code - the web form and previous mentioned variables are both working fine.

 

<?php
//Now check to see if these details are correct as per the airbrushforum database
//Connect to the database
$dbc = mysqli_connect('localhost', 'XXXXX', 'PWgoesHere', 'database name')
or die('Error connecting to MySQL server');
//Create the query and select all fields from the 'user' table
$query = "SELECT * FROM user";
$result = mysqli_query($dbc, $query)
or die('Error querying database');
//Create a row to loop through all results
$row = mysqli_fetch_array($result);
//Use a conditional statement to validate the email address and username - if either is incorrect then
//echo a message
//the following code is executing and echoing a list - how do i instead sort through the data to check 
//the $username and $email variable against what is in the database?
do 
{
 echo $row['userid'] . ' ' . $row['email'] . '<br/>';
} while ($row = mysqli_fetch_array($result));

?>

        //This is the basic conditional statement i need to check my data. (right?)
if ($row['username'] == $username && $row['email'] == $email)
{
//Execute the rest of the code here
}
else
{
//echo that they must be a member of the airbrushforum.
}

 

Can't figure out if i should be using a foreach loop? if i should be looping a conditional statement? advice?

If i've been unclear with anything i'm sure you'll let me know but please have patience with me, I don't seem to be picking up PHP as fast as i'd like to..

Link to comment
Share on other sites

When you reach the end of your while() loop, $row is set to false and so is not available once the loop exits.

 

Also no need to select all the records, just select the the one WHERE username = '$username'. And do not use SELECT * unless you really do need every field. You are only interested in the email. so

 

$sql = "SELECT email FROM user WHERE username = '$username' ";
$res = mysqli_query($dbc, $sql);
if (mysqli_num_rows($res) == 0) {
echo "User $username not found";
}
else {
$row = mysqli_fetch_assoc($res);
if ($row['email'] == $email) {
 // do something
}
else {
 // do something else
}
}

Edited by Barand
Link to comment
Share on other sites

Thanks very much for that Barand - it's such a concise & simple answer yet i couldn't come up with that myself. I guess it's just practise.

i haven't seen the 'mysqli_fetch_assoc' or ' mysqli_num_rows' functions before either so i'll have to check them out. I really appreciate your help, thank you for that :happy-04:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.