468,103 Members | 1,225 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,103 developers. It's quick & easy.

Form not sending data to MySQL-DB

I got stuck on this, and it would be really great if anybody could
point me in the right direction.

I was trying setting up these scripts following instructions in a book
from 2003, so I thought maybe some of the code might be outdated.

Basically this is what Is supposed to happen:
1.- Display a form, enter details into fields and click submit.
2.- After having clicked on submit you're redirected to another page
that will display the entered values and store them in the MySQL DB
specified.

Now what happens is that no errors are displayed after clicking on the
submit button in the form, but neither are the entered values. In
PHPMyAdmin I can see that the Db is still empty.

I created a total of 3 files that reside in the same folder:

The form, that when clicking on submit is redirected to the next file:

<html>
<head>
<title>product_registration.htm</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>

<body>
<h1>Registration of products</h1>
<FORM METHOD="post" ACTION="insert_into_and_response.php">
<p>Enter the product number: <input type="text" name"number" size="10"
value="128"></p>
<p>Name of product: <input type="text" name"name" size="30"
value="Mango"></p>
<p>Price per kilo: <input type="text" name"price" size="10"
value="24,40"></p>
<p><input type="submit" value="Send data" name="B1">
<input type="reset" value"Reset" name="B2"></p>
</body>
</html>

The file that sends the data to DB, and shows you what you just entered
in the form:
<html>
<head>
<title>insert_into_and_response.php</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>

<body>
<?php
$pris_punkt= ereg_replace(",",".",$price);
include ("dbconnect.php");
$sql = "INSERT INTO table_productspecs
(product_number, product_name, kiloprice)
VALUES
($number, '$name', $price_punkt)";
mysql_query($sql);
?>
<h1>Received following data</h1>
<p>Product number: <?php print $number ?>.</p>
<p>Name of product:: <?php print $name ?>.</p>
<p>Price per kilo: <?php print $price ?>.</p>

</body>
</html>

The configuration file containing all the details required to connect
to the DB:
<?php
$dbconnection = mysql_pconnect("localhost","dbuser","password")
or die("Could not establish connection with mysql_connect.");
mysql_select_db("DB-name",$dbconnection)
?>

The code that made the DB:
CREATE TABLE table_productspecs
(idx INT AUTO_INCREMENT PRIMARY KEY,
product_number INT NOT NULL UNIQUE,
product_name VARCHAR(30) NOT NULL,
kiloprice DECIMAL(5,2) NOT NULL);

Sep 7 '06 #1
2 2260

drakorq wrote:
I got stuck on this, and it would be really great if anybody could
point me in the right direction.

I was trying setting up these scripts following instructions in a book
from 2003, so I thought maybe some of the code might be outdated.

Basically this is what Is supposed to happen:
1.- Display a form, enter details into fields and click submit.
2.- After having clicked on submit you're redirected to another page
that will display the entered values and store them in the MySQL DB
specified.

Now what happens is that no errors are displayed after clicking on the
submit button in the form, but neither are the entered values. In
PHPMyAdmin I can see that the Db is still empty.

I created a total of 3 files that reside in the same folder:

The form, that when clicking on submit is redirected to the next file:

<html>
<head>
<title>product_registration.htm</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>

<body>
<h1>Registration of products</h1>
<FORM METHOD="post" ACTION="insert_into_and_response.php">
<p>Enter the product number: <input type="text" name"number" size="10"
value="128"></p>
<p>Name of product: <input type="text" name"name" size="30"
value="Mango"></p>
<p>Price per kilo: <input type="text" name"price" size="10"
value="24,40"></p>
<p><input type="submit" value="Send data" name="B1">
<input type="reset" value"Reset" name="B2"></p>
</body>
</html>

The file that sends the data to DB, and shows you what you just entered
in the form:
<html>
<head>
<title>insert_into_and_response.php</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>

<body>
<?php
$pris_punkt= ereg_replace(",",".",$price);
include ("dbconnect.php");
$sql = "INSERT INTO table_productspecs
(product_number, product_name, kiloprice)
VALUES
($number, '$name', $price_punkt)";
mysql_query($sql);
?>
<h1>Received following data</h1>
<p>Product number: <?php print $number ?>.</p>
<p>Name of product:: <?php print $name ?>.</p>
<p>Price per kilo: <?php print $price ?>.</p>

</body>
</html>

The configuration file containing all the details required to connect
to the DB:
<?php
$dbconnection = mysql_pconnect("localhost","dbuser","password")
or die("Could not establish connection with mysql_connect.");
mysql_select_db("DB-name",$dbconnection)
?>

The code that made the DB:
CREATE TABLE table_productspecs
(idx INT AUTO_INCREMENT PRIMARY KEY,
product_number INT NOT NULL UNIQUE,
product_name VARCHAR(30) NOT NULL,
kiloprice DECIMAL(5,2) NOT NULL);
Two problems:

First (and it might be a typo) the HTML for the form elements is
incorrect:
<input type="text" name"number" size="10" value="128">

Needs an equal sign on the name attribute:
<input type="text" name="number" size="10" value="128">

The second potential problem is that the script you've posted requires
register_globals to be enabled in php.ini. Recent versions of PHP have
this disabled by default. Either turn it on (bad) or fix the script to
not require it (good).

Sep 7 '06 #2
*** drakorq escribió/wrote (7 Sep 2006 08:10:05 -0700):
I was trying setting up these scripts following instructions in a book
from 2003, so I thought maybe some of the code might be outdated.
Certainly it is. It also have several typos.

<p>Enter the product number: <input type="text" name"number" size="10"
value="128"></p>
name="number"

$sql = "INSERT INTO table_productspecs
(product_number, product_name, kiloprice)
VALUES
($number, '$name', $price_punkt)";
mysql_query($sql);
Rather than:

"INSERT INTO table (column) VALUES ('$foo')"

Use this:

"INSERT INTO table (column) VALUES ('" . mysql_escape_string($foo) . "')"

You can also use mysql_real_escape_string()
<p>Product number: <?php print $number ?>.</p>
<p>Product number: <?php print htmlspecialchars($number) ?>.</p>

or

<p>Product number: <?=htmlspecialchars($number)?>.</p>

$dbconnection = mysql_pconnect("localhost","dbuser","password")
or die("Could not establish connection with mysql_connect.");
Using a persistent connection means that PHP will remain connected to the MySQL server even when no one is visiting the site. This can make MySQL run out of connection slots even under low load. I suggest you use mysql_connection() unless you have a good reason.
--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Sep 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by cover | last post: by
3 posts views Thread by Amer Neely | last post: by
7 posts views Thread by h7qvnk7q001 | last post: by
27 posts views Thread by Chris | last post: by
reply views Thread by vladimir.plotnikov | last post: by
4 posts views Thread by bleachie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.