UK WEB HOSTING FORUM FOR DISCUSSION ON WEB HOSTING SERVICE AND SUPPORT
LINUX HOSTING WINDOWS HOSTING PACKAGES SHOPPING CART OSCOMMERCE ZEN CART AGORA
ECOMMERCE HOSTING ASP MSSQL FRONTPAGE HOSTING PHP MYSQL HOSTING DISCUSSION FORUM
CPANEL RESELLER HOSTING DEDICATED SERVER VPS HOSTING PLESK VIRTUOZZO
Quick Search
Your forum announcement here!

  UK Web Hosting | Dedicated Server Windows and Linux VPS Forum > Web Hosting and Domains > PHP Hosting

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-04-2008, 01:48
Junior Member
 
Join Date: Dec 2005
Location: UK
Posts: 19
Unhappy Referencing $_POST data against MySQL

Firstly, apologies for being rubbish. I'm not particularly fluent with PHP, i've just been working with tutorials and manuals etc from the web. Bare with me while I wobble out an explanation attempt....

Basically I have a form (www.karlgunton.com/fluid) which has the contents auto generated from a MySQL database. On submitting the form, I just want it to go to a "total" page which will then just get emailed (not looking forward to figuring that out).

So i've got the form to submit an ID for the item in the database, and a price choice number (the amount to multiply the price in the database by) which i then just want to reference against the mysql database, so then i can output all the details again neatly & nicely, then hopefully add a bit of maths to give a total.

How on earth do i get the array from the $_POST turned into auto generated lines of mysql output?

ie. array output:
ID - amount
2 - 1
4 - 2

mysql equivalent:
name & price on row of id
Budwieser 10.99
Carlsberg 21.98

It might make more sense if you view the url and click submit.

The initial page with the drinks:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Language" content="en-gb" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>mysql table test</title>
</head>
<body>
<br><br>


<?php

$link = mysql_connect( "localhost", "", "" );
if ( ! $link ) {
  die( "Couldn't connect to MySQL" );
};
$database = "karlg_fluid";
mysql_select_db( $database ) or die (  "Couldn't open database: ".mysql_error() );

 $beer = "SELECT * FROM drinks where producttype='beer'";
 $beerboxes = mysql_query($beer) or die(mysql_error());  

?>
<center>

<form action="total.php" method="post" name="form2" id="form2" >


<table height="249" cellspacing="0" cellpadding="0" width="802" border="0" id="table3">
<tbody>
<tr><td width="736" height="65"><img class="" height="65" alt="" width="802" border="0" src="images/Fluid-DrinkCategoryBox_TOP-Beers.gif" /></td></tr>
<tr>
<td width="802" background="images/Fluid-DrinkCategoryBox_MIDDLE.gif" height="172" align="center">
<table id="table4" height="197" cellspacing="0" cellpadding="0" width="802" border="0">
<tbody>
<tr>                    
<?php                   
// START AUTO GENERATION OF TABLES FOR CATEGORY
while ($a_row=mysql_fetch_array($beerboxes)) 
{
print '
<td>
<p align="center"><img border="0" src="images/'.stripslashes($a_row['imageurl']).'">
<br>
<font size="3"><font face="Arial">'.stripslashes($a_row['productname']).'</font>
<br>
<font size="1"><font face="Arial">'.stripslashes($a_row['productinfo']).'</font>
<br>
<select size="1" name="'.stripslashes($a_row['id']).'">
<option>Choose</option>
<option value="1">One - £'.stripslashes($a_row['productprice']).'</option>
<option value="2">Two - £'.stripslashes(format_number($a_row['productprice'] *2)).'</option>
<option value="3">Three - £'.stripslashes(format_number($a_row['productprice'] *3)).'</option>
<option value="4">Four - £'.stripslashes(format_number($a_row['productprice'] *4)).'</option>
<option value="5">Five - £'.stripslashes(format_number($a_row['productprice'] *5)).'</option>
</select>

</td>
';				
}
?>	
</tr>
</tbody>
</table>
</td>
</tr>
<tr><td width="736" height="12" align="center"><img class="" height="12" alt="" width="802" border="0" src="images/Fluid-DrinkCategoryBox_BOTTOM.gif" /></td></tr>
</tbody>
</table>
<br><br>


[rest of the boxes not added yet]



<input type="submit" name="Submit" value="not setup yet">

</p>
</form>
</center>
</tr>
</table>
</body>
</html>
the current nothingness of total page
Code:
<table cellspacing="0" cellpadding="2">
<tr> 
 <td><u>Drink</u></td>
 <td><u>Amount</u></td> 
