0

I have a form via from i register the users, the code of the form is as below:

    <div class="contentArea">
<form action="NewUser.php" method="POST">
<div class="regd">
<ul>
    <li class="regdTitle">Please enter your details:</li>
    <li class="regdLable">First Name</li>
    <li><input type="Text" class="regdInput" name="first_name" /></li>
    <li class="regdLable">Last Name</li>
    <li><input type="Text" class="regdInput" name="last_name" /></li>
    <li class="regdLable">Password</li>
    <li><input type="Password" class="regdInput" name="pass" /></li>
    <li class="regdLable">Re-Type Password</li>
    <li><input type="Password" class="regdInput" name="re_pass" /></li>
    <li class="regdLable">Email</li>
    <li><input type="Text" class="regdInput" name="email" /></li>
    <li class="regdLable">Country</li>
    <li><select name="country" style="width: 300px; height: 20px;">
        <option value=" " selected="selected"></option>
        <option value="ACT">ACT</option>
        <option value="NSW">NSW</option>
        <option value="NT">NT</option>
        <option value="QLD">QLD</option>
        <option value="SA">SA</option>
        <option value="TAS">TAS</option>
        <option value="VIC">VIC</option>
        <option value="WA">WA</option>
    </select></li>
    <li><input type="button" value="Submit" class="regdBtn" alt="Submit"
        title="Submit" name="submit"/></li>
    <li><input type="button" value="Cancle" class="regdBtn" alt="Cancle"
        title="Cancle" onclick="location.href='index.php'" /></li>
</ul>
</div>
</form>
</div>

and i have NewUser.php file via from i insert the values of the form to database, code of which is as below:

<?php
// Includes
require_once ('Includes/dbconn.php');
require_once ('Includes/functions.php');

$first_name = $_POST ['first_name'];
$last_name = $_POST ['last_name'];
$pass = $_POST ['pass'];
$repass = $_POST ['re_pass'];
$email= $_POST ['email'];
$country = $_POST ['country'];


$query = "INSERT INTO user (first_name, last_name, pass, email, country)
            VALUES ('{$first_name}','{$last_name}','{$pass}','{$repass}','{$email}','{$country}')";

$result = mysql_query ( $query, $conn );

if (isset ( $result )) {
    echo "Registration sucessful";
    //redirect_to ( UserRegd.php );
} else {
    echo "<p>User Registration Failed" . mysql_error () . "</p>";
}

?>

<?php mysql_close($conn);?>

Connection code is as below:

<?php
    include_once 'Constants/ConConst.php';

    $conn = mysql_connect(DB_SERVER, DB_USER, DB_PASS);

    if(!$conn)
    {
        die('Connection not set'. mysql_error());
    }

    $db = mysql_select_db(DB_NAME, $conn);

    if(!$db)
    {
        die('Database not found'.mysql_error());
    }

?>

I m unable to insert the form values into my database...May i know where is bug in my code.

any meaningful response will be highly appreciated and thanks in advance. and is there any online editor like jsfiddle.com where we can put our code for demonstration.

6
  • 1
    The first thing you should be doing is to do put die($query) in your code to see what your actual SQL statement looks like. Do that and then post the results here Commented Apr 13, 2011 at 15:02
  • You should use PDO instead of the old methods for contacting a mysql database - net.tutsplus.com/tutorials/php/… Commented Apr 13, 2011 at 15:07
  • 2
    Your query seems vulnerable to SQL injection - please make sure to address this before the code goes live :) Commented Apr 13, 2011 at 15:09
  • +1: and prepared statements with PDO will address injection dangers :D Commented Apr 13, 2011 at 15:11
  • I m a beginner in php and dont know much about it.. i.e SQL Injection and code optimization in php... will learn it later... Commented Apr 13, 2011 at 15:13

4 Answers 4

2

Your INSERT statement is wrong. You are trying to pass too many values. {repass} should not be in the VALUES() list.

This is correct:

$query = "INSERT INTO user (first_name, last_name, pass, email, country)
            VALUES ('{$first_name}','{$last_name}','{$pass}','{$email}','{$country}')";
Sign up to request clarification or add additional context in comments.

1 Comment

I had kept the repass out... I thought it might had caused the error.. other wise i am validating the pass and re-pass in client side...
1
if (isset ( $result )) { <= THIS LINE BEING WRONG

it should be just

if ($result) {

Comments

0

Well one problem may be that you are trying to insert more VALUES then you have specified fields for.

right now it sets
first_name=$first_name
last_name=$last_name
pass=$pass
email=$repass
country=$email
???=$country

So that's one problem
The second could be your formatting of the SQL
it should be:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
So you don't need the {}.
I'm not sure about the commas.

Also check by echo ing the values for the variables to make sure that your form is submitting correctly.

read this for more help:
http://www.w3schools.com/sql/default.asp

I checked a database I had made:

//send the information to the database
// sql string to send to the database
$sql = "INSERT INTO ".$dbname.".table (First Name, Last Name) VALUES ('".$first_name."', '".$last_name."');";

Which for the VALUES is ' " . $var . " ', So you can see better what that says

// query the database
$result = mysql_query($sql);

You need to use the correct concatenation of the variables.
Make sure you use `, not '. for the column names and around the db name and table name
It is to the left of the 1.

You also hadn't queried correctly.
Try something like mine, it should work and validate correctly.

I noticed people talking about not being protected against SQL injection etc,
check you server settings/features, most now automatically prevent thest security risks.

If not, try XAMPP, as it does.

6 Comments

also what Col. Shrapnel said is true.
@Richard i had fixed that... what @Col Shrapnel suggested
I had tried to echo the vars before insert but the echo statement is not executing.. $first_name = $_POST ['first_name']; $last_name = $_POST ['last_name']; $pass = $_POST ['pass']; $email= $_POST ['email']; $country = $_POST ['country']; echo '$first_name'. '$last_name'. '$pass'.'$email'. '$country'; that means the error is in form which is not supplying the values on click of submit. But i thing the form is correct...
ah your form was almost right, but it should be type="submit", not type="button". Then it will echo correctly. Well actually you don't need the 's. just echo $first_name.$last_name.etc sorry I didn't reply sooner
@Richard Oh! atlast this might be true... anyways thank you in advance as i am going to check my code backhome, as i am in office right now. Hope we will be in touch. Thank you once again...
|
0

Lookat your query INSERT INTO user (first_name, last_name, pass, email, country) VALUES ('{$first_name}','{$last_name}','{$pass}','{$repass}','{$email}','{$country}')" they are five values (first_name, last_name, pass, email, country) and they are 6 values ('{$first_name}','{$last_name}','{$pass}','{$repass}','{$email}','{$country}')" :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.