[SOLVED] SQL Code won't insert values

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Bobo the Bugbear
Forum Newbie
Posts: 4
Joined: Sun Jul 18, 2004 1:43 am

[SOLVED] SQL Code won't insert values

Post by Bobo the Bugbear »

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I am having the problem that my SQL code is not working in inserting the defined variables that are passed from a form into the MySQL.  I tested the variables and they are being passed but the code is not inserting.  The connection to the DB is active.  Here is the code.

Code: Select all

<?php 

$name = trim($_POST["name"]);
$code = trim($_POST["code"]);
$month = trim($_POST["month"]);
$year = trim($_POST["year"]);
$URL = trim($_POST["URL"]);
$owner = trim($_POST["owner"]);
$category = trim($_POST["category"]);
$location = trim($_POST["location"]);

if ($category == ""){
    $category = 5;
}

$runQuery ("INSERT INTO sites (site_name, site_code, site_startMonth, site_startYear, site_URL, site_owner, site_category, site_location)".
"VALUES ('$name', '$code', '$month', $year, '$URL', '$owner', $category, '$location'");

$projectAdded = 0;


?>

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

$runQuery ("INSERT INTO sites (site_name, site_code, site_startMonth, site_startYear, site_URL, site_owner, site_category, site_location)".
"VALUES ('$name', '$code', '$month', $year, '$URL', '$owner', $category, '$location'");

try..

Code: Select all

mysql_query("INSERT INTO sites (site_name, site_code, site_startMonth, site_startYear, site_URL, site_owner, site_category, site_location)".
"VALUES ('$name', '$code', '$month', $year, '$URL', '$owner', $category, '$location'");
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Missing some '' around the $year var. And also have the last " in the wrong place - needs to be outside the braces.

When you have problems like this cut the query down and test it in stages. Delete it all and see if it works when you do:

Code: Select all

mysql_query("INSERT INTO sites (site_name) VALUES ('$name')");
And if that works:

Code: Select all

mysql_query("INSERT INTO sites (site_name, site_code) VALUES ('$name', '$code')");
And do it one step at a time making sure that the data is posted to the database until you have an error free query that inserts the data.
Bobo the Bugbear
Forum Newbie
Posts: 4
Joined: Sun Jul 18, 2004 1:43 am

Fixed

Post by Bobo the Bugbear »

Thanks everyone. It was the quote bit that was throwing everything off. With this fixed, it worked. Kind of funny but this could of turned into a 16hr bug.
Post Reply