problem with database connection

Codes here !

Moderators: macek, egami, gesf

jam
New php-forum User
New php-forum User
Posts: 1
Joined: Mon May 20, 2013 6:19 pm

problem with database connection

Postby jam » Mon May 20, 2013 6:29 pm

hey guys,

I made a simple registration form, everything went perfectly except that when I click submit it doesn't 't shows the string :
successful connection

and it doesn't add the user name ,pass and email to the database. I checked the code and phpmyadmin many times and I don't what went wrong.


this is my code:

Code: Select all

<html>
 
<head>
<title>my Rigesteration</title>   
</head>       
   
<form method = "post"  action  ="myRigesteration.php">
   
   
<table wedth = "400"  border = "4"  align = "center" 
 
<tr width="380">
<td colspan ="5"><h1>Registration Form</h1></td>                           
</tr>       

<tr>
<td width="140">User Name:</td>       
<td width="218"><input type = "text"  name = "name" size="30"/></td>                       
</tr>   

<tr>
<td width="140">User Password:</td>       
<td width="218"><input type = "password"  name = "pass" size="30"/></td>                       
</tr>                       
</tr>   

<tr>
<td width="140">Email:</td>       
<td width="218"><input type = "text"  name = "email" size="30"/></td>                       
</tr>   

<tr>     
<td colspan ="5" align ="center" ><input type = "submit"  name = "submit"  value = "Sign Up"/></td>                       
</tr>   


<table>
   
</html>


<?php
$db_host = "localhost";
$db_username = "root";
$db_pass = "1234";
$db_name = "user_db";
mysql_connect("$db_host","$db_username","$db_pass");
mysql_select_db("$db_name");
if (isset($_POST["submit"]))
{
 $user_name = $_POST['name'];
 $user_pass = $_POST['pass'];
 $user_email = $_POST['email'];
 
if ($user_name == "")
{
echo "<script>alert('please enter your name')</script>";   
exit();
}
if ($user_pass == "")
{
echo "<script>alert('please enter your password')</script>"; 
exit();
}
if ($user_email == "")
{
echo "<script>alert('please enter your email')</script>";
exit();
}

$check_email = "select * from users where user_email = '$user_email'";
$run = mysql_query($check_email);

if (mysql_num_rows($run) > 0)
{
echo "<script>alert('Email $user_email is already exists in our database please try another one')</script>";     
exit();   
}
$query = "insert into users(user_name,user_password,user_email) values ('$user_name',$user_pass','$user_email')";
if(mysql_query($query))
{
echo "<script>alert('registration successful')</script>";     
   
}
}

?>


apkhan2
New php-forum User
New php-forum User
Posts: 4
Joined: Fri May 17, 2013 11:57 pm

Re: problem with database connection

Postby apkhan2 » Fri May 24, 2013 9:58 am

Correct the Query cause you have forgot to give single quote in $user_pass

Code: Select all

$query = "insert into users(user_name,user_password,user_email) values ('$user_name',$user_pass','$user_email')";

To

Code: Select all

$query = "insert into users(user_name,user_password,user_email) values ('$user_name','$user_pass','$user_email')";

And how you expect to to show "successful connection" by writing "registration successful" ???


Return to “mySQL & php coding”

Who is online

Users browsing this forum: Alexa [Bot] and 1 guest