</tr> 
<?php 
while (list ($drinkid, $drinkprice) = each($_POST))
 if($drinkprice != "Choose" AND $drinkid != "Submit") {   // stops from echoing irrelevant fields
 { 
 echo "<tr><td> "; 
 echo $drinkid; 
 echo " (i want to find this id in mysql then output name, desc etc from that)";
 echo "</td><td>";
 echo $drinkprice; 
 echo " (amount to multiply price found in the mysql table)";
 echo "</td></tr>"; 
 } 
echo "<br />";
} 

?>
</table>
Apologies if it seems like i'm trying to get someone to 'write me a script', but i just cant find anything on the web for this kind of thing.

Most scripts assume i have two constant results in the array like 'NAME - Bob' (for example) which i know would just be a sort of find $_POST[name] in database type of thing, but i dont have a 'name' field

...or something.

My head hurts.
__________________
http://www.karlgunton.com

Last edited by flesso : 10-04-2008 at 11:26.
Reply With Quote
  #2 (permalink)  
Old 10-04-2008, 10:03
DavidAllen's Avatar
Premium Member
 
Join Date: Jan 2007
Location: Amersham
Posts: 315
Send a message via MSN to DavidAllen Send a message via Skype™ to DavidAllen
Default

Hi Karl
Firstly you might want to edit your post to hide the database logon details (and change them on the database) otherwise you will find that some unscrupulous type will hack your database.
Secondly you don't need all those 'stripslashes' - that is for recieving user input not output from the database.
Thirdly to answer your question - you just need to use some sql like:
PHP Code:
$sql 'select * from drinks where id = '.$drinkid
make the call to the database - get the price of that drink and multiply by $drinkprice.
__________________
David Allen - www.serina.co.uk
Reply With Quote
  #3 (permalink)  
Old 10-04-2008, 11:27
flesso's Avatar
Premium Member
 
Join Date: Mar 2007
Location: 127.0.0.1
Posts: 1,137
Send a message via MSN to flesso Send a message via Skype™ to flesso
Default

For your website's security, and as mentioned by David, I have removed your database's username and password from your post.
__________________
Regards,
Josh Hold

eUKhost Blog: Over 1000 Computer Related Articles to Sink Your Teeth Into!

Super Moderator

I'm only a moderator, and do not work for eUKhost in any way. Opinions expressed by me are mine only, and do not reflect those of either eUKhost or any company that may be listed above.
Reply With Quote
  #4 (permalink)  
Old 10-04-2008, 13:02
Junior Member
 
Join Date: Dec 2005
Location: UK
Posts: 19
Default

Quote:
Originally Posted by flesso View Post
For your website's security, and as mentioned by David, I have removed your database's username and password from your post.
Thanks, that wasn't intentional, it was 3 in the morning and id just about had enough, i've also changed the details now too.

Cheers so much DavidAllen, that one line of code has put me back on track now, i think i can actually do what i wanted. (& the headsup on the stripslashes thing)
__________________
http://www.karlgunton.com

Last edited by KarlG : 10-04-2008 at 13:25.
Reply With Quote
  #5 (permalink)  
Old 10-04-2008, 13:29
Premium Member
 
Join Date: Mar 2007
Posts: 394
Default

I'd suggest changing your username/password for that database - just to be on the safe side. It was on display for quite a while.
__________________
http://shubox.net
Reply With Quote
  #6 (permalink)  
Old 10-04-2008, 13:39
Junior Member
 
Join Date: Dec 2005
Location: UK
Posts: 19
Default

Did do as soon as i read this reply, the old settings are deleted, cheers.

I had a format_number() function before that would keep the trailing 0 on a price "ie £8.70, not 8.7" that doesnt work on print '£'.($arow['productprice']) * $drinkprice.'';, is there any simple way to stop it removing the end 0?

i assumed print '£'. format_number(($arow['productprice']) * $drinkprice).'';, would work, but it doesnt.

i really should stick to making music i think.
__________________
http://www.karlgunton.com

Last edited by KarlG : 10-04-2008 at 13:49.
Reply With Quote
  #7 (permalink)  
Old 10-04-2008, 14:25
DavidAllen's Avatar
Premium Member
 
Join Date: Jan 2007
Location: Amersham
Posts: 315
Send a message via MSN to DavidAllen Send a message via Skype™ to DavidAllen
Default

PHP Code:
print '&pound;'.format_number(($arow['productprice']*$drinkprice),2); 
should do it - the key point is the 2 in format_number which says howmany decimal places to output
__________________
David Allen - www.serina.co.uk
Reply With Quote
  #8 (permalink)  
Old 10-04-2008, 15:57
Junior Member
 
Join Date: Dec 2005
Location: UK
Posts: 19
Default

ah thats brilliant, thanks loads!
__________________
http://www.karlgunton.com
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:54.

 

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by Web Hosting 3.1.0
Copyright © 2001-2008, eUKhost.com. All rights reserved.

 
Site Map

knowledgebase articles

popular blog categories