Solved

PHP to submit values to MySQL

Posted on 2004-04-30
6
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this post we will learn different types of Android Layout and some basics of an Android App.
Simple Linear Regression
Starting up a Project

707 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