Trouble with Insert

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikeb
Forum Commoner
Posts: 60
Joined: Tue Jul 08, 2003 4:37 pm
Location: Dublin, Ireland

Trouble with Insert

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

?>
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post 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.
mikeb
Forum Commoner
Posts: 60
Joined: Tue Jul 08, 2003 4:37 pm
Location: Dublin, Ireland

Post 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
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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.
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post 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 .
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Re: Trouble with Insert

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