INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP..

PLZZ HELP ME OUT!!!!!!!!!!!

i jus don understand wads wrong wid my code...i tried out everything but the values are jus not getting inserted into the database....... plss help me....

here is the code..
<?

if ($_POST['addDatatoDB'])
{

require_once('config.php');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}

$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}

$earnings = $_POST['dropdown'];

$query = "INSERT INTO ins (hra,da) VALUES ('" .$earnings. "')";
$result = mysql_query($query);
if(mysql_errno())
{
die("failed");
}
if (mysql_affected_rows() != 1)
{
die("failed to addd");
}
}

?>


sub


" method="post">
earnings:


--select an earning--
HRA
DA






«1

Comments

  • $earnings will have any of the three values [italic]default, hra [/italic] or [italic] da[/italic].

    This implies that at run time, assuming $earnings is hra, the statement
    [b]$query = "INSERT INTO ins (hra,da) VALUES ('" .$earnings. "')";[/b]
    will evaluate to
    [b]$query = "INSERT INTO ins [color=Blue](hra,da)[/color] VALUES ([color=Red]'hra'[/color])";
    [/b]which is wrong.

    Revise your SQL statement.

  • but ive tried everything i know.......

    $query ="INSERT INTO ins(earnings) VALUES('$earnings')";

    actually ive tried revising it in many ways...but result is the same:(....

    plz can u gimme the code for inserting values from dropdown box to database using php...

    it will be really helpful
  • This post has been deleted.
  • Oky,

    I am thinking that you are still getting a problem with your SQL.
    Here is what u should do.
    - Create a table, call it test1 with 1 column lets call it col1 ( datatype VARCHAR 50 )
    - Change your SQL to
    $query = "INSERT INTO test1 (col1) VALUES ('" .$earnings. "')";
    That shud work.


    For your Information.
    I created a table called mike with a column called a in the database mysql and the attached code worked fine.






  • thanks a lot..

    but i still seem to hav a problem. only the value in the dropdownbox ie hra or da( acc to the above code) is getting inserted....the value that im entering next to hra or da is not getting entered,,

    basically its a payroll system where i haveto enter the salaries of the employees...the dropdown box is for the operator to select the category of pay....house rent allowance(hra) etc....in the next text field the salary value is entered and it shoul be stored in the database along with the category




    pls help mike....
  • Change your code by adding the green colored parts:

    [code]
    sub


    " method="post">
    earnings:


    --select an earning--
    HRA
    DA





    [/code]


    And

    [code]<?

    if ($_POST['addDatatoDB'])
    {

    require_once('config.php');

    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if(!$link) {
    die('Failed to connect to server: ' . mysql_error());
    }

    $db = mysql_select_db(DB_DATABASE);
    if(!$db) {
    die("Unable to select database");
    }

    $earnings = $_POST['dropdown'];
    [b][color=Green]$myTextBoxValue = $_POST['someTextBoxName'];[/color][/b]

    //$query = "INSERT INTO ins (hra,da) VALUES ('" .$earnings. "')";
    [color=Green][b]$query = sprintf("INSERT INTO myTwoColumnTable (column1, column2) VALUES ('%s','%s')",
    $earnings,
    $myTextBoxValue
    ); //Just a smarter way anyway
    //OR
    $query = sprintf("INSERT INTO myOneColumnTable (Column1) VALUES ('%s')",
    $myTextBoxValue
    );[/b]
    [/color]

    $result = mysql_query($query);
    if(mysql_errno())
    {
    die("failed");
    }
    if (mysql_affected_rows() != 1)
    {
    die("failed to addd");
    }
    }

    ?>[/code]


  • thanks a ton mike!!!

    and jus one more thing man...
    now the values are in the database. how do i print them or display in another frame simultaneously????

    as soon as i click addtodatabase button...the values must be stored and they must be displayed....atlast i add all the values..


  • This post has been deleted.
  • METHOD 1:
    ---------

    <?
    ...
    mysql_query($query)...
    [b]...
    ?>[/b]

    Insert the code below after the closing PHP tag [color=Blue][b]?>[/b][/color]

    [color=Blue]

    Congatulations!

    The values were successfully saved!

    Earnings : <?php echo $earnings; ?>

    The other field : <?php echo $phpVariableHoldingThatField; ?>
    You can add some nice html code , include pictures etc :)
    [/color]


    METHOD 2:
    ---------

    Create a file say 'acknowledge.php' with the following code;


    [color=Orange]

    Congatulations!
    The values were successfully saved!

    Earnings : <?php echo $_GET['var1']; ?>

    The other field : <?php echo $_GET['var2']; ?>
    You can add some nice html code , include pictures etc :)[/color]


    call this page like this within PHP (You would logically call it after executing the insert SQL statement).

    [color=Purple]header("Location:acknowledge.php?var1=".$earnings."&var2=".$amountVariable);
    [/color]









  • where should i specify the link for that frame??
    im sendin wat ive done.. pls help

    <?
    if($_POST['addDatatoDB'])
    {

    $link=mysql_connect("localhost","siva","siva");
    if(!$link){
    die('Failed to connect to server: '.mysql_error());
    }

    $db=mysql_select_db("empinfo");
    if(!$db){
    die("Unable to select database");
    }



    $query="INSERT INTO test1 (col1,col2) VALUES ('$_POST[earn]','$_POST[dropdown]')";
    $result=mysql_query($query);
    if(mysql_errno()) {
    die("failed"); }
    if(mysql_affected_rows()!=1) {
    die("failed to addd");
    }
    }

    ?>



    sub


    " method="post">
    earnings:



    --select an earning--
    HRA
    DA







    i have to create something like this for deductions too.. and same thing must be done ie values must be printed in another frame...
    wer do i specify the links for the two separate frames...one for earnings and other for deductions??
  • Its abit trick to show the acknowledgement frame without use of PHP.

    Does the attached script meet your requirements?

  • mike jus check out the attachements im sending....
    first i login...
    after that u enter a page thats divided into frames( bill.php)..now when i add earnings into database....the same must be immediatly printed in another frame ( earn.php in this case)....
    similarly deductions are added into deduce database,,,

    jus enter some records in the database and run them...u will know wad i want..

    thanx a lot man


  • Solution: Make the highlighted changes and modify earn.php to suit your display requirements.


    [code]<?
    [b] [color=Green]session_start();[/color][/b]
    if($_POST['addDatatoDB'])
    {

    $link=mysql_connect("localhost","username","password");
    if(!$link){
    die('Failed to connect to server: '.mysql_error());
    }

    $db=mysql_select_db("dbname");
    if(!$db){
    die("Unable to select database");
    }



    $query="INSERT INTO test1 (col1,col2) VALUES ('$_POST[earn]','$_POST[dropdown]')";
    [b][color=Red]//[/color][color=Grey]header("Location:earn.php?var1=".$earn."&var2=".$dropdown);[/color][/b]
    $result=mysql_query($query);
    if(mysql_errno()) {
    die("failed"); }
    if(mysql_affected_rows()!=1) {
    die("failed to addd");
    }

    [b][color=Green]$_SESSION['itemsAddedToDB'][] = array("earn"=>$_POST[earn],"dropdown"=>$_POST[dropdown]);[/color][/b]
    }



    ?>



    sub


    " method="post" [b][color=Green]onsubmit="parent.location.reload()"[/color][/b]>
    earnings:



    --select an earning--
    HRA
    DA




    [/code]


    TIP:
    (WHEN/If) it works, you may consider to change earn.php to dynamically retreive the values from
    the database table where the values are being saved instead of session.
  • heyy mike

    the values are jus gettin added to the database...they are not bein displayed...

    thanks

  • 1. Just make sure you have session_start(); as the first line in "in.php" and "earn.php"

    2. Start by using the version of earn.php I've sent?

    3. You can also try to debug by adding this line in earn.php before the foreach statement...
    print_r($_SESSION['itemsAddedToDB']);




    My SkypeID : mchibaka
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories