Jump to content

PHP register form can't access mySQL database


bicoo_2010

Recommended Posts

Hello every body

 

I'm a beginner for PHP and i make a registration form  and i can't connect to mysql database.

Please i need help for this.

 

register form:

 

 

 

<html>

<head>

<script language="javascript">

function button_actions(){

// User Name

if(!document.register_form.u_name.value){

alert('Please Enter User Name');

document.register_form.u_name.focus();

return false;}

 

// Create Password

if(!document.register_form.c_pass.value){

alert('Please Enter Your Password');

document.register_form.c_pass.focus();

return false;}

 

// Retype Password

if(!document.register_form.r_pass.value){

alert("Please fill in your password again for confirmation");

document.register_form.r_pass.focus();

return false;}

 

// Retype Password

if(document.register_form.c_pass.value != document.register_form.r_pass.value){

alert("Your Password Don't Match, Please Retype Your Password");

document.register_form.r_pass.focus();

return false;}

 

// First Name

if(!document.register_form.f_name.value){

alert('Please Enter First Name');

document.register_form.f_name.focus();

return false;}

 

// Last Name

if(!document.register_form.l_name.value){

alert('Please Enter Last Name');

document.register_form.l_name.focus();

return false;}

 

// Address

if(!document.register_form.address.value){

alert('Please Enter Address');

document.register_form.address.focus();

return false;}

 

// E-mail

if(!document.register_form.email.value){

alert('Please Enter E-mail');

document.register_form.email.focus();

return false;}

}

</script>

</head>

<body>

<form name="register_form" action="reg.php" method="post"  onsubmit="return validate()">

    User name          <input type="text" name="u_name" size="38" maxlength="25"/>

<br/>

<br/>

Create Password      <input type="text" name="c_pass" size="40" maxlength="20" />

<br/>

<br/>

Retype Password    <input type="text" name="r_pass" size="40"maxlength="20"/>

            <br/>

<br/>

Frist name    <input type="text" name="f_name" size="40" maxlength="25""/>

<br/>

<br/>

Last name   <input type="text" name="l_name" size="40"maxlength="25"/>

<br/>

<br/>

Address       <input type="text" name="address" size="40" maxlength="50"/>

<br/>

<br/>

E-mail       <input type="text" name="email" size="40" maxlength="50"  />

<br/>

<br/>

<br/>

</body>

</html>

 

 

 

and my php form to connect to database in wamp server by PHPmyadmin

reg.php

 

 

<?php

 

$con= mysql_connect("localhost","root","" );

$db = mysql_select_db("test",$con);

 

 

$u = $_POST['u_name'];

$c_pass = $_POST['c_pass'];

$r_pass = $_POST['r_pass'];

$frist = $_POST['f_name'];

$last = $_POST['l_name'];

$address = $_POST['address'];

$email = $_POST['email'];

 

$query=INSERT INTO users(u_name,c_pass,r_pass,f_name,l_name,address,email) VALUES ('$u','$c_pass','$r_pass','$frist','$last','$address','$email');

mysql_query($query);

if($query){

echo "You have successfully Registration";

}

else

{

echo "you hasn't compelete registration. Please check your enterd data";

}

mysql_close($con);

 

?>

 

Best Regards,,,

 

Link to comment
Share on other sites

You say you can't connect but these are the only two lines which do the connection:

 

$con= mysql_connect("localhost","root","" ); 
$db = mysql_select_db("test",$con);

 

Is the connection failing or is it the later code.  You can help spot where errors occur by adding "or die" to mysql functions so you see the errors.  Something like:

 

$con= mysql_connect("localhost","root","" ) or die ("Error connecting to mysql server: ".mysql_error());
$db = mysql_select_db("test",$con);

 

If you add that and don't get an error then the problem comes later.  Add the "or die" bits to all your other mysql functions but with different messages for each so you can spot where the error came from.

 

Oh, that will only work if error reporting is on but with wamp it will be.

Link to comment
Share on other sites

What Pikachu is trying to point out is that without your help we don't know which line is line 15!

 

Also, if you had told us that error in your first post I would have ignored you subject and spotted the problem.  You ARE connected to the database so there is no problem there.

 

The problem is with line 15 of your PHP which is (I think):

 

$query=INSERT INTO users(u_name,c_pass,r_pass,f_name,l_name,address,email) VALUES ('$u','$c_pass','$r_pass','$frist','$last','$address','$email');

 

which should have quotes around the query:

 

$query="INSERT INTO users(u_name,c_pass,r_pass,f_name,l_name,address,email) VALUES ('$u','$c_pass','$r_pass','$frist','$last','$address','$email')";

Link to comment
Share on other sites

the line 15 this is the query , I attached the php code and i put double quotations on query but php given this errors:

 

Notice: Undefined index: u_name in C:\wamp\www\myweb\reg.php on line 7

 

Notice: Undefined index: c_pass in C:\wamp\www\myweb\reg.php on line 8

 

Notice: Undefined index: r_pass in C:\wamp\www\myweb\reg.php on line 9

 

Notice: Undefined index: f_name in C:\wamp\www\myweb\reg.php on line 10

 

Notice: Undefined index: l_name in C:\wamp\www\myweb\reg.php on line 11

 

Notice: Undefined index: address in C:\wamp\www\myweb\reg.php on line 12

 

Notice: Undefined index: email in C:\wamp\www\myweb\reg.php on line 13

You have successfully RegistrationPHP Notice: Undefined index: u_name in C:\wamp\www\myweb\reg.php on line 7 PHP Notice: Undefined index: c_pass in C:\wamp\www\myweb\reg.php on line 8 PHP Notice: Undefined index: r_pass in C:\wamp\www\myweb\reg.php on line 9 PHP Notice: Undefined index: f_name in C:\wamp\www\myweb\reg.php on line 10 PHP Notice: Undefined index: l_name in C:\wamp\www\myweb\reg.php on line 11 PHP Notice: Undefined index: address in C:\wamp\www\myweb\reg.php on line 12 PHP Notice: Undefined index: email in C:\wamp\www\myweb\reg.php on line 13

 

[attachment deleted by admin]

Link to comment
Share on other sites

Those are all NOTICE level errors. The script ran successfully. To clear those errors up, check that each element from the $_POST array exists before trying to use it. That should all be done while the form is being validated anyhow.

Link to comment
Share on other sites

i have a the first php error:

 

Parse error: parse error in C:\wamp\www\myweb\reg.php

PHP Parse error: parse error in C:\wamp\www\myweb\reg.php

 

in query

 

$query="INSERT INTO users values("$_POST[u_name]","$_POST[c_pass]","$_POST[r_pass]","$_POST[f_name]","$_POST[l_name]","$_POST[address]","$_POST")";

 

Link to comment
Share on other sites

You can't use the same quotes inside a quoted string without escaping them. This is what's causing the current error.

$string = "This is a \"string\" of words";  // Valid
$string = 'This is a \'string\' of words'; // Valid
$string = "This is a 'string' of words'; // Valid
$string = 'This is a "string" of words'; // Valid
$string = "This is a "string" of words"; // Not valid
$string = 'This is a 'string' of words'; // Not valid

 

You need to validate the form, and escape or otherwise sanitize the data for every field before it even gets to the the query.

if( !empty($_POST['name']) ) {
     $name = mysql_real_escape_string($_POST['name']);
} else {
     $errors[] = 'Name is a required field';
}

 

If your table doesn't have exactly the same number of fields as the number you're trying to insert, you have to explicitly list the field names, in the order corresponding to the data that follows, in the query string.

Link to comment
Share on other sites

The list of field names in an insert query are only required if you don't provide a value for every column.

 

bicoo_2010, sorry to be blunt, but you seem to be just randomly changing code for no good reason and without knowing why it should or should not be a certain way. It will take you a very long time to produce any code that works using this method.

 

Just changing code in a desperate effort to eliminate error messages without finding out why it was not working in the first place does not lead to any learning and you will end up making the same basic mistake over and over.

 

You must learn the reason why you are doing any particular thing. For example, the first problem of not putting double-quotes at the start and end of your query string. You do have quotes around the other strings in your code. Quotes must be around the query string as well in order to make it a string.

 

Edit: And I just tried the form code you posted and the form is incomplete and cannot possible be submitting at all to your reg.php script. The first step in this process is the form and it must be tested and work first.

Link to comment
Share on other sites

The list of field names in an insert query are only required if you don't provide a value for every column.

 

 

That's what I said. "If your table doesn't have exactly the same number of fields as the number you're trying to insert . . . ", which implies if the fields aren't listed, you need to provide values for all of them.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.