Link to home
Start Free TrialLog in
Avatar of wantabe2
wantabe2Flag for United States of America

asked on

PHP or HTML Code

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

Avatar of Vimal DM
Vimal DM
Flag of India image

Hai,

Find the attachment and follow it up!
samples.zip
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

Avatar of wantabe2

ASKER

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

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

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

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?
ASKER CERTIFIED SOLUTION
Avatar of Justin Pilditch
Justin Pilditch
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial