Hi guys.I am new to web development and of course I have been experiensing some problems.Most of them are easy to solve and I usually find my way, but there is some thing that is getting on my nerves for some time now.

I have been trying to make a simple script for registering a user via MySQL.No filtering input, no validating only the part that adds the new user into the MySQL table. But it doesn't work.I have checked my code a couple of times and i can't see the problem.Here it is:

<?php
if(isset($_POST["name"]) && isset($_POST["pass"]) && isset($_POST["email"]))
{
$name=$_POST["name"];
$pass=$_POST["pass"];
$email=$_POST["email"];
$con=mysql_connect('localhost','root','password');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("users", $con);
mysql_query("INSERT INTO members (username,password,email) values ('$name','$pass','$email')") or die('Error');
}
?>

Recommended Answers

All 35 Replies

try this:

<?php
if(isset($_POST["name"]) && isset($_POST["pass"]) && isset($_POST["email"]))
{
$name=$_POST["name"];
$pass=$_POST["pass"];
$email=$_POST["email"];
$con=mysql_connect('localhost','root','password');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("users", $con);
$sql = "INSERT INTO `members` (username,password,email) VALUES ('" . $name . "','" . $pass . "','" . $email . "')";
$query = mysql_query($sql,$con) or die('Error: ' . mysql_error());
}
?>

if this script doesn't fix the problem, post the mysql error message and i will help you from there.

Hey.Thanks for the quick reply there mate.I am sorry you didn't get the same from me but I was unable to reply sooner.Unfortunatelly the problem remains.The worst part is that a SQL error doesn't even appear.All I get after clicking on "submit" is my page reloading blank. . .

post the rest of your code so i can see if there is a problem there.

<?php
if(isset($_POST["name"]) && isset($_POST["pass"]) && isset($_POST["email"]))
{
$name=$_POST["name"];
$pass=$_POST["pass"];
$email=$_POST["email"];
$con=mysql_connect('localhost','root','password');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("users", $con);
$sql = "INSERT INTO `members` (username,password,email) VALUES ('" . $name . "','" . $pass . "','" . $email . "')";
$query = mysql_query($sql,$con) or die('Error: ' . mysql_error());
}
?>

<html>
<body>
<form action="register.php" method="post">
Username: <input type="text" name="name" /></br>
Password: <input type="password" name="pass" /></br>
Email: <input type="text" name="email" width=40 /></br>
<input type="submit" />
</form>
</body>
</html>

OK.Here it is mate... My code with your corections.I would really appreciate if u find a solution.I have been trying for a few days now.

Is it inserting the data into the database.Of course,it will display blank because you have not triggered something to display if it is inserted correctly.try to replace this part:

$query = mysql_query($sql,$con) or die('Error: ' . mysql_error());

with this:

$query = mysql_query($sql);
if  (!$query) 
{
echo 'Error: ' . mysql_error();
}
else
{
echo 'Inserted correctly.';
}

Hey ryan. I am sorry to say that it didn't work.I Inserted the statements you proposed, but still I get a blank.No error, no nothing.Oh Isn't the page supposed to display the form again if the insert is correct,because the page is reloading?

change this line:

if(isset($_POST["name"]) && isset($_POST["pass"]) && isset($_POST["email"]))

to this:

if ($_POST['Submit'])

then add a name in your form button

<input type="submit" name="Submit" />

See if that helps.

By the way,is this page the register.php page?

Still no work mate. Again blank page.And to you question... yes this is named register.php :)
By the way I am signing off for now going to school. Again any help is appresiated.

Try this..

