Page 1 of 1

Trouble with Insert

Posted: Sun Oct 05, 2003 9:14 am
by mikeb
Hi,

I'm having trouble inserting a record. I've got lots of Insert pages written and working fine so I think my syntax is ok. But the following code just won't insert a new record. I've shortened the code for legibility but is there any inherent reason why the following won't work? I'm getting no error messages but the code will not process. I've tested the code, building it up line by line with test variable output and it works up to the point of the sql query

:arrow: property_code is being posted from a form
:arrow: the variables in the rates table are all decimal values
:arrow: All values are allowed to be null (for testing anyway)

<?php
// Includes
include("MyGlobalVariableFile.php");

// Set Table Name
$table_name="rates";

// Set up connection
$connection=mysql_connect("$glob_host", "$glob_user", "$glob_pass")
or die(mysql_error());

// Set the database
$db=mysql_select_db($db_name, $connection)
or die(mysql_error());

// Do the SQL for rates
$sql="
INSERT INTO $table_name(rates_id ,property_code, and others...)
VALUES ('', '$_POST[property_code]', and others...)";
$result=(mysql_query($sql,$connection)
or die(mysql_error());

?>

Posted: Sun Oct 05, 2003 10:36 am
by Stoneguard
Are you getting a die on insert? Or is something else happening?

Typically I test my SQL code by echoing it out to my browser, then I cut and paste it into a query session and see what happens.

Posted: Sun Oct 05, 2003 10:43 am
by mikeb
Are you getting a die on insert? Or is something else happening?
Not getting an error if that's what you mean? I'm just getting a blank html page in the browser
echoing it out to my browser
Do you mean browsing the pages? That's how I'm testing them
then I cut and paste it into a query session
Do you mean through PHPMyAdmin? Funny enough, to help ensure good syntax, I did the query manually through PHPMyAdmin and pasted that code into the php script as a starting point. Do you know if data types make a difference when querying? The table is a pricing grid attached to a property (vacation rental properties) and each 'rate' is a decimal figure representing a currency rate for a given month e.g. Jul_Rate might equal 350.00

thanks for your help

Mike

Posted: Sun Oct 05, 2003 10:51 am
by DuFF
mikeb wrote:
echoing it out to my browser
Do you mean browsing the pages? That's how I'm testing them
He means that instead of using

Code: Select all

$result=(mysql_query($sql,$connection)
or die(mysql_error());
to do the query, comment out the $result line and add

Code: Select all

echo $sql;
Then you can copy and paste the output into PHPMyAdmin to see if it works. If it doesn't then you know you've got a problem.

Posted: Sun Oct 05, 2003 3:25 pm
by mudkicker
if rates_id the primary key in your table you insert with
$sql="INSERT INTO $table_name(rates_id ,property_code, and others...) VALUES ('', '$_POST[property_code]', and others...)"; this you can use there NULL .

Posted: Sun Oct 05, 2003 10:18 pm
by JAM
You have some () wrong I belive...

Code: Select all

// from
$result = (mysql_query($sql,$connection) or die(mysql_error());
// to
$result = mysql_query($sql,$connection) or die(mysql_error());
What do you use as error-level?

mudkicker:
Good thinking, but that is not needed, if the field has a default value + 'not null' set. Both ways are allright.

Re: Trouble with Insert

Posted: Mon Oct 06, 2003 12:25 am
by Cruzado_Mainfrm
mikeb wrote:

Code: Select all

<?php
// Do the SQL for rates
$sql="
INSERT INTO $table_name(rates_id ,property_code, and others...)
VALUES ('', '$_POST[property_code]', and others...)";
$result=(mysql_query($sql,$connection)
or die(mysql_error());
?>
change $_POST[property_code] to {$_POST[property_code]}