Jump to content

web form data to database


princesohrab
 Share

Recommended Posts

Hi all,Im am trying to use a web form to send data to a table called "people" in my database using php.The script store the first time you submit the for and the data goes to the correct table too. The problem is it only allows you to do it only once meaning only 1 entry is registrered. If I try again, I get error message. Then i delete the entry from the database, I refresh the form, send again and it registers. I just need to know how to make it register more than one person's details. Below is the code

<?phpif (isset($_POST['submitted'])) {include('w-hill-reg-form-database-connect.php');$fname = $_POST['fname'];$lname = $_POST['lname'];$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";if (!mysqli_query($dbcon, $sqlinsert)) {  die ('Error inserting new record');}$newrecord = "New record added";}?><html><head><title>CALLING ALL TENNIS PLAYERS!</title></head><body><form method="post" action="w-hill-registration-form-php.php"><input type="hidden" name="submitted" value="true" /><fieldset><legend>New People</legend>    <label>First Name: <input type="text" name="fname" /></label>    <label>Last Name: <input type="text" name="lname" /></label></fieldset><br/><input type="submit" value="Add new person" /></form><?phpecho $newrecord // New record added statement added at the top?></body></html>

Link to comment
Share on other sites

What error message are you getting? How is your table is structured? If your table have primary key or unique key in any columns. InSerting duplicate entry to those column will throw mysql error. You can use mysqli_error() to know the actual mysql message

Link to comment
Share on other sites

I have created the table with 3 columns..peopleidfirstnamelastnameI believe I have them all as primary key.. (I'm not home, on the way to work)Which one should be primary key?Only peopleid?? peopleid is auto increment and was defined as medium integer..The code seems fine.. I think it's to do with the structure if the table..I'm not sure..I will reset a table and please let me know which one to use as primary.Thanks

Link to comment
Share on other sites

A primary key means that you can only have 1 record with that value. If you try to add another record that has a value for a primary key that is already in the table then that will be an error. Like birbal said, you should be checking for errors with MySQL and printing the error message so that you can know what the problem is instead of needing to guess.

  • Like 1
Link to comment
Share on other sites

What error message are you getting? How is your table is structured? If your table have primary key or unique key in any columns. InSerting duplicate entry to those column will throw mysql error. You can use mysqli_error() to know the actual mysql message
Hi Birbal,In my table called "people" I created through MySql,I have 3 columns:peopleid (auto increment)firstnamelastname which one should be primary key? At the moment,peopleid and firstname fields are as primary key. Im trying to set only the peopleid as primary but for some reason it wont let me.
Link to comment
Share on other sites

which one should be primary key?
That depends on what your requirements are. A primary key is an index that requires unique values in a field. If your first name is not unique, then should it be a primary key?
At the moment, peopleid and firstname fields are as primary key.
It seems strange to have a multi-part key that includes an autoincrement field, I'm not sure what the point of that would be. If I set up a table with an autoincrement integer field and a varchar field, and make them both part of the primary key, it never violates the uniqueness because the autoincrement field is different for each row. I'm not sure how you set up your table, but you should edit the primary index to remove the other columns.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...