Page 1 of 2
MySQL connection problem
Posted: Tue Aug 03, 2004 10:23 pm
by mmaru
I am new to PHP. Could you please help me why the following code does not add the data to the database. Note: I can use the MySQL client program to add records no problem. When I try the code below it does nothing.
Form:
Code: Select all
<html>
<head>
<title>Item Entry Form</title>
</head>
<body>
<form method="post" action="item_save.php"> Item No.: <input item_no=""
type="text name="><br>
Product: <input name="product" type="text"><br>
Description: <input name="prod_desc" type="text"><br>
Price: <input name="price" type="text"><br>
Quantity <input name="quantity" type="text"><br>
<input value="Save" type="submit"></form>
</body>
</html>
PHP code:
Code: Select all
<HTML>
<BODY>
<?php
$host='localhost';
$user='mmaru';
$pw='marumysql';
$db='store';
$connect = mysql_connect($host, $user, $pw) or die ("Could not connect.");
mysql_select_db($db);
$table1='products';
$prod_statement = "INSERT INTO $table1
VALUES('$item_no', '$product','$prod_desc','$price','$quantity')";
if(mysql_query($prod_statement, $connect))
{print "Item added successfully.<br/>";
}
else {print "Item addition failed.<br/>";}
?>
<H2>Product Saved</H2>
<A HREF='item_entry.html'>Add Another</a/
</BODY>
</HTML>
Posted: Tue Aug 03, 2004 10:32 pm
by markl999
Debug it
Code: Select all
$prod_statement = "INSERT INTO $table1 VALUES('$item_no', '$product','$prod_desc','$price','$quantity')";
echo 'Query is: '.$prod_statement.'<br />'; //make sure the query looks ok
mysql_query($prod_statement, $connect) or die(mysql_error());
echo 'Item added successfully.<br/>';
Posted: Tue Aug 03, 2004 10:36 pm
by nigma
If you do what markl999 suggested you may find that the variables used in your $prod_statement do not have a value. I would think this is because you have register_globals off.
For more information regarding register_globals check out
http://wiki.devnetwork.net/index.php/RegisterGlobals
http://www.php.net/manual/en/ini.sect.d ... er-globals
Try inserting the following code before you set the variable $prod_statement:
Code: Select all
$product = $_POST['product'];
$prod_desc = $_POST['product_desc'];
$price = $_POST['price'];
$quantity = $_POST['quantity'];
Posted: Tue Aug 03, 2004 11:07 pm
by mmaru
Thank you for you kind help. I made the changes you suggested and it does give me a balnk screen. The modified form and php code is below:
Code: Select all
<html>
<body>
<h2>Item Entry Form</h2>
<form method='post' action='item_save.php'>
Item No.: <input type='text' name='item_no'/><br/>
Product.: <input type='text' name='product'/><br/>
Description: <input type='text' name='prod_desc'/><br/>
Price: <input type='text' name='price'/><br/>
Quantity: <input type='text' name='quantity'/><br/>
<input value="Save" type="submit">
</form>
</body>
</html>
Code: Select all
<HTML>
<BODY>
<?php
$host='localhost';
$user='mmaru';
$pw='marumysql';
$db='store';
$connect = mysql_connect($host, $user, $pw) or die ("Could not connect.");
mysql_select_db($db);
$table1='products';
$item_no = $_POST['item_no'];
$product = $_POST['product'];
$prod_desc = $_POST['product_desc'];
$price = $_POST['price'];
$quantity = $_POST['quantity'];
$prod_statement = "INSERT INTO $table1 VALUES('$item_no', '$product','$prod_desc','$price','$quantity')";
echo 'Query is: '.$prod_statement.'<br />'; //make sure the query looks ok
mysql_query($prod_statement, $connect) or die(mysql_error());
echo 'Item added successfully.<br/>';
?>
<A HREF='item_entry.html'>Add Another</a/
</BODY>
</HTML>
Thank you.
Maru
[
Edit: Added php and code tags. --markl999]
Posted: Tue Aug 03, 2004 11:26 pm
by markl999
Try using error_reporting, eg
Code: Select all
<?php
error_reporting(E_ALL); //add this line
$host='localhost';
$user='mmaru';
$pw='marumysql';
$db='store';
Posted: Tue Aug 03, 2004 11:26 pm
by nigma
Damn! Everyone has mod status but me.
Posted: Tue Aug 03, 2004 11:47 pm
by mmaru
Thank you. I made the changes and a blnak screen is what I get. Would there be a simple code that show me whether I can even connect without even trying to add any record using a form?
Code: Select all
<HTML>
<BODY>
<?php
error_reporting(E_ALL); //add this line
$host='localhost';
$user='mmaru';
$pw='marumysql';
$db='store';
$connect = mysql_connect($host, $user, $pw) or die ("Could not connect.");
mysql_select_db($db);
$table1='products';
$item_no = $_POST['item_no'];
$product = $_POST['product'];
$prod_desc = $_POST['product_desc'];
$price = $_POST['price'];
$quantity = $_POST['quantity'];
$prod_statement = "INSERT INTO $table1 VALUES('$item_no', '$product','$prod_desc','$price','$quantity')";
echo 'Query is: '.$prod_statement.'<br />'; //make sure the query looks ok
mysql_query($prod_statement, $connect) or die(mysql_error());
echo 'Item added successfully.<br/>';
?>
<A HREF='item_entry.html'>Add Another</a/
</BODY>
</HTML>
Thank you again for helping me.
[
Edit: Added php and code tags. --markl999]
Posted: Tue Aug 03, 2004 11:51 pm
by nigma
You want to see if you can connect to a database and if so output a success message?
If so:
Code: Select all
<?php
$link = @mysql_connect("host", "user", "pass");
if (!$link) {
// unable to connect to database
echo 'Failed to connect';
}
else {
// you were able to connect to the database
echo 'Database connect succeeded';
}
?>
Posted: Tue Aug 03, 2004 11:53 pm
by mmaru
What is mod status?
Posted: Tue Aug 03, 2004 11:56 pm
by nigma
mod status, a.k.a being a moderator. I was joking around.
Posted: Wed Aug 04, 2004 12:12 am
by mmaru
Hello nigma,
I thought that meant changeing the config file. Is there even a way to just simple test a connection to MySQL database using PHP without adding any records?
Posted: Wed Aug 04, 2004 12:19 am
by mmaru
feyd | Please use Code: Select all
tags when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
nigma tested the code you provided and I get a blnk screen.
PHP:
Code: Select all
<?php
$link = @mysql_connect("localhost", "mmaru", "marumysql");
if (!$link) {
// unable to connect to database
echo 'Failed to connect';
}
else {
// you were able to connect to the database
echo 'Database connect succeeded';
}
?>
I am using PHP 5 if it is going to make a difference.
feyd | Please use Code: Select all
tags when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Wed Aug 04, 2004 12:20 am
by feyd
Code: Select all
<?php
mysql_connect('host','username','password') or die(mysql_error());
$query = mysql_query('SELECT VERSION()') or die(mysql_error());
print_r(mysql_fetch_row($query));
?>
Posted: Wed Aug 04, 2004 12:30 am
by mmaru
It does not work - I do not know why. I have not used PHP with MySQL before this is just my first try to see whether I can even connect before I do anything. Am I missing something in configuring the two?
<?php
mysql_connect('localhost','mmaru','marumysql') or die(mysql_error());
$query = mysql_query('SELECT VERSION()') or die(mysql_error());
print_r(mysql_fetch_row($query));
?>
Posted: Wed Aug 04, 2004 12:35 am
by mmaru
markl999 | Please use Code: Select all
tags when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
By the way - Apache and PHP work no problem. The following code works fine.
Code: Select all
HTML:
<HTML>
<HEAD>
<TITLE>Using Numbers</TITLE>
</HEAD>
<BODY>
<?php
/* $Quantity must be passed to this page from a form or via the URL.
$Discount is optional.*/
$Cost=2000.00;
$Quantity = abs($Quantity);
$Discount = abs($Discount);
$Tax=0.06;
$Tax = $Tax++; // $Tax is now worth 1.06.
$TotalCost = (($Cost * $Quantity) - $Discount) * $Tax;
$Payments = round($TotalCost, 2) / 12;
// Print the results.
print ("You requested to pruchase $Quantity widget(s) at \$$Cost each.\n<P>");
print ("The total with tax, minus your \$$Discount discount, comes to $");
printf ("%01.2f", $TotalCost);
print (".\n<P>You may purchase the widget(s) in 12 monthly installments of $");
printf ("%01.2f", $Payments);
print (" each.\n<P>");
?>
</BODY>
</HTML>
PHP:
Code: Select all
<HTML>
<HEAD>
<TITLE>Using Numbers</TITLE>
</HEAD>
<BODY>
<?php
/* $Quantity must be passed to this page from a form or via the URL.
$Discount is optional.*/
$Cost=2000.00;
$Tax=0.06;
$TotalCost = $Cost * $Quantity;
$Tax = $Tax + 1; // $Tax is now worth 1.06.
$TotalCost = $TotalCost - $Discount;
$TotalCost = $TotalCost * $Tax;
$Payments = $TotalCost / 12;
// Print the results.
print ("You requested to pruchase $Quantity widget(s) at \$$Cost each.\n<P>");
print ("The total with tax, minus your \$$Discount discount, comes to $");
printf ("%01.2f", $TotalCost); \\ \$$TotalCost.\n<P>");
print (".\n<P>You may purchase the widget(s) in 12 monthly installments of $");
printf ("%01.2f", $Payments); \\\n$$Payments) each.\n<P>");
print (" each.\n<P>");
?>
</BODY>
</HTML>
markl999 | Please use Code: Select all
tags when posting code. Please don't make us tell you again.[/color]