[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP to submit values to MySQL

Posted on 2004-04-30
6
Medium Priority
?
330 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 375 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 375 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Six Sigma Control Plans
Screencast - Getting to Know the Pipeline

830 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