Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 11th, 2013, 02:03 PM
Authorized User
 
Join Date: Apr 2013
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Default passing selected data to database

Someone please tell me what I'm doing wrong. It doesn't work.
HTML Code:
<HTML>
<body bgcolor="#ccffff">
<FORM name=Form action="rateselect.php" method=post>
<B><center>
Set tax rate <SELECT name=taxrate>
<OPTION value=0.0000 selected>0.0%
<OPTION value=0.02900>2.9%
<OPTION value=0.04000>4.0%
<OPTION value=0.04225>4.225%
<OPTION value=0.04500>4.5%
<OPTION value=0.04700>4.7%
<OPTION value=0.05000>5.0%
<OPTION value=0.05300>5.3%
<OPTION value=0.05500>5.5%
<OPTION value=0.05600>5.6%
<OPTION value=0.05750>5.75%
<OPTION value=0.06000>6.0%
<OPTION value=0.06250>6.25%
<OPTION value=0.06500>6.5%
<OPTION value=0.06850>6.85%
<OPTION value=0.06875>6.875%
<OPTION value=0.07000>7.0%
<OPTION value=0.08250>8.25%
</OPTION></SELECT><p>
<CENTER>
<INPUT type=image height=24 alt="submit button" width=129 src="rollsubmit.gif" 
        border=0>
</CENTER>
</FORM></B></BODY></HTML>
PHP Code:
<?php
if (isset( $_POST['taxrate']) ) 
{
$taxrate=$_POST['taxrate'];
}
mysql_connect("localhost","root","");
mysql_select_db('numbersdb') or die( "Unable to select database");
print 
$_POST['taxrate'];
$query "
INSERT INTO numbdata (taxrate)
VALUES('
$taxrate')";
echo 
"data inserted</font><br /><br />"
$stat mysql_query($query) or die('Query failed: ' mysql_error()); 
mysql_close();
?
 
Old October 17th, 2013, 12:47 AM
Registered User
 
Join Date: Oct 2013
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
Default Re: putting data in a database

Hello,

What exactly do you want to do? If you want to update the table, you have to use update query or you will have to use insert to insert new record.
Regard
 
Old October 17th, 2013, 12:54 AM
Registered User
 
Join Date: Oct 2013
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
Default Re: putting data in a database

Oh sorry, I didn't read the code to the end. What problem do you have with the code? I think you should use update instead of insert and since you are define a particular column in a table, you should just update. Or what do you think?
The Following User Says Thank You to Adebayo For This Useful Post:
ataloss (October 17th, 2013)
 
Old October 17th, 2013, 04:17 PM
Authorized User
 
Join Date: Apr 2013
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Default

I can't seem to get the code right
PHP Code:
<?php 
// error_reporting(0);
error_reporting(E_ALL E_NOTICE); 
mysql_connect("localhost","root",""); 
mysql_select_db("numbersdb") or die( "Unable to select database"); 
if(!empty(
$_POST["submit"])) 

 
$taxrate $_POST['taxrate']; 
 
$query="SELECT * FROM numbdata Where id='$id'"
 
$result=mysql_query($query); 
 if(
mysql_num_rows($result)) 
 {  
}else{echo 
"No listing for taxrate $taxrate .<br />Select another? .<br />";} 

if(!empty(
$_POST["update"])) 

$sql "UPDATE numbdata SET 
taxrate = '" 
.mysql_real_escape_string($_POST['taxrate']) . "', 
WHERE taxrate='"
.$_POST['taxrate']."'"
mysql_query($sql) or die(mysql_error());
 echo 
"Record for taxrate ".$_POST["taxrate"]." has been updated"
 } 
?>
 
Old October 18th, 2013, 11:25 AM
Authorized User
 
Join Date: Apr 2013
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Default

I have a one record database table (numbdata)with fields such as taxrate.
I'm trying to select one option using HTML, pass it to PHP and
update the one field (taxrate)in numbdata.
 
Old October 22nd, 2013, 06:47 PM
Authorized User
 
Join Date: Apr 2013
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Default

hello all, Below is my current code w/response. I hope for a solution, thanks
Quote:
GA is invalid.
Select another ?

Abbreviate; NY, CA, etc;
PHP Code:
<html><body bgcolor="ccffff"><center>
<?php
error_reporting
(E_ALL E_NOTICE);
mysql_connect("localhost","root","");
mysql_select_db("taxdb") or die( "Unable to select database");
if(!empty(
$_POST["submit"]))
      {
 
$state $_POST['state'];
$taxrate $_POST['taxrate'];
 
$query="SELECT * FROM taxtbl Where id='$id'";
 
$result=mysql_query($query);
 if(
mysql_num_rows($result))
      {
  echo 
"<form action='#' method='post'><b><br /><br />
    <table border='1'>
    <tr>
<th>state</th>
    </tr>"
;
  while(
$row mysql_fetch_assoc($result))
      {
   echo 
"<tr>
<td><input type='text' size=2 name='state' value='" 
$row['state'] . "'></td>
   </tr>"
;
      }
   echo 
"</table>
<input type='submit' name='update' value='Update Record' />
   </form>"
;
      } 
else{echo 
"$state is invalid.<br />Select another ?<br />";}
      }
if(!empty(
$_POST["update"]))
      {
$sql "UPDATE taxtbl SET
 taxrate = '" 
mysql_real_escape_string($_POST['taxrate']) . "', 
    WHERE state'"
.$_POST["state"]."'";
    
mysql_query($sql) or die("Update query failed.");
    echo 
"tax rate".$_POST["state"]." has been set ...";
      }
?>
<form method="post" action="#"> <br />
Abbreviate; 
 <input type="text" name="state"/>NY, CA, etc;  <p>
<input type="submit" name="submit" value="make selection"/>
</form>
</center></BODY></HTML>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing a hardcoded value and manipulating data before passing it to Database table Abhishek Iyer Biztalk 0 August 17th, 2010 06:45 AM
Another There is a problem with your selected data stevensrf1 ASP.NET 2.0 Basics 0 August 4th, 2007 02:02 PM
No Database selected... jimboroberts BOOK: Professional Search Engine Optimization with PHP: A Dev's Guide to SEO ISBN: 978-0-470-10092-9 0 June 25th, 2007 03:12 AM
run database query when drop-down is selected crmpicco Classic ASP Basics 4 August 9th, 2005 12:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.