Solved

PHP or HTML Code

Posted on 2011-03-01
7
262 Views
Last Modified: 2012-05-11
I have the following code that looks perfect in a browser. Is there a way for me to add some php code t add this to my mysql database? I've tried several different code snips I've found online but can't get any to work. The database name is customers & the table is customer_info. Below is the code without the mysql connect code:


<html>

<head>
<title> Update Database </title>
</head>

<body>
<form method="post" action="add.php">

First Name: <br />

<input type="text" name="customer_fname" size="30" /><br />

Last Name: <br />

<input type="text" name="customer_lname" size="30" /><br />

Location: <br />

<input type="text" name="location" size="30" /><br />


<input type="submit" value="Add Customer" />


</form>
</body>
</html>

Open in new window

0
Comment
Question by:wantabe2
[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
  • 3
7 Comments
 
LVL 7

Expert Comment

by:Vimal DM
ID: 35008550
Hai,

Find the attachment and follow it up!
samples.zip
0
 
LVL 11

Expert Comment

by:Murfur
ID: 35008720
Here's a pretty simple set-up.

You could also put it in the top half of your form page rather than have a separate page.

<?php
#	Page: add.php
##########################

#	Report all errors
##########################
ini_set('display_errors',1);
error_reporting(E_ALL);

#	db connection settings
##########################

#	server IP
$host	= "localhost";
#	sql user
$user	= "database username";
#	sql pw
$pw	= "database password";
#	sql database
$db	= "customers";

#	get submitted values & escape the text string
#################################################
if ( isset( $_POST['customer_fname'] ) ){
	$customer_fname = addslashes( $_POST['customer_fname'] );
}
if ( isset( $_POST['customer_lname'] ) ){
	$customer_lname = addslashes( $_POST['customer_lname'] );
}
if ( isset( $_POST['location'] ) ){
	$location = addslashes( $_POST['location'] );
}

#	connect to db server
##########################
$conn = mysql_connect( $host, $user, $pw )
or die( "Error! Unable to connect to database server: <br/>" . mysql_error() );
#	connect to db
##########################
$rs = mysql_select_db( $db, $conn )
or die( "Error! Unable to connect to database:  <br/>" . mysql_error() );


$strSQL = "INSERT INTO customer_info
	( customer_fname, customer_lname, location )
	VALUES
	( '" . $customer_fname . "', '" . $customer_lname . "', '" . $location . "' )";

#	execute db insert
##########################
if (!mysql_query( $strSQL, $conn )){
	echo( "Unable to save data to database: <br/>" . mysql_error() . "<br/>" . $strSQL . "</span><br/>" );
}
else{
	header( "Location: next_page.php" );
}
?>

Open in new window

0
 
LVL 15

Author Comment

by:wantabe2
ID: 35008821
Why is this code not working inserting what is typed in into the mysql database?
<html>

<?php
	$config["HOSTNAME"]		= "localhost";
	$config["USERNAME"]		= "root";
	$config["PASSWORD"] 	= "mypassword";
	$config["DBNAME"]      	= "customers";
	
	$dbconn = mysql_pconnect($config["HOSTNAME"], $config["USERNAME"], $config["PASSWORD"]) or  die(mysql_error());
	mysql_select_db($config["DBNAME"] , $dbconn);
?>

<head>
<title> Update Database </title>
</head>

<body>
<form method="post" action="add.php">

 <b>First Name:</b> <br />

<input type="text" name="customer_fname" size="35" /><br />

<b>Last Name:</b> <br />

<input type="text" name="customer_lname" size="35" /><br />

<b>Location:</b> <br />

<input type="text" name="location" size="35" /><br />

<b>Order Date:</b> <br />

<input type="text" name="order_date" size="35" /><br />

<b>Ship Date:</b> <br />

<input type="text" name="ship_date" size="35" /><br />


<input type="submit" value="Submit" />

</form>
</body>
</html>

Open in new window

0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 15

Author Comment

by:wantabe2
ID: 35008832
Disregard my last post, I inserted the wrong code...why is this code not working?
<html>

<?php
	$config["HOSTNAME"]		= "localhost";
	$config["USERNAME"]		= "root";
	$config["PASSWORD"] 	= "mypassword";
	$config["DBNAME"]      	= "customers";
	
	$dbconn = mysql_pconnect($config["HOSTNAME"], $config["USERNAME"], $config["PASSWORD"]) or  die(mysql_error());
	mysql_select_db($config["DBNAME"] , $dbconn);
?>

<head>
<title> Update Database </title>
</head>

<body>
<form method="post" action="add.php">

 <b>First Name:</b> <br />

<input type="text" name="customer_fname" size="35" /><br />

<b>Last Name:</b> <br />

<input type="text" name="customer_lname" size="35" /><br />

<b>Location:</b> <br />

<input type="text" name="location" size="35" /><br />

<b>Order Date:</b> <br />

<input type="text" name="order_date" size="35" /><br />

<b>Ship Date:</b> <br />

<input type="text" name="ship_date" size="35" /><br />


<input type="submit" value="Submit" />

</form>
</body>
</html>

Open in new window

0
 
LVL 11

Expert Comment

by:Murfur
ID: 35009044
Because you have not told it to do anything.
You have merely made the connection to the database server.

The ACTION parameter of the FORM tag tells the page what to do, in your example, send the form values to a page called add.php
All the database actions should be in that page or, as I said before, you could put it in the same page as your form but then you need to add the rest of the PHP insert as well.

Kind of like this:


<?php
#	Report all errors
##########################
ini_set('display_errors',1);
error_reporting(E_ALL);

#	if form submitted, process the values
#################################################
if ( $_SERVER['REQUEST_METHOD'] == "POST" ) ){
	
	#	db connection settings
	##########################

	#	server IP
	$host	= "localhost";
	#	sql user
	$user	= "database username";
	#	sql pw
	$pw		= "database password";
	#	sql database
	$db		= "customers";

	#	get submitted values & escape the text string
	#################################################
	if ( isset( $_POST['customer_fname'] ) ){
		$customer_fname = addslashes( $_POST['customer_fname'] );
	}
	if ( isset( $_POST['customer_lname'] ) ){
		$customer_lname = addslashes( $_POST['customer_lname'] );
	}
	if ( isset( $_POST['location'] ) ){
		$location = addslashes( $_POST['location'] );
	}


	#	connect to db server
	##########################
	$conn = mysql_connect( $host, $user, $pw )
	or die( "Error! Unable to connect to database server: <br/>" . mysql_error() );
	#	connect to db
	##########################
	$rs = mysql_select_db( $db, $conn )
	or die( "Error! Unable to connect to database:  <br/>" . mysql_error() );


	$strSQL = "INSERT INTO customer_info
				( customer_fname, customer_lname, location )
				VALUES
				( '" . $customer_fname . "', '" . $customer_lname . "', '" . $location . "' )";

	#	execute db insert
	##########################
	if (!mysql_query( $strSQL, $conn )){
		echo( "Unable to save data to database: <br/>" . mysql_error() . "<br/>" . $strSQL . "</span><br/>" );
	}
	else{
		header( "Location: next_page.php" );
	}
}
?>
<head>
	<title> Update Database </title>
