Jump to content

Recommended Posts

Ok I don't understand what I'm doing wrong here. This code I wrote takes the info from my form and posts it to my db. What I need to do is take the fuel_type from the form match it to the same name in the expense_categories table, then put the value of the find in the expense_fuel table.

 

when i put my select * code, thats showing up in my output html instead of it doing the actual action... what the hell did i do wrong =(

 


<?php
$con = mysql_connect("localhost","root","");
if (!$con)
 {
 die('Could not connect: ' . mysql_error());
 }

include('includes/header.php');


$fuel_type = $_POST['fuel_type'];
$fuel_amount = $_POST['fuel_amount'];
$fuel_month = $_POST['fuel_month'];
$fuel_day = $_POST['fuel_day'];
$fuel_year = $_POST['fuel_year'];


mysql_select_db("terra_elegante_operations", $con);

$fuel_code = 'SELECT * FROM expense_categories WHERE type = $fuel_type'; 


$sql="INSERT INTO expense_fuel (id, fuel_type, amount, month, day, year)
VALUES
('$fuel_code', '$fuel_type', '$fuel_amount', '$fuel_month', '$fuel_day', '$fuel_year')";

if (!mysql_query($sql,$con))
 {
 die('Error: ' . mysql_error());
 }

echo "1 record added";
echo "$fuel_code";



include('includes/footer.php');

?>

Link to comment
https://forums.phpfreaks.com/topic/269078-i-must-be-stupid/
Share on other sites

You are not querying your SELECT statement. You are literally putting the SELECT query inside the INSERT query.

 

$fuel_code = 'SELECT * FROM expense_categories WHERE type = $fuel_type'; 


$sql="INSERT INTO expense_fuel (id, fuel_type, amount, month, day, year)
VALUES
('$fuel_code', '$fuel_type', '$fuel_amount', '$fuel_month', '$fuel_day', '$fuel_year')";

 

This will create a query that looks like this

INSERT INTO expense_fuel (id, fuel_type, amount, month, day, year)
VALUES
('SELECT * FROM expense_categories WHERE type = $fuel_type', '$fuel_type', '$fuel_amount', '$fuel_month', '$fuel_day', '$fuel_year')

 

Can you explain again what exactly you are trying to do? Although I have pointed out a serious problem, I still cannot understand your purpose. If you want the value from a SELECT query to go into your INSERT query, then why are you SELECTing everything, that makes no sense unless the tables expense_fuel and expense_categories have the exact same columns....which I highly doubt.

 

 

It would help you more than us tremendously if you post your database layout and some sample data.

Edited by Zane
Link to comment
https://forums.phpfreaks.com/topic/269078-i-must-be-stupid/#findComment-1382708
Share on other sites

Don't use "SELECT * ". Specify what you want to select, in this case the fuel code.

 

$sql = 'SELECT fuel_code FROM expense_categories WHERE type = $fuel_type';
$res = mysql_query($sql);
if (mysql_num_rows($res) > 0)
   $fuel_code = mysql_result($res, 0);

Link to comment
https://forums.phpfreaks.com/topic/269078-i-must-be-stupid/#findComment-1382711
Share on other sites

Hey guys, ok Basically Im adding expense tracking and reporting to the company portal iv been working on all year for my landscaping & tree service company. I have a form on the portal for fuel expenses, theres going to be more added prob next week, but i want to get this one done first.

 

So I have 2 seperate DB tables, 1 is expense_categories, the other is expense_fuel

 

the expense_categories only has 2 fields, the id field witch is the unique 3 digit number (gasoline is 001) and the type (gasoline) all of the expense codes are in this table, not just fuel. there are 3 fuel types that pertain to the form that i posted, diesel, gasoline, and cycle oil.

 

the expense_fuel table has 6 fields, id (this will be the 3 digit code), fuel_type (gasoline, diesel, cycle oil), amount (obvious), month, day, year

 

i was working on this quick this morning before i ran out of the house to my night job, i was thinking about it at work tonight. basically i have a select box on the form that selects the 3 types of fuel, what i need to do is search in the category table for the name, pull the id code, and stick it in the expense_fuel record along with the date, amount, etc. but as i said, i was thinking about it tonight, and i think i kinda did it in reverse, i should have posted the fuel code and matched a name to the code on the report and manipulation, not in the db itself, no purpose for it to be in the expense_fuel category at all actually, just need to stick the code there. i might try to just do some java later and figure out how to put it so that the user select box is shown as the text, gasoline, diesel, cycle oil, and when they submit the value is actually 001, 002, 003, then the reports will just do a sql query to match 001 to anything applied in the expense_categories table, and pull the name of it, diesel, etc.

 

Im going to mess with it more tommorrow, like i said i was in a rush and i think i realized a few of the stupid mistakes i made with it, just too tired to sort through it now, plus i havent worked with php since around december since i started the business lol. been too busy.

 

i hope that gives you a bit more info about what im trying to do, tommorrow im going to mess with it, if i figure out what im trying to do ill post it here, or if you figure it out first great lol =)

Edited by Angeleyezz
Link to comment
https://forums.phpfreaks.com/topic/269078-i-must-be-stupid/#findComment-1382811
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.