Howdy, Stranger!

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

Categories

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

lionreddylionreddy Member Posts: 14
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






«13

Comments

  • tradmtradm Member Posts: 49
    $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.

  • lionreddylionreddy Member Posts: 14
    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
  • lionreddylionreddy Member Posts: 14
    This post has been deleted.
  • tradmtradm Member Posts: 49
    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.






  • lionreddylionreddy Member Posts: 14
    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....
  • tradmtradm Member Posts: 49
    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]


  • lionreddylionreddy Member Posts: 14
    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..


  • lionreddylionreddy Member Posts: 14
    This post has been deleted.
  • tradmtradm Member Posts: 49
    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]









  • lionreddylionreddy Member Posts: 14
    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??
«13
Sign In or Register to comment.