Search
Left Quote    Nearly all men can stand adversity, but if you want to test a man's character, give him power.
- Abraham Lincoln    
Right Quote
 
[login] | [Register]
Forum Index » PHP and mySQL » Viewing Topic and Replies
Post Reply


        Viewing Topic: NEW TO PHP, NEED HELP PLEASE
  This user is offline  santose13
  Subject: "NEW TO PHP, NEED HELP PLEASE" Posted: @ 4:03 pm on Feb 17 2011   
Member #: 1370
Rank: User - (5)
Since: 02/17/11
Posts: 5

I am new to PHP and i am tring to desing a members registration page from the information gattherd on this website. Now the code below may look really bad but please undterstand i am new to all of this. I am trying to get the code to verify if the user already exsist in the database. Right now i can input informatio in the datebase: name, email, password, ect. How would i make it so that the user gets an error message if he is already in the mysql database? Thank for the help ahead of time. Thank you.

Code:

<?php
include 'db.php';

$name = $_POST['name'];
$email = $_POST['email'] ;
$username = $_POST['usernam e'];
$password = $_POST['passwor d'];
$phone_number = $_POST['phone_n umber'];


// check e-mail format

if (!preg_match(&q uot;/.*@.*..*/& quot;, $_POST['email'] ) ||

preg_match(&quo t;/(<|>/" ;, $_POST['email'] )) {

die('Invalid e-mail address.');

}

// now we can add them to the database.

// encrypt password

$insert_query = 'insert into users (
name,
email,
phone_numb er,
username,
password
)
values
(
"' . $_POST['name'] . '",
"' . $_POST['email'] . '",
"' . $_POST['phone_n umber'] . '",
"' . $_POST['usernam e'] . '",
"' . md5($_POST['pas sword']) . '"
)';

mysql_query($in sert_query);



?>
<h1>Regis tered</h1> ;

<p>Thank you, your information has been added to the database,

you may now <a href="inde x.php?id=login& quot; title="Log in">log in</a>.&l t;/p>



Edited at 01:49:44 am on 02/23/11


    Viewed: 10,460 Times | Reply to This | To top
  This user is offline  sm0ke
  Subject: "re: NEW TO PHP, NEED HELP PLEASE" Posted: @ 10:12 pm on Feb 17 2011    

Member #: 168
Rank: User - (83)
Since: 06/05/06
Posts: 34
From: Manchester - UK

You can set the name and email feilds to contain unique values, so if a user enters an email address already in the system it errors on the DB side as it's not allowed to have the same email repeated in the email field.

Or do a quick check on the database - if email or name exist show error message else insert.....

I'd write the correction but fringe is on!

The site has a good tutorial for creaing a members area, you should check it out.




Just because I dont care, doesnt mean I dont understand.

purepcs@hotmail
.co.uk |     Viewed: 10,449 Times | Reply to This | To top
  This user is offline  santose13
  Subject: "re: NEW TO PHP, NEED HELP PLEASE" Posted: @ 2:05 am on Feb 18 2011    
Member #: 1370
Rank: User - (5)
Since: 02/17/11
Posts: 5

Thank you for your reply. still trying to figure it out.



Edited at 02:05:57 am on 02/18/11


    Viewed: 10,442 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: NEW TO PHP, NEED HELP PLEASE" Posted: @ 4:04 am on Feb 18 2011    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

Did you check out the Members System article?




-bs0d | AllSyntax.com

    Viewed: 10,435 Times | Reply to This | To top
  This user is offline  santose13
  Subject: "re: NEW TO PHP, NEED HELP PLEASE" Posted: @ 5:25 am on Feb 18 2011    
Member #: 1370
Rank: User - (5)
Since: 02/17/11
Posts: 5

I finaly figured it out, but i am runing in to another problem now. when i check my code it keeps going to a new page telling me "invalid email address". I guess is becouse of the email format code. How can i have this message show up on the register page only without displaying a new page? I hope i am being clear enough but i have been working all day trying to figure this out. Here is the code that i have so far:

Code:


<?php

include 'db.php';

//*Define post fields into simple variables
$name = $_POST['name' ];
$email = $_POST['email '];
$phone_number = $_POST['phone_ number'];
$username = $_POST['userna me'];
$password = $_POST ['password'];


// check e-mail format

if (!preg_match(& quot;/.*@.*..*/ ", $_POST['email ']) ||

preg_match(&qu ot;/(<|>/&quo t;, $_POST['email '])) {

die('Invalid e-mail address.');
}


$sql_email_che ck = mysql_query(&q uot;SELECT email FROM users WHERE email='$email '";
$sql_username_ check = mysql_query(&q uot;SELECT username FROM users WHERE username='$use rname'";

$email_check = mysql_num_rows( $sql_email_chec k);
$username_chec k = mysql_num_rows( $sql_username_c heck);

if(($email_che ck > 0) || ($username_chec k > 0)){
echo "Please fix the following errors: <br />";
if($email_che ck > 0){
echo "<stro ng>Your email address has already been used by another member in our database. Please submit a different Email address!<br />";
unset($email );
}
if($username_ check > 0){
echo "The username you have selected has already been used by another member in our database. Please choose a different Username!<br />";
unset($usern ame);
}
include 'register form.html'; // Show the form again!
exit(); // exit the script so that we do not create this account!
}

// now we can add them to the database.

// encrypt password

$insert_query = 'insert into users (
name,
email,
phone_numb er,
username,
password
)
values
(
"' . $_POST['name' ] . '",
"' . $_POST['email '] . '",
"' . $_POST['phone_ number'] . '",
"' . $_POST['userna me'] . '",
"' . md5($_POST['pa ssword']) . '"
)';

mysql_query($in sert_query);

$subject = "Your Membership at BloodyShirts.co m!";
$message = "Dear $first_name $last_name,
Thank you for registering at our website, http://www.Bloo dyShirts.com!

You are two steps away from logging in and accessing our exclusive members area.

To activate your membership, please click here: http://localhos t/members%20are a/activate.php? id=$userid& code=$password

Once you activate your memebership, you will be able to login with the following information:
Username: $username
Password: $password

Thanks!
The Webmaster

This is an automated response, please do not reply!";

mail($email_ad dress, $subject, $message, "From: <suteu10@yah oo.com>\nX- Mailer: PHP/" . phpversion());
echo 'Your membership information has been mailed to your email address! Please check it and follow the directions!';

?>



Edited at 01:48:22 am on 02/23/11


    Viewed: 10,432 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: NEW TO PHP, NEED HELP PLEASE" Posted: @ 6:40 am on Feb 20 2011    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

Maybe set the form action to PHP_Self so that it loads the same page. On error, display appropriately, and display form. This is how its done in the members system article here.




-bs0d | AllSyntax.com

    Viewed: 10,411 Times | Reply to This | To top
  This user is offline  santose13
  Subject: "re: NEW TO PHP, NEED HELP PLEASE" Posted: @ 6:10 pm on Feb 20 2011    
Member #: 1370
Rank: User - (5)
Since: 02/17/11
Posts: 5

I am sorry for asking such a stupid question but how do i do that? Do I write it out in the code? Or is it a option that i have to select? I am using adobe Dreamweaver CS3 to write out the code. If i do write it out in the code where would i put it?




    Viewed: 10,407 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: NEW TO PHP, NEED HELP PLEASE" Posted: @ 1:06 am on Feb 21 2011    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

Just follow the instructions, starting here: Registration Script




-bs0d | AllSyntax.com

    Viewed: 10,395 Times | Reply to This | To top
  This user is offline  santose13
  Subject: "re: NEW TO PHP, NEED HELP PLEASE" Posted: @ 9:53 pm on Feb 21 2011    
Member #: 1370
Rank: User - (5)
Since: 02/17/11
Posts: 5

I have tried the PHP_SELF just like in the tutorial. I kept getting error messages. Here is the rigistration code that i am using. Can you please tell me where and hot to isert the code you stated:


Code:


<!DOCTYPE html PUBLIC "-//W3C// DTD XHTML 1.0 Transitional//E N" "http://w ww.w3.org/TR/xh tml1/DTD/xhtml1 -transitional.d td">
<html xmlns="ht tp://www.w3.org /1999/xhtml&qu ot;>
<head>
<meta http-equiv=&qu ot;Content-Type " content=" text/html; charset=utf-8& quot; />
<title>Un titled Document</ti tle>
</head>

<body>
<form name="reg istration_form " method="p ost" action="r egister1.php&q uot; onsubmit=" ;return Validate();&qu ot;>

<table width="10 0%" border="0 " cellpadding=&q uot;4" cellspacing=&q uot;0"> ;
<tr>
<td width="24 %" align="le ft" valign="t op">Na me*</td>
<td width="76 %">< ;input type="tex t" name="nam e">< ;/td>
</tr>
<table width="10 0%" border="0 " cellpadding=&q uot;4" cellspacing=&q uot;0"> ;
<tr>
<td width="24 %" align="le ft" valign="t op">Em ail Address*</td >
<td width="76 %">< ;input type="tex t" name="ema il">&l t;/td>
</tr>
<table width="10 0%" border="0 " cellpadding=&q uot;4" cellspacing=&q uot;0"> ;
<tr>
<td width="24 %" align="le ft" valign="t op">Us ername*</td& gt;
<td width="76 %">< ;input type="tex t" name="use rname"> ;</td>
</tr>
<table width="10 0%" border="0 " cellpadding=&q uot;4" cellspacing=&q uot;0"> ;
<tr>
<td width="24 %" align="le ft" valign="t op">Pa ssword*</td& gt;
<td width="76 %">< ;input type="pas sword" name="pas sword"> ;</td>
</tr>< table width="10 0%" border="0 " cellpadding=&q uot;4" cellspacing=&q uot;0"> ;
<tr>
<td width="24 %" align="le ft" valign="t op">Re type Password*</t d>
<td width="76 %">< ;input type="pas sword" name="pas sword_confirmat ion">& lt;/td>
</tr>< table width="10 0%" border="0 " cellpadding=&q uot;4" cellspacing=&q uot;0"> ;
<tr>
<td width="24 %" align="le ft" valign="t op">Ph one Number*</td& gt;
<td width="76 %">< ;input type="tex t" name="pho ne_number" ;></td> ;
</tr>
<tr>
<td align="le ft" valign="t op">&a mp;nbsp;</td >
<td><i nput type="sub mit" name="Sub mit" value="Re gister"&g t;</td>
</tr>
</table>


<script language = "Javascri pt">

function Validate()
{
if (document.regis tration_form.na me.value == '')
{
alert('Please fill in your name!');
return false;
}
if (document.regis tration_form.em ail.value == '')
{
alert('Please fill in your email address!');
return false;
}
if (document.regis tration_form.us ername.value == '')
{
alert('Please fill in your desired username!');
return false;
}
if (document.regis tration_form.pa ssword.value == '')
{
alert('Please fill in your desired password!');
return false;
}
if (document.regis tration_form.pa ssword_confirma tion.value == '')
{
alert('Please fill in your password again for confirmation!' );
return false;
}
if (document.regis tration_form.pa ssword.value !=
document.regist ration_form.pas sword_confirmat ion.value)
{
alert("Th e two passwords are not identical! "+
"Please enter the same password again for confirmation&q uot;
return false;
}
if (document.regis tration_form.ph one_number.valu e == '')
{
alert('Please fill in your phone number!');
return false;
}
return true;
}
</script>
</body>
</html>



Edited at 01:41:09 am on 02/23/11


    Viewed: 10,382 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: NEW TO PHP, NEED HELP PLEASE" Posted: @ 1:41 am on Feb 23 2011    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

Please try to use bbcode when posting your code, otherwise your posts are an eye sore and it makes the code difficult to interpret. It also keep's the emoticons from appearing in your code.

With that being said, it seems clear to me that you're still not following the article.

If you want my recommendation, it would be to build your script exactly like the Members System article. Once you get that working, start customizing it into what you need.




-bs0d | AllSyntax.com

    Viewed: 10,362 Times | Reply to This | To top
Viewing Page: 1 of 1


1 |

You must be logged in to post on the forums. Login or Register








"AllSyntax.com" Copyright © 2002-2017; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank