Solved

PHP or HTML Code

Posted on 2011-03-01
7
259 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Make check boxes work 8 41
Need to add another Column in Powershell script output 2 32
Session on Html 8 38
Why is my $_POST not going to results page 10 35
When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

740 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