PHP

Moderators: None (Apply to moderate this forum)
Number of threads: 1847
Number of posts: 5013

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP.. Posted by lionreddy on 1 Aug 2009 at 10:49 PM
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");
}
}

?>

<html>
<head><title>sub</title>
</head>
<body>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<tr><td align="right">earnings:</td>
<td align="left">
<select name="dropdown">
<option value="default">--select an earning--</option>
<option value="hra">HRA</option>
<option value="da">DA</option>
</select>
<td><input type="text" value=""></td></tr>
<tr><td><input type="submit" name="addDatatoDB" value="add to database"></td></tr>
</form>
</body>
</html>
Report
Re: INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP.. Posted by tradm on 3 Aug 2009 at 12:10 AM
$earnings will have any of the three values default, hra or da.

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

Revise your SQL statement.

Report
Re: INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP.. Posted by lionreddy on 3 Aug 2009 at 6:33 AM
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
Report
This post has been deleted. Posted by lionreddy on 3 Aug 2009 at 6:33 AM
This post has been deleted.
Report
Re: INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP.. Posted by tradm on 3 Aug 2009 at 7:08 AM
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.






Attachment: mike.pdf (27094 Bytes | downloaded 270 times)
Report
Re: INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP.. Posted by lionreddy on 8 Aug 2009 at 9:39 PM
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....
Report
Re: INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP.. Posted by tradm on 9 Aug 2009 at 10:59 PM
Change your code by adding the green colored parts:

<html>
<head><title>sub</title>
</head>
<body>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<tr><td align="right">earnings:</td>
<td align="left"> 
<select name="dropdown">
<option value="default">--select an earning--</option>
<option value="hra">HRA</option>
<option value="da">DA</option>
</select>
<td><input type="text" value="" name="someTextBoxName"></td></tr>
<tr><td><input type="submit" name="addDatatoDB" value="add to database"></td></tr>
</form>
</body>
</html>



And

<?

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'];
$myTextBoxValue = $_POST['someTextBoxName'];

//$query = "INSERT INTO ins (hra,da) VALUES ('" .$earnings. "')";
$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
                );


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

?>



Report
Re: INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP.. Posted by lionreddy on 10 Aug 2009 at 1:39 AM
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..


Report
This post has been deleted. Posted by lionreddy on 10 Aug 2009 at 1:40 AM
This post has been deleted.
Report
Re: INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP.. Posted by tradm on 10 Aug 2009 at 2:17 AM
METHOD 1:
---------

<?
...
mysql_query($query)...
...
?>


Insert the code below after the closing PHP tag ?>

<br>
<h1><font color="green">Congatulations!</font></h1>
The values were successfully saved!<br>
Earnings : <b> <?php echo $earnings; ?> <br>
The other field : <b> <?php echo $phpVariableHoldingThatField; ?>
<i> You can add some nice html code , include pictures etc :)



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

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


<br>
<h1><font color="green">Congatulations!</font></h1>
The values were successfully saved!<br>
Earnings : <b> <?php echo $_GET['var1']; ?> <br>
The other field : <b> <?php echo $_GET['var2']; ?>
<i> You can add some nice html code , include pictures etc :)



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

header("Location:acknowledge.php?var1=".$earnings."&var2=".$amountVariable);










Report
Re: INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP.. Posted by lionreddy on 10 Aug 2009 at 3:29 AM
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");
}
}

?>


<html>
<head><title>sub</title>
</head>
<body>
<form action="<?echo$_SERVER['PHP_SELF'];?>" method="post">
<tr><td align="right">earnings:</td>
<td><input type="text" name="earn" value=""></td></tr>
<td align="left">
<select name="dropdown">
<option value="default">--select an earning--</option>
<option value="hra">HRA</option>
<option value="da">DA</option>
</select>
<tr><td><input type="submit" name="addDatatoDB" value="add to database"></td></tr>
</form>
</body>
</html>


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??
Report
Re: INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP.. Posted by tradm on 10 Aug 2009 at 4:59 AM
Its abit trick to show the acknowledgement frame without use of PHP.

