Jump to content

how to insert data into database


leegreaves

Recommended Posts

At the moment im creating a "registration" page using php. At the start ive created my initial connection to the mysql database; the html body of it begins underneath this, my conundrum is where should i put the code that actually enters the data into the database at. Should this also be placed at the beginning of my page or should it go after the html section. I do know I was told b4 that any php should go at the beginning of the page before any html code, so from this i must assume that it should go at the start as well. Also, after connecting, what code do i use to insert the "entry" into my database. would be VERY grateful for all pointers.

What command do i use to insert this "data" if i know what that is then i should be able to look at existing examples and be able to work out how to implement it in my page.

Link to comment
https://forums.phpfreaks.com/topic/177401-how-to-insert-data-into-database/
Share on other sites

Generally speaking insertion should be done at the top of the script. Especially if the insertion could effect the contents of the HTML. How you insert it will depend on the type of database you are using. Most people use MySQL so you would be using the mysql_query function with an SQL statement along the lines of...

 

INSERT INTO table (column1, column2) VALUES ('item 1', 'item 2')

yer i am using mysql for the database, my form is using the "post" method to collect the data...so i just need to work out how to transfer the data from the form to my database. So, ok, i do have to put that instruction prior to the html form that ive written. Im guessing in the example u gave above:

 

Column 1, Column 2 must be the table structure for example if they were the username and password then they would be named username and password

and under the "Values", item 1 and item 2, etc, would be the actual data that has to be inserted. Im guessing I would replace column (1,2,etc) and item (1,2,etc) with the actual names/id's i have in my html form (I hope im guessing right or in that direction)

 

My table has names/id's in them in the form:

 

<input type="text" id="Editbox1" name="username" value="">

 

So the column in this case would be "username" referring to the field in the database but would the value be the item cos im assuming the value is the username that would be enterend on the form itself?

 

I hope im getting a little bit of this right.

The first set after the table name should match the columns in the database that you are inserting. The values would be the values from your form. The values from your for are stored in the $_POST array, using a key that matches the name attribute of the HTML inputs. So if you have...

 

<input type="text" id="Editbox1" name="username" value="">

 

The value is accessed through the script using $_POST['username']. The values are put in the sql statement in the second set of brackets (following VALUES) in the same order as the column names. You should also read up on the mysql_real_escape_string() in order to prevent SQL Injection attacks.

OK im understading that a little bit there cags. Though im not sure a bit about where the $_POST['username'] or whatever item goes in between the brackets bit comes in. And yes, thorpe, I will take your advice and read the hudzilla link in your signature.

 

Is there anywhere on the forum where i can take a look at existing examples and adjust them to my needs on here, if not do you know of any good sites, cos when i try searching for them i tend to come up with a list of not so useful sites

Is there anywhere on the forum where i can take a look at existing examples and adjust them to my needs on here, if not do you know of any good sites, cos when i try searching for them i tend to come up with a list of not so useful sites

 

I gave you  a link to a free book.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.