3 Replies - 717 Views - Last Post: 28 February 2009 - 01:20 PM Rate Topic: -----

#1 pngrafxx126   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 07-November 06

Problem with Form Inserting into DB

Posted 28 February 2009 - 12:21 PM

Hi all:

So I'm trying to make an insert form functional.

It says it's working because I added a query at the end saying whether or not its inserted, but when I look into the DB -- it's not there.
Can anyone tell me if this code flow is correct?

I have 2 files.

insert.html
<html>
<head>
<title>New Customer</title>
<?php
	$cust_number = $_POST['cust_number'];
	$fname = $_POST['fname'];
	$lname = $_POST['lname'];
	$street = $_POST['street'];
	$city = $_POST['city'];
	$state = $_POST['state'];
	$zipcode = $_POST['zipcode'];
?>
</head>
<body>
<h2>Create New Entry into Customer</h2>

<form action="insert.php" method="post">
<table width="400" border="0" cellpadding="1" cellspacing="3" class="table1">
  <tr>
	<td>Customer Number</td>
	<td><input name="cust_number" type="text" size="30"></td>
  </tr>
  <tr>
	<td>First Name</td>
	<td><input name="fname" type="text" size="30"></td>
  </tr>
  <tr>
	<td>Last Name</td>
	<td><input name="lname" type="text" size="30"></td>
  </tr>
  <tr>
	<td>Street</td>
	<td><input name="street" type="text" size="30"></td>
  </tr>
	<tr>
	<td>City</td>
	<td><input name="city" type="text" size="30"></td>
  </tr>
  <tr>
	<td>State</td>
	<td><input name="state" type="text" size="30"></td>
  </tr>
  <tr>
	<td>Zip</td>
	<td><input name="zip" type="text" size="30"></td>
  </tr>
  <tr>
	<td> </td>
	<td><input type="submit" name="submit" value="Insert"></td>
  </tr>
</table>
</form>

</body>
</html>



insert.php
<?php
	$conn = @mysql_pconnect("localhost","xxx","xxx")
	or die ( "Unable to connect to the database" );
	
	$nr = @mysql_select_db ("xxx")
	or die("Can't select database");
	
	if ($fname and $lname and $street and $city and $state and $zip) {
		$sql = "INSERT INTO CUSTOMER (cust_number fname, lname, street, city, state, zip) VALUES (".$cust_number.",".$fname.",".$lname.",".$street.",".$city.",".$state.",".$zipcode.")";
		echo "Query A: ".$sql."<br>";
		$sql = mysql_real_escape_string($sql);
		echo "Query B: ".$sql."<br>";
		$nr = mysql_query($sql, $conn);

		if ($nr) {
			echo ("New Customer: $fname $lname has been added");
		}
			
		} else {
			echo "Some fields are empty, please fill them in.<br />";
			echo "<a href=\"java script:java script:history.go(-1)\">Click here to go back to previous page</a>";
	}
?>



Are these 2 files communicating correctly?
Please help.

Thanks,
Keith

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Form Inserting into DB

#2 BMR777   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 41
  • Joined: 01-February 09

Re: Problem with Form Inserting into DB

Posted 28 February 2009 - 12:38 PM

Echo out your final query and paste into PHPMyAdmin. See if you get a syntax error or if it works. Most the time data just doesn't appear it's a SQL syntax error, which PHPMyAdmin will confirm if this is it.
Was This Post Helpful? 0
  • +
  • -

#3 laces12   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 20-November 08

Re: Problem with Form Inserting into DB

Posted 28 February 2009 - 12:44 PM

change this:
<?php
	$conn = @mysql_pconnect("localhost","xxx","xxx")
	or die ( "Unable to connect to the database" );
   
	$nr = @mysql_select_db ("xxx")
	or die("Can't select database");
   
	if ($fname and $lname and $street and $city and $state and $zip) {
		$sql = "INSERT INTO CUSTOMER (cust_number fname, lname, street, city, state, zip) VALUES (".$cust_number.",".$fname.",".$lname.",".$street.",".$city.",".$state.",".$zipcode.")";
		echo "Query A: ".$sql."<br>";
		$sql = mysql_real_escape_string($sql);
		echo "Query B: ".$sql."<br>";
		$nr = mysql_query($sql, $conn);

		if ($nr) {
			echo ("New Customer: $fname $lname has been added");
		}
		   
		} else {
			echo "Some fields are empty, please fill them in.<br />";
			echo "<a href=\"java script:java script:history.go(-1)\">Click here to go back to previous page</a>";
	}
?>



To this:
<?php
	$conn = @mysql_pconnect("localhost","xxx","xxx")
	or die ( "Unable to connect to the database" );
   
	$nr = @mysql_select_db ("xxx")
	or die("Can't select database");
   
	if ($fname and $lname and $street and $city and $state and $zip) {
		$sql = "INSERT INTO CUSTOMER (cust_number fname, lname, street, city, state, zip) VALUES (".$cust_number.",".$fname.",".$lname.",".$street.",".$city.",".$state.",".$zipcode.")";
		echo "Query A: ".$sql."<br>";
		$sql = mysql_real_escape_string($sql);
		echo "Query B: ".$sql."<br>";
		$nr = mysql_query($sql, $conn) or die(mysql_error());

		if ($nr) {
			echo ("New Customer: $fname $lname has been added");
		}
		   
		} else {
			echo "Some fields are empty, please fill them in.<br />";
			echo "<a href=\"java script:java script:history.go(-1)\">Click here to go back to previous page</a>";
	}
?>



What happens?
Was This Post Helpful? 0
  • +
  • -

#4 pngrafxx126   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 07-November 06

Re: Problem with Form Inserting into DB

Posted 28 February 2009 - 01:20 PM

laces -- those codes look the same i dont see a difference..

but i used BMR's method -- I got it working now.

thanks guys

nvm i see you added the "or die(mysql_error());"

i'll use that next time though thanks

This post has been edited by pngrafxx126: 28 February 2009 - 01:19 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1