Sinisterly
Database insertion error - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: PHP (https://sinister.li/Forum-PHP)
+--- Thread: Database insertion error (/Thread-Database-insertion-error)



Database insertion error - devendra - 04-30-2014

I am trying to create a simple registration form
but when I click to submit button data isn't inserting in database.
Plz can anyone help me regarding this.
Thank you.

My registration.html file
Code:
<!-- To change this template, choose Tools | Templates and open the template in the editor. --> <!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <div> <form action="index.php" method="post" name="reg"> <table style="width:100%"> <tr><td style="30%"> Name:</td> <td style="70%"><input type="text" name="name"></td> </tr> <tr><td style="30%">Email:</td> <td style="70%"><input type="text" name="mail"></td> </tr><tr><td style="30%">Password:</td> <td style="70%"><input type="text" name="pass"></td> </tr><tr><td style="30%"></td> <td style="70%"><input type="submit" value="submit"></td> </tr> </table> </form> </div> </body> </html>

My php file
Code:
<?php Include ('connect.php'); if(isset($_POST['submit'])!="") { if($_POST['name']==''|| $_POST['email']==''||$_POST['pass']=='') { echo "Please fill the following"; } else { $sql="insert into student(name,email,pass) values('".$_POST['name']."','".$_POST['email']."','".$_POST['pass']."')"; $res=mysql_query($sql); if($res) { echo "values inserted"; } else { echo "insertion error"; } } mysql_close("db"); } ?>

my connect file

Code:
<?php $host="localhost"; $user="dave"; $pass="pass"; mysql_connect($host,$user,$pass); mysql_select_db("db"); ?>

here is table structure
with id auto increment

[Image: image.jpg]
Thank You.


RE: Database insertion error - levi01 - 04-30-2014

This code looks fine, what is the structure of your SQL table?


RE: Database insertion error - devendra - 04-30-2014

(04-30-2014, 10:48 AM)levi01 Wrote: This code looks fine, what is the structure of your SQL table?

uploaded table structure.


RE: Database insertion error - dropzon3 - 04-30-2014

Can you tell us what the actual error message is? The error codes and messages exist for a reason.

http://www.php.net/manual/en/function.mysql-error.php

EDIT: Is `id` an auto-increment row, since you're not providing a value and your structure image doesn't show it as having a default value that could be your issue.


RE: Database insertion error - zomgwtfbbq - 04-30-2014

Did you use mysql_error() to see what error is returned?

MySQL extension is deprecated anyway, better switch to PDO or you'll end up writing the whole site sooner or later..


RE: Database insertion error - levi01 - 04-30-2014

Like @dropzon3 said use this value:

Code:
`id` int(11) NOT NULL AUTO_INCREMENT,



RE: Database insertion error - Ligeti - 05-07-2014

Your image/structure shows (id, name, email, password)
your code: $sql="insert into student(name,email,pass)

Also I would recommend that you validate the email/passwords/names ... etc. using regex! (there are tons of example online)

Peace


RE: Database insertion error - Ex094 - 05-08-2014

You've given the code for the php file, I couldn't stop noticing the fact the you've given the index file to the form action:

Code:
<form action="index.php" method="post" name="reg">

If you have kept the PHP code in a file say add.php then I think the above line should be:

Code:
<form action="add.php" method="post" name="reg">

Because when you'll press the Submit button it'll redirect you to the action page since you've put the index.php hence it goes again to the page and does nothing because your index page has no PHP to be processed.

Complete For Index:

Code:
<!-- To change this template, choose Tools | Templates and open the template in the editor. --> <!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <div> <form action="add.php" method="post" name="reg"> <table style="width:100%"> <tr><td style="30%"> Name:</td> <td style="70%"><input type="text" name="name"></td> </tr> <tr><td style="30%">Email:</td> <td style="70%"><input type="text" name="mail"></td> </tr><tr><td style="30%">Password:</td> <td style="70%"><input type="text" name="pass"></td> </tr><tr><td style="30%"></td> <td style="70%"><input type="submit" value="submit"></td> </tr> </table> </form> </div> </body> </html>



RE: Database insertion error - troy_odyssey34 - 05-28-2014

I don't know why but I find when the code you have:
Code:
$sql="insert into -->student(name,email,pass)<-- values('".$_POST['name']."','".$_POST['email']."','".$_POST['pass']."')";
when you have those brackets there where you have the table name, it doesn't like to insert into the DB I would just remove it so that it looks like this:
Code:
$sql="INSERT INTO student VALUES('','".$_POST['name']."','".$_POST['email']."','".$_POST['pass']."')"
As long as all of the columns have a place in the VALUES then it should insert fine. I hope this was helpful. Good luck! :Grin: