0

Hi I am working on a simple php registration. But everytime I am submitting the registration page, i am getting a blank screen, no error, no display.

The code in my php file is :

<?php

if($_SERVER['REQUEST_METHOD']=="POST"){
$IP = //my hostname
$dbuser = "my user id";

$conn = new mysqli_connect($IP, $dbuser, "","my databse name");
if(! $conn )
{
  die('Could not connect: ' . mysqli_error());
}

$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
$address = $_POST['address'];


$query = "SELECT email FROM user where email='".$email."'";
$result = mysqli_query($conn,$query);
$numResults = mysqli_num_rows($result);

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) // Validate email address
        {
            $message =  "Invalid email address please type a valid email!!";
        }


        elseif($numResults>=1)
        {
            $message = $email." Email already exist!!";
        }


else
        {
            mysqli_query("(insert into user(name,phone_number, email,pass1, pass2, address) values
            ('".$name."','".$phone."', '".$email."', '".$pass1."','".$pass2."','".$address."')");


            echo $message = "Signup Sucessfully!!";
        }


mysqli_close($conn);

}

print_r(error_get_last());

?>

there is no issue in establishing connection as i am using this connection method in other pages and they are working fine. Also i should specify that currently i am working on cloud 9 and my mysql database is on cloud9 itself.

please help me in undersstanding the trouble.

ritika
  • 61
  • 1
  • 10
  • 1
    Use error reporting, error_reporting(E_ALL); ini_set('display_errors', 1); – Mahadeva Prasad Sep 29 '14 at 08:39
  • use at the top of the code to view the error – Nitin Kaushal Sep 29 '14 at 08:39
  • If there's a syntax error on your page, the above tips won't work. – KIKO Software Sep 29 '14 at 08:42
  • echo your Insert query before run and check for syntax error. – TBI Sep 29 '14 at 08:43
  • after adding this codes, i am getting this error: "Fatal error: Class 'mysqli_connect' not found in /home/ubuntu/workspace/signup_code.php on line 9" line 9 has the following : $conn = new mysqli_connect($IP, $dbuser, "","my database name"); – ritika Sep 29 '14 at 08:44
  • @KIKOSoftware what is the syntax error. I just cant put my finger on it...please direct – ritika Sep 29 '14 at 08:46
  • you dont need to use new keyword in connection string. You can use - mysqli_connect('localhost','username','password'); mysqli_select_db("dbname"); – TBI Sep 29 '14 at 08:47
  • @user1290121: You got a nice error message, so you didn't have a syntactical problem. – KIKO Software Sep 29 '14 at 08:56
  • well i actually used that because someone suggested that, anyways after removing the "new" i am getting the following error: "Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/ubuntu/workspace/signup_code.php on line 42 Signup Sucessfully!!Array ( [type] => 2 [message] => mysqli_query() expects at least 2 parameters, 1 given [file] => /home/ubuntu/workspace/signup_code.php [line] => 42 )" line 42 has the insert query – ritika Sep 29 '14 at 08:57
  • Try to use this - mysqli_query($conn,"Your insert query"); This query will include your connection string. – TBI Sep 29 '14 at 09:01
  • yeah, i fixed that...thanks for the help...i deserve downvotes honestly, a stupid mistake – ritika Sep 29 '14 at 09:08

3 Answers3

1

You say your line 42 has the insert query. I suppose you mean this query:

mysqli_query("(insert into user(name,phone_number, email,pass1, pass2, address) values
        ('".$name."','".$phone."', '".$email."', '".$pass1."','".$pass2."','".$address."')");

As your error says "2 parameters expected", you are missing here your connection parameter. You should have this:

mysqli_query($conn, "(insert into user(name,phone_number, email,pass1, pass2, address) values
        ('".$name."','".$phone."', '".$email."', '".$pass1."','".$pass2."','".$address."')");
0

Put this in on the first line of your script

ini_set('display_errors', '1');

This will then display the errors on the page and you will be able to see what is going wrong.

Also rather than the IP address try "localhost"

$conn = new mysqli_connect("localhost", $dbuser, "","my databse name");

If the database is on the same machine this may solve your problem.

0

Also your code is incredibly unsafe and will suffer from SQL injection. Please consider the following lines:

$email = $_POST['email'];

$query = "SELECT email FROM user where email='".$email."'";

Basically you put whatever the user is passing into your SQL statement. What if he enters "' OR 1; DROP TABLE user". You would lose all your data.

Please read about SQL injection and use PDO:

olvlvl
  • 2,396
  • 1
  • 22
  • 22
  • No need to escape the `$email` because you are using doublequotes `"` at the start and end of your query, nor is this an awnser to OP's Question. – Azrael Sep 29 '14 at 08:48
  • thanks for the suggestion. i will surely implement it. but right now i am just trying to learn something and thsi blank screen is not helping me at all...:( – ritika Sep 29 '14 at 08:52
  • You do need to escape `$email`, it's one of the cases explained on the Wikipedia page. You can use `mysqli::real_escape_string`, or better yet prepared statements: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php – olvlvl Sep 29 '14 at 08:54
  • 1
    Sorry about that. Should I delete my post ? – olvlvl Sep 29 '14 at 09:55