473,779 Members | 2,062 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to insert data into databse when a button is clicked

2 New Member
hiii everybody,

i have a little code that inserts the data into the databse. but the problem is the insertion should be peroformed only when a button is clicked. i have problems in this because i am not able to call the function when the button is clicked. hope anyone can help me out with a good code.

echo"Enter the name :";
echo"<input name='names' type='text'>";
echo"<input type='submit' value='ADD name' align=center><b r>";
$link = mysql_connect(" ","","");
mysql_select_db ("db");

/*the below statements must get executed only when the button is clicked*/
$query="insert into table value("");
mysql_query($qu ery);
Jan 23 '07 #1
2 12655
acoder
16,027 Recognized Expert Moderator MVP
Just use a simple submit button which posts the data to another page which should be specified in your form tag, e.g.
[HTML]<form action="youract ionpage.php" method="post" ...>
<input type="submit" ...>[/HTML]
Jan 23 '07 #2
ronverdonk
4,258 Recognized Expert Specialist
The easiest way to handle this is in one script. After you entered the name and clicked the 'ADD name' button, the script itself is invoked again and processes the entered value by inserting it into the database.
The script's process is the folllowing:
Expand|Select|Wrap|Line Numbers
  1. If form is submitted 
  2.   Do
  3.     get the value
  4.     connect to server and db
  5.     build insert stmt
  6.     insert value into db
  7.     issue message
  8.   End-Do
  9. End-If
  10. Else
  11.   Do
  12.     show form
  13.     enter data
  14.     at click submit form
  15.   End-Do
  16. End-Else
[php]<?php
// check to see if form is submitted
if (isset($_POST['names'])) {
// get the POSTed value after cleansing
$names = stripslashes($_ POST['names']);
// setup the server and db connection
$link = mysql_connect(" ","","")
or die("Cannot connect to server: ".mysql_error() );
mysql_select_db ("db")
or die("Cannot connect to database: ".mysql_error() );
// build the MySQL query
$query="INSERT INTO table VALUES('$names' )";
// insert into db
$res = mysql_query($qu ery)
or die("INSERT error: ".mysql_error() );
// issue message
echo "Field succesfully added to database";
}
else {
echo '<form action="'.$_SER VER['PHP_SELF'].'" method="POST">' ;
echo "Enter the name :";
echo "<input name='names' type='text'>";
echo "<input type='submit' value='ADD name' align=center><b r>";
}
?>[/php]
Good luck.

Ronald :cool:
Jan 23 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
1818
by: Luis | last post by:
Application X has three screens. The user captures information on screen one and then clicks a navigation button to go onto screen two. He does the same on screen three. At the bottom of screen three is a submit button. When this button is clicked the system does some calculations with the information that is supplied and then uploads all the info that was captured on the screens to the database. My question is: Should the data be...
4
11835
by: authorking | last post by:
I use the following code to insert a data record in to a datatable of an access database.But every time I execute the command, there will rise an exception and the insert operation can't be completed. here's the code: System.Data.OleDb.OleDbConnection hist_cnn = new System.Data.OleDb.OleDbConnection(); System.Data.OleDb.OleDbCommand hist_command = new System.Data.OleDb.OleDbCommand();
1
1901
by: authorking | last post by:
I create the databse with the follwing code. I really don't know why the insert operation can't be completed. databse creation code: ADOX.CatalogClass cat = new ADOX.CatalogClass();//define a Jet database class cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=history_data.mdb;" + "Jet OLEDB:Engine Type=5");//create a Jet database System.Data.OleDb.OleDbConnection hist_cnn = new System.Data.OleDb.OleDbConnection();
2
12888
by: John Tyce | last post by:
When a button is clicked, a date is inserted or added into a combo box like this : ComboBox.Items.Add(string) or ComboBox.Items.Insert(0,string); Either way, the new string does not show up in the ComboBox. I get no errors or problems, it just will not work. At load time I am retrieving dates from an Oracle database and adding them to the ComboBox with out any problems. However, once the application is up and running I cannot add to the...
5
3247
by: Chris | last post by:
Based upon some prevoius postings on what to do for adding a 'add' row to a datagrid I utilize the footer to create the 'add' row. The only issue is that I have it sharing the 'UpDate_Command' and I use an argument to difference between an 'edit' vs. and 'add. But since I have field validation on both 'footer' and 'edit' columns I can't submit my edits since the footer validation kicks in.If I take the validation off then the both work fine...
0
1770
by: Steven Blair | last post by:
Hi, Would be grateful if anyone with a high understanding of the SqlDataSource and FormView could have a look at my example and my problems. This has really been puzzling me for some time, and would like and expert just to confirm what I am doing is the best approach. Application example:
2
10850
by: shapper | last post by:
Hello, I am working with a ListView but I suppose that with a GridView might be the same. Instead of having an Insert Button on each GridView row I would like to have only one Insert button, for example, in the GridView footer. When this button is clicked it should put the GridView in Insert Mode and the EditTemplate should be showed, maybe (?), in the last GridView
3
21088
by: Don | last post by:
I have a detailsView that I use with a GridView. When a row is selected I show the GridView with a default mode of Edit. This shows my fields with text boxes for changing the data. It also has the update and cancel buttons. Now I need to put the DetailsView into Insert mode when a button is clicked. In the button clicked event handler, in the code behind, I change to insert mode with this code:...
2
3205
by: adamace5o | last post by:
When i try to use post variables with php and mysql i can't get the insert into statement to accept varibles as values. If i use 'test' instead of $test it does work. I suspect it is something to do with the javascript im using but i can print the correct values so why am i unable to use them to enter data to a database? sorry i am new to all this here is the updatedata.php file <?php session_start(); $host="localhost"; // Host...
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.