MySQL connection problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

mmaru
Forum Newbie
Posts: 14
Joined: Tue Aug 03, 2004 10:23 pm

MySQL connection problem

Post 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>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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/>';
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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'];
mmaru
Forum Newbie
Posts: 14
Joined: Tue Aug 03, 2004 10:23 pm

Post 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

&lt;html&gt;
&lt;body&gt;
&lt;h2&gt;Item Entry Form&lt;/h2&gt;
&lt;form method='post' action='item_save.php'&gt;
Item No.: &lt;input type='text' name='item_no'/&gt;&lt;br/&gt;
Product.: &lt;input type='text' name='product'/&gt;&lt;br/&gt;
Description: &lt;input type='text' name='prod_desc'/&gt;&lt;br/&gt;
Price: &lt;input type='text' name='price'/&gt;&lt;br/&gt;
Quantity: &lt;input type='text' name='quantity'/&gt;&lt;br/&gt;
&lt;input value="Save" type="submit"&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

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]
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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';
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Damn! Everyone has mod status but me.
mmaru
Forum Newbie
Posts: 14
Joined: Tue Aug 03, 2004 10:23 pm

Post 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]
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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';
}
?>
mmaru
Forum Newbie
Posts: 14
Joined: Tue Aug 03, 2004 10:23 pm

Post by mmaru »

What is mod status?
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

mod status, a.k.a being a moderator. I was joking around.
mmaru
Forum Newbie
Posts: 14
Joined: Tue Aug 03, 2004 10:23 pm

Post 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?
mmaru
Forum Newbie
Posts: 14
Joined: Tue Aug 03, 2004 10:23 pm

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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));

?>
mmaru
Forum Newbie
Posts: 14
Joined: Tue Aug 03, 2004 10:23 pm

Post 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));

?>
mmaru
Forum Newbie
Posts: 14
Joined: Tue Aug 03, 2004 10:23 pm

Post 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]
Post Reply