Solved

PHP to submit values to MySQL

Posted on 2004-04-30
6
322 Views
Last Modified: 2010-04-17
I have a webpage which has lots of text boxes to enter information.  When pressing submit, that information is presented back to you on a second page.  As well as this, I want that data to be submitted into a MySQL database.  Does anyone know the code to make the data insert itself when pressing a submit button?

So far I have:



<form action="page2.php" method="POST">


Title:
<input type="text" name="title" size="4"/>
<br>
Firstname:
<input type="text" name="forname" size="56"/>
<br>

........

and

<input type="submit" value="Send"/>
0
Comment
Question by:j_byron
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:indigostark
ID: 10958688
$title = $_POST['title'];
$forname = $_POST['forname'];

// $host = hostname to the database
// $username = username to the database
// $password = password to the database
$dbLink = mysql_connect($host, $user, $password) or die ("Could not connect");;

// $sql = SQL INSERT STATEMENT (ie) INSERT INTO .... ... .. .
$result = mysql_query($sql, $dbLink);
0
 

Author Comment

by:j_byron
ID: 10958762
Do I need

$title = $_POST['title'];
$forname = $_POST['forname'];

as well as the

Title:
<input type="text" name="title" size="4"/>
<br>
Firstname:
<input type="text" name="forname" size="56"/>
<br>

Also, how do I get it do happen upon pressing the submit button?
0
 
LVL 4

Assisted Solution

by:indigostark
indigostark earned 125 total points
ID: 10959227
Have your HTML in a file called (for example) form.htm
===================================
<form name="form1" method="POST" action="nextpage.php" >
<input type="text" name="title" size="4"/>
<br>
Firstname:
<input type="text" name="forname" size="56"/>
<br>

etc .........

===========================
now in your file called nextpage.php
have the following

<?php
$title = $_POST['title'];
$forname = $_POST['forname'];

// $host = hostname to the database
// $username = username to the database
// $password = password to the database
$dbLink = mysql_connect($host, $user, $password) or die ("Could not connect");;

// $sql = SQL INSERT STATEMENT (ie) INSERT INTO .... ... .. .
$result = mysql_query($sql, $dbLink);

etc .........
?>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:j_byron
ID: 10968000
There are no errors.  But the data is not getting into the database.

I have tried these two queries:

1)

$query = "INSERT INTO mytablename title,forname,...)VALUES('$title','$forname',....)";

Or

2)

$query = "INSERT INTO mytablename VALUES('$title','$forname',....)";

and in both cases:

$result = mysql_query($query,$conn);


Are those queries ok?
0
 

Author Comment

by:j_byron
ID: 10968013
1) I missed out the bracket:

$query = "INSERT INTO mytablename (title,forname,...)VALUES('$title','$forname',....)";
0
 
LVL 1

Accepted Solution

by:
darpangoel earned 125 total points
ID: 10975632
yeah the query looks ok

the syntax again is

$query = INSERT INTO $tablename VALUES ('$val1',$val2','$val3');
just put those value in the order of the fields .use  "describe $tablename;" or "select * from $tablename;"  to get the order of the fields.

you don't need the link_identifier ( the 2nd parameter )  use
if (!($result = mysql_db_query ( $query )))
            echo (mysql_error());
or do this
to test the query is working or not do this.
echo  the query and the copy paste it on the terminal. check if it works. if there are any errors rectify .

ne more doubts???
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 65
Looking for example pivot year code used in Y2K 4 61
How can i compile this github project?? 2 74
control image tags in a string ? 12 108
A short article about a problem I had getting the GPS LocationListener working.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now