0

I am trying to write a simple PHP script:

  1. A user visits a webpage and registers their email address and a password via a form (no other iinputs required).
  2. Their email and password are stored in a database and a random unique 6 digit pin is generated and stored against the email. The pin is checked against existing pins in the database to ensure it is unique.
  3. The pin is then displayed on the webpage and shown to the user following confirmation of their email and password submission.

TL;DR users sign up and get a unique pin.

I have the following PHP code, but it doesn'nt seem to execute on my WAMP server. It just returns the .php pages as raw text. Am I missing some obvious syntax or configuration?

<?
if($_POST['srSubmit'] && $_POST['srEmail'] && $_POST['srPass']) {
    $conn = mysql_connect('localhost','','');
    mysql_select_db("db_test",$conn);
    while(1) {
        $pin = rand(111111,999999);
        $sel = mysql_query("SELECT * FROM formusers WHERE pin = '$pin'");
        if(mysql_num_rows($sel) != 0) { continue; }

        mysql_query("INSERT INTO formusers(email,password,pin) VALUES('".$_POST['srEmail']."','".$_POST['srPass']."','".$pin."')");
        if(mysql_affected_rows()!=-1)  {
            echo "Pin:" . $pin;
            exit;
        } else {
            echo "Existing email, try again<br />";
        }
        break;
    }

}
?>
<form method="POST" action="">
<input type="email" name="srEmail" value="" placeholder="Email" /><br />
<input type="password" name="srPass" value="" placeholder="Password" /><br />
<input type="submit" name="srSubmit" value="Register" />
</form>
alias51
  • 8,178
  • 22
  • 94
  • 166
  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Amal Murali Jul 22 '13 at 18:05

3 Answers3

2

The opening tag for php does not look right:

<?
if($_POST['srSubmit'] && $_POST['srEmail'] && $_POST['srPass']) {

should be:

<?php
if($_POST['srSubmit'] && $_POST['srEmail'] && $_POST['srPass']) {
immulatin
  • 2,118
  • 1
  • 12
  • 13
  • Probably the cause -- short tags ( ?>) are not enabled by default. – Jacob S Jul 22 '13 at 18:10
  • 1
    Also please don't use short tags because they are evil! :D Since 5.4 there is a new concept in PHP: use long opening tags: ``. The only "short tag" that is enabled by default ([it is NOT affected](http://php.net/manual/en/language.basic-syntax.phpmode.php) by the `short_open_tag` setting) is the echo tag: `= ... ?>`. – Borislav Sabev Jul 23 '13 at 08:04
1

Short PHP tags (<? ?>) are not enabled by default and that's why your code isn't working.

Change your code to use the full PHP tags () for consistency over different development environments.

<?php // <-- notice the difference

if($_POST['srSubmit'] && $_POST['srEmail'] && $_POST['srPass']) {

But, if you still want to enable short tags, you can do so by modifying the short_open_tag directive in your php.ini file, like so:

short_open_tag = on

For reasons of compatibility, but it's recommended to avoid using it anyway. Read this answer to understand the differences.

Hope this helps!

Community
  • 1
  • 1
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

1) Place 'php' after the "<" and "?" at the beginnning

problem persists?

2) Sounds like a configuration thing, as if the server doesnt see your php as a php it has to execute, but as text it has to display. Before I ask if youre sure the php module is loaded, is it a new installation of WAMP? It happened to me a long time ago, and i remember it was a little configuration thing. I also remembering finding the answer somewhere around the web ;)