Jump to content

Register


stanleybb

Recommended Posts

Hi i would jsut like to say im new to php

 

I want to make a registration page for my website, i have a mysql_connect.php link to the database which is phpmyadmin. The problem i am having is that as soon as you type in the info, on the form location on the site and click the button it doesnt save anything to the database. Why is this and what can i do?

 

THats the code

 

<?php

 

$page_title = 'Register';

include ('./includes/header.html');

 

//check if the form has been submitted.

if(isset($_POST['submitted'])) {

 

$errors = array(); // initialize error array.

 

//Check first name

if (empty($_POST['first_name'])) {

$errors[]= 'You forgot to enter your first name.';

} else {

$fn = trim($_POST['first_name']);

}

 

//Check last name

if (empty($_POST['last_name'])) {

$errors[]= 'You forgot to enter your last name.';

} else {

$ln = trim($_POST['last_name']);

}

 

//Check email address

if (empty($_POST['email'])) {

$errors[]= 'you forgot to enter email address.';

} else {

$e = trim($_POST['email']);

}

 

//check the passwords match

if(!empty($_POST['password1'])) {

if($_POST['password1'] != $_POST['password2']) {

$errors[] = 'Your password did not match the confirmed password.';

} else {

 

$p = trim($_POST['password1']);

}

} else {

$errors[] = 'You forgot to enter your password.';

}

 

if(empty($errors)) { // if everythings okay.

 

//register the user in the database.

require_once('../mysql_connect.php'); // connect to the db

 

//Make the query

$query = "INSERT INTO users (first_name, last_name, email, password, registration_date)

VALUES ('$fn', '$ln', '$e', SHA('$p'), NOW())";

$result = @mysql_query ($query); //run query.

 

if($result) { // if it ran ok

 

//send an email, if desired

 

//print message

echo '<h1 id="mainhead">Thank you!</h1>

<p>You are now registered.</p><p><br /></p>';

 

//include the footer and quit the script (to not show the form).

include ('.includes/footer.html');

exit();

 

} else {

 

echo '<h1 id="mainhead">Systems Error</h1>

<p class="error">you could not be registered due to a system error</p>';

echo '<p>' . mysql_error(). '<br /><br />Query: ' .$query . '</p>';

include ('./includes/footer.html');

exit();

}

 

mysql_close(); // close the database connection.

 

} else { //Report the errors

 

echo '<h1 id="mainhead">Error!</h1>

<p class="error"> The following error(s) occurred:<br/>';

foreach($errors as $msg) { //print each error.

echo " - $msg<br />\n";

}

 

echo '</p><p>Please try again.</p><p><br /></p>';

 

} // End of if (empty($errors)) IF.

 

}// End of the main Submit conditional.

 

?>

 

<h2>Register</h2>

<form action="register.php" method="post">

<p>First Name: <input type="text" name="first_name" size="15" maxlength="15" value="<?php if

(isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>

 

<p>Last Name: <input type="text" name="last_name" size="15" maxlength="30" value="<?php if

(isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>

 

<p>Email Address: <input type="text" name="email" size="15" maxlength="30" value="<?php if

(isset($_POST['email'])) echo $_POST['email']; ?>" /></p>

 

<p>Password: <input type="password" name="password1" size="10" maxlength="20" /></p>

 

<p>Confirm Password: <input type="password" name="password2" size="10" maxlength="20" /></p>

 

<p><input type="submit" name="submit" value="Register" /></p>

<input type="hidden" name="submitted" value="TRUE" />

</form>

<?php

include ('.includes/footer.html');

?>

Link to comment
https://forums.phpfreaks.com/topic/92908-register/
Share on other sites

try now see what the database chucks out.....

 

always echo yor querys out to see if there set correctly.

 

$query = "INSERT INTO users (first_name, last_name, email, password, registration_date)
VALUES ('$fn', '$ln', '$e', SHA('$p'), NOW())";
$result = mysql_query ($query)or die(mysql_error()); 

 

try include()

 

include("../mysql_connect.php"); // connect to the db

Link to comment
https://forums.phpfreaks.com/topic/92908-register/#findComment-475933
Share on other sites

set for all in one......

 

<?php

$page_title = 'Register';
include ('./includes/header.html');

//check if the form has been submitted.
if(isset($_POST['submitted'])) {

$errors = array(); // initialize error array.

//Check first name
if (empty($_POST['first_name'])) {
$errors[]= 'You forgot to enter your first name.';
} else {
$fn = trim($_POST['first_name']);
}

//Check last name
if (empty($_POST['last_name'])) {
$errors[]= 'You forgot to enter your last name.';
} else {
$ln = trim($_POST['last_name']);
}

//Check email address
if (empty($_POST['email'])) {
$errors[]= 'you forgot to enter email address.';
} else {
$e = trim($_POST['email']);
}

//check the passwords match
if(!empty($_POST['password1'])) {
if($_POST['password1'] != $_POST['password2']) {
$errors[] = 'Your password did not match the confirmed password.';
} else {

$p = trim($_POST['password1']);
}
} else {
$errors[] = 'You forgot to enter your password.';
}

if(empty($errors)) { // if everythings okay.

//register the user in the database.
require_once('../mysql_connect.php'); // connect to the db

//Make the query
$query = "INSERT INTO users (first_name, last_name, email, password, registration_date)
VALUES ('$fn', '$ln', '$e', SHA('$p'), NOW())";
$result = @mysql_query ($query); //run query.

if($result) { // if it ran ok

//send an email, if desired

//print message
echo '<h1 id="mainhead">Thank you!</h1>
<p>You are now registered.</p><p>
</p>';

//include the footer and quit the script (to not show the form).
include ('.includes/footer.html');
exit();

} else {

echo '<h1 id="mainhead">Systems Error</h1>
<p class="error">you could not be registered due to a system error</p>';
echo '<p>' . mysql_error(). '

Query: ' .$query . '</p>';
include ('./includes/footer.html');
exit();
}

mysql_close(); // close the database connection.

} else { //Report the errors

echo '<h1 id="mainhead">Error!</h1>
<p class="error"> The following error(s) occurred:
';
foreach($errors as $msg) { //print each error.
echo " - $msg
\n";
}

echo '</p><p>Please try again.</p><p>
</p>';

} // End of if (empty($errors)) IF.

}// End of the main Submit conditional.

?>

<h2>Register</h2>
<form action=" " method="post">
<p>First Name: <input type="text" name="first_name" size="15" maxlength="15" value="<?php if
(isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>

<p>Last Name: <input type="text" name="last_name" size="15" maxlength="30" value="<?php if
(isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>

<p>Email Address: <input type="text" name="email" size="15" maxlength="30" value="<?php if
(isset($_POST['email'])) echo $_POST['email']; ?>" /></p>

<p>Password: <input type="password" name="password1" size="10" maxlength="20" /></p>

<p>Confirm Password: <input type="password" name="password2" size="10" maxlength="20" /></p>

<p><input type="submit" name="submit" value="Register" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
include ('.includes/footer.html');
?>

Link to comment
https://forums.phpfreaks.com/topic/92908-register/#findComment-475941
Share on other sites

i created the file register.php with this code

 

<?php

 

$page_title = 'Register';

include ('./includes/header.html');

 

//check if the form has been submitted.

if(isset($_POST['submitted'])) {

 

$errors = array(); // initialize error array.

 

//Check first name

if (empty($_POST['first_name'])) {

$errors[]= 'You forgot to enter your first name.';

} else {

$fn = trim($_POST['first_name']);

}

 

//Check last name

if (empty($_POST['last_name'])) {

$errors[]= 'You forgot to enter your last name.';

} else {

$ln = trim($_POST['last_name']);

}

 

//Check email address

if (empty($_POST['email'])) {

$errors[]= 'you forgot to enter email address.';

} else {

$e = trim($_POST['email']);

}

 

//check the passwords match

if(!empty($_POST['password1'])) {

if($_POST['password1'] != $_POST['password2']) {

$errors[] = 'Your password did not match the confirmed password.';

} else {

 

$p = trim($_POST['password1']);

}

} else {

$errors[] = 'You forgot to enter your password.';

}

 

if(empty($errors)) { // if everythings okay.

 

//register the user in the database.

require_once('../mysql_connect.php'); // connect to the db

 

//Make the query

$query = "INSERT INTO users (first_name, last_name, email, password, registration_date)

VALUES ('$fn', '$ln', '$e', SHA('$p'), NOW())";

$result = @mysql_query ($query); //run query.

 

if($result) { // if it ran ok

 

//send an email, if desired

 

//print message

echo '<h1 id="mainhead">Thank you!</h1>

<p>You are now registered.</p><p>

</p>';

 

//include the footer and quit the script (to not show the form).

include ('.includes/footer.html');

exit();

 

} else {

 

echo '<h1 id="mainhead">Systems Error</h1>

<p class="error">you could not be registered due to a system error</p>';

echo '<p>' . mysql_error(). '

 

Query: ' .$query . '</p>';

include ('./includes/footer.html');

exit();

}

 

mysql_close(); // close the database connection.

 

} else { //Report the errors

 

echo '<h1 id="mainhead">Error!</h1>

<p class="error"> The following error(s) occurred:

';

foreach($errors as $msg) { //print each error.

echo " - $msg

\n";

}

 

echo '</p><p>Please try again.</p><p>

</p>';

 

} // End of if (empty($errors)) IF.

 

}// End of the main Submit conditional.

 

?>

 

 

 

 

the other is just called tryme.php that has the other in it

 

 

<h2>Register</h2>

<form action="register.php" method="post">

<p>First Name: <input type="text" name="first_name" size="15" maxlength="15" value="<?php if

(isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>

 

<p>Last Name: <input type="text" name="last_name" size="15" maxlength="30" value="<?php if

(isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>

 

<p>Email Address: <input type="text" name="email" size="15" maxlength="30" value="<?php if

(isset($_POST['email'])) echo $_POST['email']; ?>" /></p>

 

<p>Password: <input type="password" name="password1" size="10" maxlength="20" /></p>

 

<p>Confirm Password: <input type="password" name="password2" size="10" maxlength="20" /></p>

 

<p><input type="submit" name="submit" value="Register" /></p>

<input type="hidden" name="submitted" value="TRUE" />

</form>

<?php

include ('.includes/footer.html');

?>

 

Link to comment
https://forums.phpfreaks.com/topic/92908-register/#findComment-475956
Share on other sites

instead of

//Make the query
$query = "INSERT INTO users (first_name, last_name, email, password, registration_date)
VALUES ('$fn', '$ln', '$e', SHA('$p'), NOW())";
$result = @mysql_query ($query); //run query.

 

try:

//Make the query
$query = "INSERT INTO users (first_name, last_name, email, password, registration_date)
VALUES ('$fn', '$ln', '$e', SHA('$p'), ".time().")";
$result = @mysql_query ($query) or die("Query: ".$query."<br>".mysql_error()); //run query.

 

two changes: instead of now() you should use time() to insert a unix timestamp to record dates/times in a db.

second i added a type of error catcher. see what it spits out.

 

hope this helps,

Link to comment
https://forums.phpfreaks.com/topic/92908-register/#findComment-475982
Share on other sites

i removed the NOW and did  that echo'text here' thing and nothing happend :(

 

if i changed the require(mysql thing) to include it doesnt work either could it be the mysql_connect file?

 

<?php

 

DEFINE ('DB_USER', 'people_co_uk');

DEFINE ('DB_PASSWORD', 'password');

DEFINE ('DB_HOST', 'localhost');

DEFINE ('DB_NAME', 'people_co_uk');

 

$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die ('Could not connect to MySQL: ' . mysql_error() );

 

@mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );

?>

Link to comment
https://forums.phpfreaks.com/topic/92908-register/#findComment-475990
Share on other sites

$result = @mysql_query ($query) or die("Query: ".$query."<br>".mysql_error()); //run query.

 

If you can find another way to test if the Register is happening...

 

Remove the $result var set, and just run it as shown.

mysql_query($query) or die("Query: ".$query."<br>".mysql_error()); //run query.

 

 

Link to comment
https://forums.phpfreaks.com/topic/92908-register/#findComment-476002
Share on other sites

yer the database is set up and table called users

 

and the info is right to connect to the database it just wont save anything

 

 

Also if i take it back to how it use to be but split up, as soon as you click register it displays you to the header.html page thing, and when i go on phpmyadmin site there isnt any new fields.

Link to comment
https://forums.phpfreaks.com/topic/92908-register/#findComment-476031
Share on other sites

Might I suggest using strings instead of DEFINE

 

 

<?php

 

$db_user = 'people_co_uk';

$db_pass = 'password';

$db_host = 'localhost';

$db_db = 'people_co_uk';

 

$dbc = mysql_connect ($db_host, $db_user, $db_pass) or die ('Could not connect to MySQL: ' . mysql_error() );

 

@mysql_select_db ($db_db) OR die ('Could not select the database: ' . mysql_error() );

?>

 

By the error it seems to be coming from your connection to the DB

Link to comment
https://forums.phpfreaks.com/topic/92908-register/#findComment-476035
Share on other sites

this is what i have so far, i dont get any errors when i click register but nothing is being saved

 

mysql_connect.php

 

<?php

 

$db_user = 'people_co_uk';

$db_pass = 'password';

$db_host = 'localhost';

$db_db = 'people_co_uk';

 

$dbc = mysql_connect ($db_host, $db_user, $db_pass) or die ('Could not connect to MySQL: ' . mysql_error() );

 

@mysql_select_db ($db_db) OR die ('Could not select the database: ' . mysql_error() );

?>

 

 

 

 

 

 

 

 

 

 

tryme.php

 

 

<h2>Register</h2>

<form action="register.php" method="post">

<p>First Name: <input type="text" name="first_name" size="15" maxlength="15" value="<?php if

(isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>

 

<p>Last Name: <input type="text" name="last_name" size="15" maxlength="30" value="<?php if

(isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>

 

<p>Email Address: <input type="text" name="email" size="15" maxlength="30" value="<?php if

(isset($_POST['email'])) echo $_POST['email']; ?>" /></p>

 

<p>Password: <input type="password" name="password1" size="10" maxlength="20" /></p>

 

<p>Confirm Password: <input type="password" name="password2" size="10" maxlength="20" /></p>

 

<p><input type="submit" name="submit" value="Register" /></p>

<input type="hidden" name="submitted" value="TRUE" />

</form>

<?php

include ('.includes/footer.html');

?>

 

 

 

 

 

 

 

 

 

 

 

 

 

register.php

 

<?php

 

$page_title = 'Register';

include ('./includes/header.html');

 

//check if the form has been submitted.

if(isset($_POST['submitted'])) {

 

$errors = array(); // initialize error array.

 

//Check first name

if (empty($_POST['first_name'])) {

$errors[]= 'You forgot to enter your first name.';

} else {

$fn = trim($_POST['first_name']);

}

 

//Check last name

if (empty($_POST['last_name'])) {

$errors[]= 'You forgot to enter your last name.';

} else {

$ln = trim($_POST['last_name']);

}

 

//Check email address

if (empty($_POST['email'])) {

$errors[]= 'you forgot to enter email address.';

} else {

$e = trim($_POST['email']);

}

 

//check the passwords match

if(!empty($_POST['password1'])) {

if($_POST['password1'] != $_POST['password2']) {

$errors[] = 'Your password did not match the confirmed password.';

} else {

 

$p = trim($_POST['password1']);

}

} else {

$errors[] = 'You forgot to enter your password.';

}

 

if(empty($errors)) { // if everythings okay.

 

//register the user in the database.

require_once('../mysql_connect.php'); // connect to the db

 

//Make the query

$query = "INSERT INTO users (first_name, last_name, email, password, registration_date)

VALUES ('$fn', '$ln', '$e', SHA('$p'), NOW())";

$result = mysql_query ($query); //run query.

 

if($result) { // if it ran ok

 

//send an email, if desired

 

//print message

echo '<h1 id="mainhead">Thank you!</h1>

<p>You are now registered.</p><p>

</p>';

 

//include the footer and quit the script (to not show the form).

include ('.includes/footer.html');

exit();

 

} else {

 

echo '<h1 id="mainhead">Systems Error</h1>

<p class="error">you could not be registered due to a system error</p>';

echo '<p>' . mysql_error(). '

 

Query: ' .$query . '</p>';

include ('./includes/footer.html');

exit();

}

 

mysql_close(); // close the database connection.

 

} else { //Report the errors

 

echo '<h1 id="mainhead">Error!</h1>

<p class="error"> The following error(s) occurred:

';

foreach($errors as $msg) { //print each error.

echo " - $msg

\n";

}

 

echo '</p><p>Please try again.</p><p>

</p>';

 

} // End of if (empty($errors)) IF.

 

}// End of the main Submit conditional.

 

?>

 

 

 

 

What program do you use to type in php by the way

Link to comment
https://forums.phpfreaks.com/topic/92908-register/#findComment-476051
Share on other sites

revraz, the @mysql_select_db is fine I've never removed it in any of my scirpts.

 

stanleybb,

You don't have the script terminating on fail again.

 

in register.php

 

Rewrite:

$result = mysql_query ($query); //run query

 

To:

$result = mysql_query ($query) or die($query.'<br>'.mysql_error()); //run query

 

Link to comment
https://forums.phpfreaks.com/topic/92908-register/#findComment-476063
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.