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">';
}
?>

 
sorry, sloppy coding. it should be extract ($row);
 
this time it gave this error,

Fatal error: Call to undefined function: () in e:\webareas\ou301\easyfly\insert_destinations.php on line 2

 
difficult to debug that! what is line 2 of the code?
 
Sorry for that, should have pointed that out.

$mysql_pconnect($hostname_Connection, $username_Connection, $password_Connection)
or die(mysql_error());
 
sorry - found the error

it should be mysql_pconnect and NOT $mysql_pconnect

again, apologies for sloppy coding - i wrote straight into the text box rather than a php editor (by way of explanation rather than excuse...)
 
Fatal error: Call to undefined function: () in e:\webareas\ou301\easyfly\insert_destinations.php on line 7


Line 7 being:

$query ("DESCRIBE destination");
 
$query = "DESCRIBE destination";

gosh - i must be tired...
 
Hurray, here is the result...

Array
(
[Field] => destination_id
[Type] => mediumint(8) unsigned
[Null] =>
[Key] => PRI
[Default] =>
[Extra] => auto_increment
)

Array
(
[Field] => destination_name
[Type] => varchar(30)
[Null] =>
[Key] =>
[Default] =>
[Extra] =>
)

Array
(
[Field] => destination_adult_price
[Type] => float
[Null] =>
[Key] =>
[Default] => 0
[Extra] =>
)

Array
(
[Field] => destination_children_price
[Type] => float
[Null] =>
[Key] =>
[Default] => 0
[Extra] =>
)

cheers!
 
right...
think i've got it now. here is a rewrite of the code.
let me know how it goes....

Code:
<?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); //debug the incoming vars
    
    
    if (isset($_POST['Submit'])) 
	{
    	mysql_pconnect($hostname_Connection, $username_Connection, $password_Connection) 
			or die("Unable to connect to db " . mysql_error());
    	mysql_select_db($database_Connection)
			or die("unable to select database. ". mysql_error());
		
    
	    $query = 
				"INSERT 
				INTO destination 
				(`destination_name`, `destination_adult_price`, `destination_children_price`)
				 VALUES 
				 ('$destination', $AdultPrice, $ChildrenPrice)"; //don't quote the prices as they are float data types             
	    mysql_query ($query)
			or die("Query didn't work. ".mysql_error());
    }
    ?>
 
Thanks a million jpadie like I and many other people in this forum has previously said, you are a true genious and an inspiration to many! Thank you once more for your effortless help.

It Works!
 
my pleasure. don't forget that you should escape the vars before you using them in a db query when you are using a production system. a quick way to do this is to replace your variable declaraion code segment with

Code:
foreach ($_POST as $key=>$val)
{ 
  $tmp[$key] = mysql_escape_string($val);
}
extract ($tmp);

i'm also really lazy so tend to have all the fields in my database set to the datatype varchar. this way i can just add single quotes to all my db writes and don't need to worry about the data type i am using!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top