Solved

PHP or HTML Code

Posted on 2011-03-01
7
250 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
  • 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 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

23 Experts available now in Live!

Get 1:1 Help Now