Jump to content

Whats Wrong with this code.


Flowdy

Recommended Posts

I'm new to php and just reading a book learning how it works etc but i cant seem to figure whats wrong with this code. I keep getting

[b]Parse error: parse error in c:\program files\easyphp1-8\www\registration.php on line 25[/b]

This is my PHP Code
[quote]<?php

/*
Has the user submitted data?
If not, display the registration form.
*/
if (! isset($_POST['submitbutton'])) {
    echo file_get_contents("C:\Program Files\EasyPHP1-8\www");

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

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

   mysql_select_db("corporate");

  /* 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 insert 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 successful!</P>";
         }
      } catch(Exception $e) {
          echo "<p>".$e->getMessage()."</p>";
      } #endCatch
    }
  }
?>[/quote]

any my html code

[code]<html>
<body>

<form method="post" action="registration.php">
Name:<br>
<input type="text" name="name">
<br>
Email Address:<br>
<input type="text" name="email">
<br>
Username:<br>
<input type="text" name="username">
<br>
Password:<br>
<input type="password" name="password">
<br>
Verify Password:<br>
<input type="password" name="Vpassword">
<br>
<br>
<input type="submit" value="Register">
</form>
</body>
</html>[/code]


Link to comment
https://forums.phpfreaks.com/topic/28501-whats-wrong-with-this-code/
Share on other sites

Missing closing double quote in your query string:

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



Note: You should also protect your db from sql injection.
$query = "INSERT INTO userauth (commonname, email, username, pswd)
                  VALUES ('$_POST[name]', '$_POST[email]',
                  '$_POST[username]', md5('$_POST[pswd]'"));

should be

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

i think
Sorry for my first misplace of the double quote  ::)
In most cases you should also use quotes inside the post definitions.

Try this:
[code]

<?php

if(!isset($_POST['submitbutton']))
{
echo file_get_contents("C:\Program Files\EasyPHP1-8\www");
}
else
{
$conn = mysql_pconnect("localhost", "corpweb", "secret");
mysql_select_db("corporate");

if ($_POST['password'] != $_POST['Vpassword'])
{
echo "<p>The passwords do not match. Please go back and try again.</P>";
}
else
{
try
{
$name = mysql_real_escape_string(stripslashes($_POST['name']));
$email = mysql_real_escape_string(stripslashes($_POST['email']));
$username = mysql_real_escape_string(stripslashes($_POST['username']));
$pass = mysql_real_escape_string(stripslashes($_POST['password']));
$pass = md5($pass);

$result = mysql_query("INSERT INTO userauth (commonname, email, username, pswd) VALUES ('$name', '$email', '$username', '$pass')") or die(mysql_error());

if (!$result)
{
throw new Exception("Registration problems were encountered!");
}
else
{
echo "<p>Registration was successful!</p>";
}
}
catch(Exception $e)
{
echo "<p>".$e->getMessage()."</p>";
}
}
}

?>

[/code]

You must also name your submitbutton --> name="submitbutton"
I tired the code you posted but now is saying

[b]Parse error: parse error in c:\program files\easyphp1-8\www\registration.php on line 19[/b]

I was using my book as reference and in there it didnt have "submitbutton" it just had 'submitbutton'
if you want to sniff for submitbutton (as you do) you will have to name it. Else it won't be a posted variable so it will neither be set (isset())

Do you add something else in the code when you try it ??

and the part:
[color=blue]echo file_get_contents("C:\Program Files\EasyPHP1-8\www");[/color]
This doesn't refer to a file, just a directory....

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.