Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mysql Insert Problems 4

Status
Not open for further replies.

vivilady

Programmer
Mar 5, 2005
36
GB
Hi, I am trying to insert three fields from my form into a mysql db but for some reason It dosn't give my errors and yet it dosn't insert it into the db, any suggestions please?

<?php require_once('Connections/Connection.php'); ?>
<?php

$submit = $_POST['submit'];
$destination = $_POST['destination_name'];
$AdultPrice = $_POST['Adult_price'];
$ChildrenPrice = $_POST['Children_price'];

if(isset($submit)) {
$Connection = mysql_pconnect($hostname_Connection, $username_Connection, $password_Connection) or die(mysql_error());
mysql_select_db($database_Connection, $Connection);
$query = ("INSERT INTO destination (destination_name, destination_adult_price, destination_children_price) VALUES ('$destination', '$AdultPrice', '$ChildrenPrice')");
$Recordset1 = mysql_query($query, $Connection) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);


mysql_close();
}
else {
echo '<p><font color="990000" size="+2">OOPS!! The EMAIL AND OR, USERNAME you entered are not valid registered user detail</font></p>';
echo '<p> Please either re-enter the correct email and a matching username, or Register an Account using the link on this page!</p>';
echo '<b><hr color="990000">';
}
?>

 
You should be getting some kind of error.

These lines:

$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

(at least the second) should produce a "Not a valid MySQL result resource" error, as an INSERT query only returns TRUE or FALSE, not a resource handle against which you can call mysql_fetch_*(). I recommend that you take a look at mysql_affected_rows().


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I have even commented out those two lines still no errors and still didnt insert... any other possible reasons?
 
Is anything at all being inserted? Even rows with blank entries?

Have you printed out your query to the browser and examined it?
Have you copied the query from the browser to your favorite MySQL admin tool and run the query?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
i think in your code you will always have $submit being set as you are expressly declaring it. it might be empty of course. change the line to:

Code:
if(isset($_POST['submit']))

in addition to sleipnir214's advice above, i'd also suggest dumping the incoming POST vars (print_r($_POST);) to make sure that the variables are coming in to the script as you expect them.

 
BTW, "if(isset($submit))" will always be true, because yue have "$submit = $_POST['submit'];" before the if statment. Be carefull with this issue.
 
Thanks guys, I'll try out these now, I'll update you in few moments, wish me luck...
 
Hi, I've tried dumping the data into the screen, and the are getting posted. I also changed the submit statemaent as suggested by jpadie but still there were no errors reported and the information were not inserted into the database... any more ideas please?

 
could you post the var dump output?

could you also post a screen dump of the sql statement? (ie just an echo $query; statement

thanks
 
just in case add ' in field's names

Code:
 $query = ("INSERT INTO destination ('destination_name', 'destination_adult_price', 'destination_children_price') VALUES ('$destination', '$AdultPrice', '$ChildrenPrice')");

you never know ;o)
 
Array ( [destination_name] => Scotland [Adult_price] => 500 [Children_price] => 120 [Submit] => UPDATE DATABASE )
 
$query = ("INSERT INTO destination (destination_name, destination_adult_price, destination_children_price) VALUES ('$destination', '$AdultPrice', '$ChildrenPrice')");
 
General recommendation:
Any mysql statament that could 'fail' should trap such a failure. This includes the select_db statement and the query statements. In your original code there is no mysql_error() checking after the connection was established.
 
Just In case, Here is the full code currently

<?php require_once('Connections/Connection.php'); ?>
<?php

$submit = $_POST['submit'];
$destination = $_POST['destination_name'];
$AdultPrice = $_POST['Adult_price'];
$ChildrenPrice = $_POST['Children_price'];


print_r($_POST);


if(isset($_POST['submit'])) {
$Connection = mysql_pconnect($hostname_Connection, $username_Connection, $password_Connection) or die(mysql_error());
mysql_select_db($database_Connection);

$query = ("INSERT INTO destination ('destination_name', 'destination_adult_price', 'destination_children_price') VALUES ('$destination', '$AdultPrice', '$ChildrenPrice')");
$Recordset1 = mysql_query ($query, $Connection);
}
?>
 
and there's one of your problems! the form control submit has a capital "S" and variables are case sensitive.

change the if statment to "if (isset($_POST['Submit']))"

i'd also escape the values of the POST array before using it:

$destination = mysql_escape_string($destination);
etc.

but this should not have stopped your original code from working.

could you post the actual screen output of the query? i.e. after setting the $query variable run echo $query; and post it here.

 
2 things:

1. add "echo $query" just to see your insert statment.

2. do you have "autocommit" enable in your MySQL DB? it is enabled by default, but take a look to that.

Cheers.
 
Array ( [destination_name] => gege [Adult_price] => 121 [Children_price] => 112 [Submit] => UPDATE DATABASE ) INSERT INTO destination ('destination_name', 'destination_adult_price', 'destination_children_price') VALUES ('gege', '121', '112')
 
thanks, the insert statement looks fine (in the abstract).

next step is to double check your sql schema

could you post the output of this script:
Code:
require_once('Connections/Connection.php');
$mysql_pconnect($hostname_Connection, $username_Connection, $password_Connection) 
  or die(mysql_error());
mysql_select_db($database_Connection)
  or die (mysql_error());

$query ("DESCRIBE destination");
$result = mysql_query($query) 
  or die (mysql_error());
while ($row=mysql_fetch_assoc($result))
{
  extract $row;
  echo "<pre>";
  print_r($row);
  echo "</pre>";
}
 
the code gave an error on this line " extract $row; "

Thanks
 

Parse error: parse error in e:\webareas\ou301\easyfly\insert_destinations.php on line 12
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top