PDA

View Full Version : INSERT into MySQL problem


russia5
06-01-2008, 09:13 PM
I have a form that posts the narrative of a month, ie) January and inserts it into a MySQL field, age_mth (it works fine) I want to also insert the numerical value of the month into another MySQL field age_month ie) 01 should go into age_month - this does not work and I need to figure out what I am doing wrong.

I have created a file called month_conversion.php that has a lookup table that converts the narrative to the month.



<?php

SMARTY INCLUDES GO HERE

$lookup = array("January" => 01, "February" => 02, "March" => 03. "April" => 04, "May" => 05, "June" => 06, "July" => 07, "August" => 08, "September" => 09, "October" => 10, "NOvember" => 11, "December" => 12);
// use month names on your form

$month = $_POST["age_month"]; // the name
$monthnum = $lookup[$month]; // the number
$query = 'INSERT INTO subdomain_submission (age_month) VALUES ('$monthnum')';


?>




the database table is subdomain_submission and the database field is age_month. The SMARTY includes are standard on my site and connect to the database for the other applications.

Upon submission, I get a 0 put into age_month no matter what month I have selected. Can someone see what I am doing wrong?

GO ILLINI
06-01-2008, 11:12 PM
The main problem:
$query = 'INSERT INTO subdomain_submission (age_month) VALUES ('$monthnum')';you cannot have a variable inside single quotes.
$query = "INSERT INTO `subdomain_submission` (age_month) VALUES ('{$monthnum}');";There are also several other problems.

$lookup = array("January" => 01, "February" => 02, "March" => 03, "April" => 04, "May" => 05, "June" => 06, "July" => 07, "August" => 08, "September" => 09, "October" => 10, "November" => 11, "December" => 12);
You need to fix that comma(and the capital O in nov. was bothering me...) and you need to convert all of the numbers to strings(put them in quotes); you have them set as integers and integers cannot start with 0.

-Adam

Apothem
06-01-2008, 11:12 PM
$query = 'INSERT INTO subdomain_submission (age_month) VALUES ('.$monthnum.')';

You forgot to attach the variable.

Edit: Buggers, someone posted before I did.