Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting new data into database.

Status
Not open for further replies.

jxfish2

Technical User
Jan 24, 2002
183
US
2 questions:

1) What command do I use, to commit new data, entered into an online form, to the database?

i.e. If the PHP code, is located inside of the HTML form document, how do I keep the PHP code from executing, until after the form has been completed, and a button of some type is pressed? (Like a submit button)... Or do I NEED to put the PHP insert commands, inside of an externally called script?

2) In the on-line PHP documentation, it lists a whole bunch of MySQL commands, but all apparently are for querying the database... I do not see any that would allow for inserting of data into a database from the web-based front-end... Are there any specific PHP / MySQL commands, that would insert or update data in the database?

TIA

Joe
 
sql commands are issued to insert data.

you use insert and update.

insert syntax is:

insert INTO [table_name] (column_name1, ...) VALUES (value1, ...);

update is:

update [table_name] (col_name1, ...) VALUES (value1, ...) WHERE [whatever condition you stipulate];

you need the php connect statement to connect to the db, and the execute statement to execute the query.

Finally, your php can either call itself or an external script. Your choice, but in both cases it would use a form-input button to trigger the call. The table name, colums, etc, can be passed to the script though <input hidden> statements and the variables through <input text> or <select> statements.

There are lots of examples snippets of code on the web that you can look to for examples.
 
Hi Kubla,

Thanks for responding, but I don't really need the SQL commands...

I need the PHP commands, that will allow me to perform an update or insert...

I need the PHP commands that will allow me to select a system and database to work with, and I need the PHP commands that will allow me to execute a SQL command...

TIA

Joe F.
 
----------------

I need the PHP commands, that will allow me to perform an update or insert...

I need the PHP commands that will allow me to select a system and database to work with, and I need the PHP commands that will allow me to execute a SQL command...
----------------

Well, you can read up what you'll need to do in many sources, but essentially, you need to connect to the db with the php mysql_connect function:


then you'll need to send the query with the mysql_db_query function:


Good luck!


 
what i like to do is to create a page with a form that references itself. this way i control when the script executes and only have to maintain one page. i start with
checking to see if a form variable is present and then either submit the mysql function to insert the data or call the form and write it to the page. like this:

<?
if ((!$fname) && (!lname)){
//if the form variables are not present call the form
call GenerateForm();
} else {
//insert the data
require(&quot;dbconn.php&quot;);
//include file that holds the connection string to the DB
//insert the data
$InsertData = &quot;insert into TableName values ($fname,$lname)&quot;;
$myResult = mysql_query($InsertData, $dbconn);

function GenerateForm(){
echo &quot;<html><head><title></title></head><body>
<form name = myform method = post action = thispage.php>
<table><tr><td>First Name></td>
<td><input name = fname size = 20 type = text></td>
<td>Last Name></td>
<td><input name = lname size = 20 type = text></td>
</form></table></body></html>&quot;;
}
?>

it's rough but this should give you the idea needed.

hth

Bastien

There are many ways to skin this cat,
but it still tastes like chicken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top