Does the attached script meet your requirements?

Attachment: script.php (1673 Bytes | downloaded 188 times)
Report
Re: INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP.. Posted by lionreddy on 10 Aug 2009 at 7:49 AM
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

Attachment: lion.php (6190 Bytes | downloaded 175 times)
Report
Re: INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP.. Posted by tradm on 10 Aug 2009 at 11:18 PM

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


<?  
    session_start();
   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]')"; 
//header("Location:earn.php?var1=".$earn."&var2=".$dropdown);
   $result=mysql_query($query); 
   if(mysql_errno())    { 
   die("failed");    } 
   if(mysql_affected_rows()!=1)    { 
   die("failed to addd"); 
   } 
    
   $_SESSION['itemsAddedToDB'][] = array("earn"=>$_POST[earn],"dropdown"=>$_POST[dropdown]);
} 
   
   
   
?> 
   

 <html> 
   <head><title>sub</title> 
   </head> 
   <body> 
   <form action="<?echo$_SERVER['PHP_SELF'];?>" method="post" onsubmit="parent.location.reload()"> 
   <tr><td align="right">earnings:</td> 
   <td><input type="text" name="earn" value=""></td></tr>   
   <td align="left"> 
   <select name="dropdown"> 
   <option value="default">--select an earning--</option> 
   <option value="hra">HRA</option> 
   <option value="da">DA</option> 
   </select> 
   <tr><td><input type="submit" name="addDatatoDB" value="add to database"></td></tr> 
   </form> 
   </body> 
   </html> 



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.
Attachment: earn.php (648 Bytes | downloaded 163 times)
Report
Re: INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP.. Posted by lionreddy on 11 Aug 2009 at 1:33 AM
heyy mike

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

thanks

Report
Re: INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP.. Posted by tradm on 11 Aug 2009 at 2:36 AM
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
Report
Re: INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP.. Posted by lionreddy on 11 Aug 2009 at 3:53 AM
sorry

actually mike.. the values are not gettin inserted to database also..


Report
Re: INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP.. Posted by tradm on 11 Aug 2009 at 10:21 PM
Hie,

Change the parameters for mysql_connect(), mysql_select_db() and ensure that the table being referenced in the query is existent.

Regards,
Mike.
Report
Re: INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP.. Posted by lionreddy on 12 Aug 2009 at 12:24 AM
hey mike


thanks a ton man! it works:)
thanks a lot man!!! and jus one more thing

now that my values are displayed in earn.php

how do i ADD all the values that i entered???


regards
Report
Re: INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP.. Posted by tradm on 12 Aug 2009 at 12:47 AM
Hie,

Instead of reading from session variable, earn.php should now get the values in the DB. If I could have the structure of your database table where the earnings are saved, I can help better.

Am online on Skype.

regards,
Mike
Report
Re: INSERT VALUE FROM DROP DOWN BOX TO THE DATABASE USING PHP.. Posted by pipercub on 9 May 2010 at 5:59 PM
Hello Mike; I have a similar issue.

I have a mysql database with ID numbers. The user has to select a status for an individual record (id number). The statuses of the records could be any 4 background colors which mean different things. I created a dropdown box with the different colors and then created a select dropdown box with the id numbers. The user selects the id number (row number), which is the row number and the bgcolor and submits to the PHP server for the action of the table.
This works fine except the row does not keep the color when you exit the web and come back again. The table does not show the bgcolors the user selected for the row. I need the row bgcolor to stay for an individual row number. I used Javascript to pass the variables for the user selections with a $_GET[‘rowNum’] and $_GET[‘statColor’]. How can I make the row bgcolor stay after the user exits the web and come back to check what he selected. I also used ‘” . bgcolor . “ within the while statement when comparing the ID numbers. Thanks for any help you can provide.

Report
This post has been deleted. Posted by madison12 on 20 May 2010 at 11:00 PM
This post has been deleted.
Report
This post has been deleted. Posted by madison12 on 20 May 2010 at 11:01 PM
This post has been deleted.
1 2  Next



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.