Enki
Enki

Reputation: 1

PHP won't update MySQL tables

I have a "Windows Apache MySQL PHP" server on my laptop. I get absolutely no error messages, but when I send things to MySQL via PHP script, nothing happens in MySQL (I also sent something via the MySQL command prompt and it looks like it just made a test table and didn't put anything in it like I asked, but I'm not positive I did everything I should have in this case). I've rescripted my whole page a different way and it still doesn't work.

Here is a picture of my form and how the table looks in MySQL:

http://tinypic.com/view.php?pic=21kjgno&s=5

System:

Windows 7 Home Premium SP1 64 bit-- Apache 2.4.4 32 bit with ssl0.9.8-- PHP 5.4.11 32 bit VC9-- MySQL 5.5.31 64 bit with Navicat Lite

<?php  
define('DB_HOST', 'localhost');
define('DB_NAME', 'projectedin');
define('DB_USER','root');
define('DB_PASSWORD','');

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " .mysql_error());

function NewUser()
{
    $userName = $_POST['username'];
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $password =  $_POST['password'];
    $birthday =  $_POST['birthday'];
    $gender =  $_POST['gender'];
    $query = "INSERT INTO users (username,firstname,lastname,password,birthday,gender) VALUES ('$username','$firstname','$lastname','$password','$birthday','$gender')";
    $data = mysql_query ($query)or die(mysql_error());
    if($data)
    {
        echo "YOUR REGISTRATION IS COMPLETED...";
    }
}

function SignUp()
{
    if(!empty($_POST['username']))   //checking the 'user' name which is from Sign-Up.html, is it empty or have some text
    {
    $query = mysql_query("SELECT * FROM users WHERE username = '$_POST[username]' AND password = '$_POST[password]'") or die(mysql_error());

    if(!$row = mysql_fetch_array($query) or die(mysql_error()))
    {
        newuser();
    }
    else
    {
        echo "SORRY...YOU ARE ALREADY REGISTERED USER...";
    }
    }
}
if(isset($_POST['submit']))
{
    SignUp();
}
mysqli_close ($con);
?>  

Upvotes: 0

Views: 1173

Answers (2)

Ignacio Ocampo
Ignacio Ocampo

Reputation: 2713

You have an error in your Signup query:

$query = mysql_query("SELECT * FROM users WHERE username = '$_POST['username']' AND password = '$_POST['password']'") or die(mysql_error());

I changed $_POST[username] to $_POST['username'] and $_POST[password] to $_POST['password']

Also, you are vulnerable for MySQL injections, you should use mysql_real_scape_string function to clean each $_POST var: $userName = mysql_real_scape_string($_POST['username']);

Finally, you should not use mysql* functions, they are deprecated.

Upvotes: 0

Mike Brant
Mike Brant

Reputation: 71384

There are a number of concerns here (more on that later), but this might be an issue for you:

You try to call your new user function like this:

newuser();

However it is named NewUser. This is case sensitive and will not work. Look at your error logs to see the errrs you are getting here.

Other issues:

  • You are using deprecated mysql_* functions. If you are learning PHP, learn the right way and use mysqli or PDO.
  • You are not escaping your input at all, and are therefore very prone to SQL injection attacks.
  • You are kind of randomly using functions when they don't really bring you any value.
  • You are kind of going outside of typical PHP coding standard when having your function names start with uppercase letter. This is a bit unusual for PHP, though this would actually work.

Upvotes: 2

Related Questions