Page 1 of 1

[SOLVED] SQL Code won't insert values

Posted: Sun Jul 18, 2004 1:43 am
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]

Posted: Sun Jul 18, 2004 1:45 am
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'");

Posted: Sun Jul 18, 2004 4:38 am
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.

Fixed

Posted: Sun Jul 18, 2004 4:47 pm
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.