Anders
Anders

Reputation: 15

Basic , Sending data to database, PHP

I'm new to PHP and so far I've managed to connect to my database, but when I try to send data to the database I don't get any script errors, but the data doesn't go through and I receive the message "Succesfully Created User!", which basically means the data has been sent right?

Here is my script:

<?php
$user = $_POST['user'];
$name = $_POST['name'];
$pass = $_POST['password'];
$con = mysql_connect("fdb13.awardspace.net", "myusername", "mypassword") or die(mysql_error());
if(!$con)
    die('Could not connectzzz: ' . mysql_error());
mysql_select_db("myusername" , $con) or die ("could not load the database" . mysql_error());

$check = mysql_query("SELECT * FROM unitytut WHERE `user`='".$user."'");
$numrows = mysql_num_rows($check);
if($numrows == 0)
{
    $pass = md5($pass);

    //$ins = "INSERT INTO unitytut (user, name, pass) VALUES ('John', 'Doe', 'John');";

        //$ins =  ("INSERT INTO `unitytut` (`user`), (`name`), (`pass`) VALUES ('' , 'John', 'Doe', 'John''John', 'Doe', 'John') ; ");

            $ins =  ("INSERT INTO `unitytut` (`user`), (`name`), (`pass`) VALUES ('' , '".$user."' , '".$name."', '".$pass."') ; ");


   if($ins)
        die("Succesfully Created User!");

    else
        die("ERROR");

}
else
{
    die("user already exists!");
}

?>

I've tried several insert methods, but the only 3 functions that didn't receive the "ERROR" message was:

$ins = mysql_query("INSERT INTO unitytut (`user`), (`name`), (`pass`) VALUES ('' , '$user' , '$name', '$pass')" ) ;

and

$ins =  ("INSERT INTO `unitytut` (`user`), (`name`), (`pass`) VALUES ('' , 'John', 'Doe', 'John''John', 'Doe', 'John') ; ");

and

$ins =  ("INSERT INTO `unitytut` (`user`), (`name`), (`pass`) VALUES ('' , '".$user."' , '".$name."', '".$pass."') ; ");

I also have an ID in my table, but as I've been researching this issue without managing to fix it, I also read that I don't need to specify the ID in the script as the user is getting an ID automatically, but as I haven't gotten this to work I wouldn't know for sure.

I'm very new to php and databases in general, but I've been stuck on this for a while now, so any help would be greatly appreciated :)

Also any feedback on the way I posted this is also welcome, did I give you too much information, too many questions in one post, too much text etc.?

Thanks!

Upvotes: 0

Views: 54

Answers (1)

DannyTheDev
DannyTheDev

Reputation: 4173

You should format your INSERT statement like this:

$ins = mysql_query("INSERT INTO unitytut (`user`,`name`,`pass`) VALUES ('$user' , '$name', '$pass')" ) ;

However, mysql_query is deprecated, See this link and follow it's advice on using a newer extension

Upvotes: 3

Related Questions