0

This is the page code after clicked register button.Config.php works for db connection. It totally works . I have tried same mail and username already registered , it checks and not allow to retake it. so I think the problem start on $sql insertion line .There is no error showed up on php. It shows my function fail().

mysql_query($sql) was mysql_query($sql,$conn) before but I wanted to try if Its the problem however looks it isnt.

note: it was working on another hosting. I moved everything. Since , it stopped working.

Thank you.

   <head>
       <script>
          function done() {
              alert("Registration has been successful");
          }
      </script>
      <script>
          function fail() {
              alert("Registration has been unsuccessful,Please try again");
          }
      </script>
       <script>
          function exist_username() {
              alert("Username already has taken, please try again.");
          }
      </script>
      <script>
          function exist_mail() {
              alert("You already has been registered.Please sign in.");
          }
      </script>
   </head>

   <body>

<?php

         include("config.php");

         if(isset($_POST['connect'])) 
  {
            if($conn)
      {
                  $value=$_POST['name'];
                $value2=$_POST['email'];
                $value3=$_POST['password'];
              $value4=$_POST['username'];

              $check_username=mysql_query("SELECT username FROM member WHERE username='$value4'");
              $check_mail=mysql_query("SELECT email FROM member WHERE email='$value2'");

           if(mysql_num_rows($check_username)!=0)
               {
                      echo '<script type="text/javascript">',
                           'exist_username();',
                           '</script>';
                           $URL="mywebsite.com";
                      echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $URL . '">';
                      echo "<script type='text/javascript'>document.location.href='{$URL}';</script>";
               }
           if (mysql_num_rows($check_mail)!=0) 
              {
                 echo '<script type="text/javascript">',
                           'exist_mail();',
                           '</script>';
                           $URL="http://mywebsite.com";
                      echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $URL . '">';
                      echo "<script type='text/javascript'>document.location.href='{$URL}';</script>";

              }
            else
                {

                    $sql = "INSERT INTO member(name,email,password,username,registertime) VALUES('$value','$value2', '$value3', '$value4', CURRENT_TIMESTAMP )";

                    $retval = mysql_query($sql);

                    if($retval)
                    {
                    echo '<script type="text/javascript">',
                         'done();',
                         '</script>';
                         $URL="http://mywebsite.com";
                    echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $URL . '">';
                    echo "<script type='text/javascript'>document.location.href='{$URL}';</script>";

                    }
                    else
                    {
                     echo '<script type="text/javascript">',
                         'fail();',
                         '</script>';
                             $URL="http://mywebsite.com";
                    echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $URL . '">';
                    echo "<script type='text/javascript'>document.location.href='{$URL}';</script>";
                    }
                }
      }
                   else
                   {
                    die('Could not connect: DB ' . mysql_error());
                   }
  }

         ?>
            </body>
</html>
b166er
  • 19
  • 5
  • #1. Enable [`error_reporting(E_ALL);`](http://php.net/manual/en/function.error-reporting.php) and check your logs. #2. `mysql_*` functions are deprecated since PHP 5.5 (and **removed entirely** in PHP 7) and you should [stop using them](http://stackoverflow.com/q/12859942) if you can. You should choose another API that allows you to use prepared statements (which you *really should* when dealing with user-input), like `mysqli_*` or PDO - see [choosing an API](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Qirel Sep 19 '16 at 22:35
  • And looking at your logic, the INSERT will only happen if `if (mysql_num_rows($check_mail)!=0)` fails. You could optimise your query by selecting `WHERE username='$var' OR email='$othervar'`, and just have one query. – Qirel Sep 19 '16 at 22:37
  • ...and naming variables such as `$value1`, `$value2` and so forth is considered bad practice, and can easily get confusing and messy! – Qirel Sep 19 '16 at 22:40
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Sep 19 '16 at 22:40
  • and the way you do your redirects (echoing a js-script after an alert box) is quite ...old fashioned... Have a look at ajax! That's the way such user-interactions are done nowadays. – Jeff Sep 19 '16 at 22:41
  • 1
    _it was working on another hosting. I moved everything. Since , it stopped working._ Could this be because your new hosting is using PHP7 where the `mysql_` API no longer exists? – RiggsFolly Sep 19 '16 at 22:41
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Sep 19 '16 at 22:42
  • You dont need 4 seperate ` – RiggsFolly Sep 19 '16 at 22:43

0 Answers0