VoteMeUp
VoteMeUp

Reputation: 15

Error on submit form

Why i can't insert on my database? This is my code

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$bdate = $_POST['bdate'];
$id = $_POST['id'];

if (!empty($name) && !empty($email)){
    mysql_query("insert into customer(id,name,email,bdate) values($id,'$name','$email','$bdate')");
}
else {
    echo "Please fill <strong>name</strong> and <strong>e-mail</strong> fields!";
}

?>

My form is

<form method="post" action="new_customer.php">
    ID: <input type="text" name="id" size="5" value="<?php echo $new_id; ?>" disabled="disabled" /><br />
    Name: <input type="text" name="name" size="50" /><br />
    E-mail: <input type="text" name="email" size="30" /><br />
    Birth Date: <input type="text" name="bdate" size="10" /><br /><br />
    <input type="submit" value="Save" />
</form>

The var $new_id is filled with this query

SELECT (Max(id) + 1) as new_id FROM customer

But when I submit the form, the error i get is:

Notice: Undefined index: id in F:\UniServer\www\admin\customer.php on line 6

and then a mysql error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' '')' at line 1

i think the first error causes the secund, right?

Upvotes: 0

Views: 100

Answers (4)

Quasdunk
Quasdunk

Reputation: 15220

Instead of disabled="disabled" you want to use readonly="readonly". This prevents the field from being edited, but the value can still be read programatically (which is not the case with disabled).

Upvotes: 0

Md Jawed Shamshedi
Md Jawed Shamshedi

Reputation: 314

Please provide the code of mysql that is fetching records, as there might be an error in assignment of table id value to php value.

Upvotes: 0

matino
matino

Reputation: 17725

Because disabled fields are not present in $_POST array.
In your case make id field auto increment in the database and make a query without it:

mysql_query("INSERT INTO customer(name, email, bdate) values('$name', '$email', '$bdate')"); 

Also your code is subject to SQL injection - use mysql_real_escape_string before the query:

$name = mysql_real_escape_string($_POST['name']);

Upvotes: 2

Arfeen
Arfeen

Reputation: 2623

remove " disabled="disabled" " ...

This is stopping the form to post the field to your PHP.

Upvotes: 0

Related Questions