</head>

	<body>
		<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
			<b>First Name:</b> <br />
			<input type="text" name="customer_fname" size="35" /><br />
			<b>Last Name:</b> <br />
			<input type="text" name="customer_lname" size="35" /><br />
			<b>Location:</b> <br />
			<input type="text" name="location" size="35" /><br />
			<input type="submit" value="Submit" />
		</form>
	</body>
</html>

Open in new window

0
 
LVL 15

Author Comment

by:wantabe2
ID: 35009066
Murfur,
Your code worked great for me & thanks! After I hit the submit button the data does go into the mysql database but it then takes my to a browser page that says "HTTP 404 Page Cannot Be Found". Is there any way I can get it to take me to another location such as www.google.com instead of the page not found page?
0
 
LVL 11

Accepted Solution

by:
Murfur earned 500 total points
ID: 35009181
Ah, I hadn't expected you to use this code verbatim! It can't find "next_page.php"....

Look in line 57 - header( "Location: next_page.php" );

change "next_page.php" to where you want to go

So to go to a page in your site called "wibble.html" put
header( "Location: wibble.html" );

or to go to google.com, put
header( "Location: http://www.google.com" );
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Considerations PHP using multiple locations 9 33
Mysql query one to many 11 37
Testing a list of Emails for validity 4 32
Developers / Staff Setup 10 40
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

737 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