Solved

PHP or HTML Code

Posted on 2011-03-01
7
264 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Find out what you should include to make the best professional email signature for your organization.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

724 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