Jump to content

First try code help


Flowdy

Recommended Posts

This is the first time ive tried to code anything, A registration form...but it doesnt seem to work :(

 

First the HTML

<html>
<head>
<include ="registration.php">
<body>
<form>
<p>Name:<br>
<input type="text"  size="20"><br>
<p>Email Address:<br>
<input type="text" size="20"><br>
<p>Username:<br>
<input type="text" size="20"><br>
<p>Password:<br>
<input type="password" size="20"><br>
<p>Verify Password:<br>
<input type="password" size="20"><br>
<br>
<input type="Submit" value="Submit">


</form>
</body>
</html>

 

now the PHP

<?php

/*
Has user submitted data?
If not, display the registration form.
*/
if (! isset($_POST['submitbutton'])) {
echo file_get_contents("/templates/register.html");

/* Form data has been submitted. */
} else {

$conn = mysql_pconnect("localhost", "corpweb", "secret");

mysql_select_db("mygame");

/* Ensure that the password and password verifier match. */
if ($_POST['pswd'] != $_POST['pswdagain']) {
echo "<p>The passwords do not match. Please go back and try again.</p>";

/* Passwords match, attempt to inster information into userauth table. */
} else {

try {
$_query ="INSERT INTO userauth (commonname, email, username, pswd)
VALUES ('$_POST[name]', '$_POST[email]', '$_POST[username]', md5('$_POST[pswd]'));

$result = mysql_query($query);
if (! $result) {
throw new Exception(
"Registration problems were encountered!"
);
} else {
echo "<p>Registration was sucessful!</p>;
}
} catch(Exception $e) {
echo "<p>".$e->getMessage()."</p>";
} #endCatch
}
}
?>

 

Thanx for any help given

Link to comment
https://forums.phpfreaks.com/topic/78617-first-try-code-help/
Share on other sites

<include ="registration.php">

 

I've never seen that used before lol.

 

You need to POST the data to your PHP script,

 

where you have <form> change it to this:

 

<form name="registration" method="post" action="url to your PHP script">

Link to comment
https://forums.phpfreaks.com/topic/78617-first-try-code-help/#findComment-397823
Share on other sites

$_query ="INSERT INTO userauth (commonname, email, username, pswd)
VALUES ('$_POST[name]', '$_POST[email]', '$_POST[username]', md5('$_POST[pswd]'));

 

needs to be....

 

$_query ="INSERT INTO userauth (commonname, email, username, pswd)
VALUES ('{$_POST['name']}', '{$_POST['email']}', '{$_POST['username']}','" . md5($_POST['pswd']) . "'";

Link to comment
https://forums.phpfreaks.com/topic/78617-first-try-code-help/#findComment-397867
Share on other sites

Sorry here is the updated code

 

<?php

/*
Has user submitted data?
If not, display the registration form.
*/
if (! isset($_POST['submitbutton'])) {
echo file_get_contents("/templates/register.html");

/* Form data has been submitted. */
} else {

$conn = mysql_pconnect("localhost", "corpweb", "secret");

mysql_select_db("mygame");

/* Ensure that the password and password verifier match. */
if ($_POST['pswd'] != $_POST['pswdagain']) {
echo "<p>The passwords do not match. Please go back and try again.</p>";

/* Passwords match, attempt to inster information into userauth table. */
} else {

try {
$_query ="INSERT INTO userauth (commonname, email, username, pswd)
VALUES ('{$_POST['name']}', '{$_POST['email']}', '{$_POST['username']}','" . md5($_POST['pswd']) . "'";

$result = mysql_query($query);
if (! $result) {
throw new Exception(
"Registration problems were encountered!"
);
} else {
echo "<p>Registration was sucessful!</p>;
}
} catch(Exception $e) {
echo "<p>".$e->getMessage()."</p>";
} #endCatch
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/78617-first-try-code-help/#findComment-398023
Share on other sites

What version of PHP do you have installed? I see you're using try/catch statements these are only available with PHP5. PHP4 does not support these.

 

Also line 34 should be:

echo "<p>Registration was sucessful!</p>";

You forgot the " at the end of the line to close the string.

Link to comment
https://forums.phpfreaks.com/topic/78617-first-try-code-help/#findComment-398061
Share on other sites

You'll have to remove the try/catch statement then and use basic if/else statements instead:

<?php

/*
Has user submitted data?
If not, display the registration form.
*/
if (! isset($_POST['submitbutton']))
{
    echo file_get_contents("/templates/register.html");
/* Form data has been submitted. */
}
else
{
    $conn = mysql_pconnect("localhost", "corpweb", "secret");
    mysql_select_db("mygame");

    /* Ensure that the password and password verifier match. */
    if ($_POST['pswd'] != $_POST['pswdagain'])
    {
        echo "<p>The passwords do not match. Please go back and try again.</p>";

        /* Passwords match, attempt to inster information into userauth table. */
    }
    else
    {
        // never use raw _POST data within a query. Always escape user input. 
        // I suggest using mysql_real_escape_string to help prevent SQL Injection attacks.
        $name     = mysql_real_escape_string($_POST['name']);
        $email    = mysql_real_escape_string($_POST['email']);
        $username = mysql_real_escape_string($_POST['username']);

        $query  = 'INSERT INTO userauth (commonname, email, username, pswd) ';
        $query .= "VALUES ('$name', '$email', '$username','" . md5($_POST['pswd']) . "'";

        $result = mysql_query($query) or die('Registration problems were encountered!');

        echo "<p>Registration was sucessful!</p>";
    }
}

?>

That code should work as expected. Note I added a bit of extra code to help prevent SQL Injection attacks.

Link to comment
https://forums.phpfreaks.com/topic/78617-first-try-code-help/#findComment-398172
Share on other sites

Thanx for that  :) now am getting an error on line 9

 

am guessing its this part

echo file_get_contents("/templates/register.html");

 

as its were your file is stored right..and mine isnt stored in /templates/

mine is in

 

/home/poison/public_html/testphp/register.html

 

but when i put that in i still get the same error msg

 

Warning: file_get_contents(home/poison/public_html/testphp/register.html) [function.file-get-contents]: failed to open stream: No such file or directory in /home/poison/public_html/testphp/registration.php on line 9
Link to comment
https://forums.phpfreaks.com/topic/78617-first-try-code-help/#findComment-398200
Share on other sites

I'm still getting the same error msg

 

Warning: file_get_contents(/templates/register.html) [function.file-get-contents]: failed to open stream: No such file or directory in /home/poison/public_html/testphp/registration.php on line 9

 

This is going to sound stupid but is it cos i havent got a connect.php?

Link to comment
https://forums.phpfreaks.com/topic/78617-first-try-code-help/#findComment-398264
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.