<?php
if(isset($_POST['submit'])){
	if(!empty($_POST["name"]) && !empty($_POST["pass"]) && !empty($_POST["email"]))
	{
		$name=$_POST["name"];
		$pass=$_POST["pass"];
		$email=$_POST["email"];
		$con=mysql_connect('localhost','root','password');
		if (!$con)
		{
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db("users");
		$sql = "INSERT INTO members (username,password,email) VALUES ('$name','$pass','$email')";
		$query = mysql_query($sql) or die('Error: ' . mysql_error());
		echo "Inserted successfully !";
	} else {
		echo "Fields can't be empty !";
	}
}
?>
<html>
<body>
<form action="register.php" method="post">
Username: <input type="text" name="name" /></br>
Password: <input type="password" name="pass" /></br>
Email: <input type="text" name="email" width=40 /></br>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>

Ok guys I am back. nav33n thanks for this man, but unfortunatelly It still doesnt work.The blank page remains and I am am getting no error messages at all. Something stops the scripts from executing but I have no idea what...

Umm.. echo statements to check the flow of the code. Check if its entering the loops correctly. Check the form variable names. Put print_r($_POST); on top of the page to see if all the form variables are posted properly.

hie

my fujitsu siemens amilo pro v3515 laptop with vista home basic has been displaying windows resuming when i switch it on ..been going on for 2 days...cannot get any of the keys to do anything...can anyone help?????.. thanx

hie

my fujitsu siemens amilo pro v3515 laptop with vista home basic has been displaying windows resuming when i switch it on ..been going on for 2 days...cannot get any of the keys to do anything...can anyone help?????.. thanx

You need to post this question in hardware forum.

Since it's the only place left with no error detection, how about trying:

if (!mysql_select_db("users", $con))
die ("Database 'users' not found");

Ok thanks to all your effort guys we have an improvement.I tried what nav33n said and I put echo statements everywhere. The flow of the code stops right after $con=mysql_connect('localhost','root','password'); . Ok so far so good. What now? Where might be the problem?

hmm! instead of $con=mysql_connect('localhost','root','password'); give $con=mysql_connect('localhost','root','password') or die(mysql_error()); I dont think establishing the connection is the problem. If that was the problem, you would have got an error message while executing the script. Anyway, change the connection string line as above and try again !

Try opening a command window and check if you can connect with the root user and 'password' as a password:

> mysql -u root -p
Enter password: password

(make sure the mysql command is in your path or else navigate where it is)

nav33n mate I tried with the die statement and there is no error but the code still stops after the attempt to connect. And LeBurt there is no problem connecting to mysql the way you said.
I have double checked everything I input but it still doesn't work.What could be the problem?

Thats strange. Put the connection string as 1st line of your code. Is it entering all the other conditions ?

I remember having connection problems when I started which I resolved by appending the mysql socket address to the host.

mysql_connect (host:socket, username, password)

I can't remember however if I got connection errors or not, but hey, it's worth the try. On my system:

$con = mysql_connect ('localhost:/var/mysql/mysql.sock', 'root', 'password');

Note that the location of the socket might be different on your system (I'm on OS X) but just locate the mysql.sock file and use the resulting path.

Hi

This works perfectly on my wampserver:

if(isset($_POST["name"]) && isset($_POST["pass"]) && isset($_POST["email"]))
{
$name=$_POST["name"];
$pass=$_POST["pass"];
$email=$_POST["email"];
$con=mysql_connect('localhost','user1','11424028');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("rand", $con);
$sql = "INSERT INTO `members` (username,password,email) VALUES ('" . $name . "','" . $pass . "','" . $email . "')";
$query = mysql_query($sql,$con) or die('Error: ' . mysql_error());
}
?>

<html>
<body>
<form method="post">
Username: <input type="text" name="name" /></br>
Password: <input type="password" name="pass" /></br>
Email: <input type="text" name="email" width=40 /></br>
<input type="submit" />
</form>
</body>
</html>

The form action was redirecting the page before the mysql statement was sent,
So i simply just took it out :D

If this doesnt work, i would suggest going over your database and making sure the fields and table names are exactly right

anyway, hope the above works m8.

Reece

sorry ignore the red above its with my database details .... heres the version with yours .....

if(isset($_POST["name"]) && isset($_POST["pass"]) && isset($_POST["email"]))
{
$name=$_POST["name"];
$pass=$_POST["pass"];
$email=$_POST["email"];
$con=mysql_connect('localhost','root','password');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("users", $con);
$sql = "INSERT INTO `members` (username,password,email) VALUES ('" . $name . "','" . $pass . "','" . $email . "')";
$query = mysql_query($sql,$con) or die('Error: ' . mysql_error());
}
?>

<html>
<body>
<form method="post">
Username: <input type="text" name="name" /></br>
Password: <input type="password" name="pass" /></br>
Email: <input type="text" name="email" width=40 /></br>
<input type="submit" />
</form>

hope it helps
reece

The result when I put the connection line 1st in my script is a blank page. Not even the form is displayed. I though It might be my mistake mispelling something but unless I am turning blind everything looks well.LeBurt and Desiner_101 I will try your suggestions in a minute and see if something works..

By the way thanks for all the help guys

Designer_101 I tried what you said to see if it works.Well I dont know why but it doesn't. But when I read LeBurt's post I am striked by an idea. Is MySQL database supposed to be installed in the same directory as Apache Server or PHP. And if it is could that be causing the problem?And LeBurt If MySQL is in X:/Program Files/MySQL and my Apache is well X:/Program Files/Apache how do I select MySQL directory via localhost?

hi
I think now the problem isnt your code, perhaps like you said a direcory issue.

Theres a peice of free software called
Wampserver 2.0

i highly recomend it, it is
-apache
-php
-mySQL
-phpMyADMIN

all in one and is so much easier to use.

download it from here

http://www.wampserver.com/en/download.php

just
download
install
and start designing

hope this helps

Actually, mysql is connect to PHP, not to Apache and no, they don't have to be in the same directory. The php.ini file on your system must have a line like this:

extension=mysql.so (or mysql.dll on windows)

(make you remove the semicolon to uncomment the line)

Otherwise, using the socket address should do it if there are still connection problems like on OS X...

So its a package instal and it instals everything without any hard to do properties and adjustments. Because once I tried to instal my admin and it was a nightmare. Anyway I will try it tomorrow.I am really tired today. Thanks a lot guys. After I try this tomorrow I will post to say if the problem remains.

Thanks again.

ok il pop on tomorow to see you.
Np
night night

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.