Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › PHP While Loop's and $_POST - Help
New Posts  All Forums:Forum Nav:

PHP While Loop's and $_POST - Help

post #1 of 6
Thread Starter 
Hi There,

On the home straight of finishing my first website - Haven't had much sleep and I've hit a wall.

I was wondering if any of you knew how to Get $_POST from a while loop.

More Detail.

I have an offers page and and a account page. In between them is a submit page which sends to mysql.

On the offers page I have this while loop
Code:
<?php
$currentqry = mysql_query("SELECT * FROM listings WHERE uid = '$currentuser'");

while($row = mysql_fetch_array($currentqry))
{
        echo '<li class="styled"><input id="buylist" name="buylist" class="check" type="checkbox" value="'.$row['title'].'" >&nbsp;'.$row['title'].'</input></li><br>';    
}

?>

Now what I want to do is get $_POST from "buylist" and store this in the mysql table so I can read it on the account page when required.

Im trying to do this with the PHP script
Code:
$buy = $_POST['buylist'];
$buylist = array($buy);
  

foreach($buylist as $key)
{
        $allkey = $key . ", ";        
}


$rallkey = substr($allkey, 0, -2);



// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

$offer = mysql_query("INSERT INTO `offers`(`id`, `sellerid`, `sellerlistid`, `buyerid`, `buyerlistid`, `buyercustom`, `sellercustom`, `accepted`, `pending`, `time`) VALUES (NULL,'$selluser','$sellid','$buyuser','$rallkey','$_POST[cash]','empty','0','1',NULL)") or die('Query failed: ' . mysql_error() . "<br />\n$sql");

//echo header("Location:account.php?u=".$_SESSION['username']);

echo $rallkey;


Which works for all apart from returning values of all checked checkboxes as a comma separated string so I can insert into my mysql table.

Can anyone help please?
post #2 of 6
I'm afraid I'm lost as to what you want to know. What I can decipher is that you want to put the contents of the variable "buylist" into your SQL table?

I don't know. Can you point me to the code that isn't working?
Daily
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K @ 4.8GHz, 1.4V Maximus IV Extreme GTX 560 DCII TOP 4x4GB Kingston HyperX 1866MHz CL11 
Hard DriveHard DriveCoolingCooling
Kingston HyperX 3K 120GB Samsung 640GB Thermochill PA120.2 Jingway DB-1 Pump 
CoolingCoolingOSMonitor
2x Kaze Jyuni 1900RPM EK Supreme HF Copper plexi Arch Linux x64 Acer X223HQ 1920x1080 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750 Modded Corsair Carbide 500R Logitech M500 
  hide details  
Reply
Daily
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K @ 4.8GHz, 1.4V Maximus IV Extreme GTX 560 DCII TOP 4x4GB Kingston HyperX 1866MHz CL11 
Hard DriveHard DriveCoolingCooling
Kingston HyperX 3K 120GB Samsung 640GB Thermochill PA120.2 Jingway DB-1 Pump 
CoolingCoolingOSMonitor
2x Kaze Jyuni 1900RPM EK Supreme HF Copper plexi Arch Linux x64 Acer X223HQ 1920x1080 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750 Modded Corsair Carbide 500R Logitech M500 
  hide details  
Reply
post #3 of 6
Thread Starter 
Yep I want to put the value of the checked checkboxes buylist into my mysql table.

I guess the code im having trouble with is:
Code:
$buy = $_POST['buylist'];
$buylist = array($buy);
  

foreach($buylist as $key)
{
        $allkey = $key . ", ";        
}


$rallkey = substr($allkey, 0, -2);


I need all the checked vaules from the below while loop in my table as one row.

Ive got $rallkey set to be input to the table at the moment
Code:
<?php
$currentqry = mysql_query("SELECT * FROM listings WHERE uid = '$currentuser'");

while($row = mysql_fetch_array($currentqry))
{
        echo '<li class="styled"><input id="buylist" name="buylist" class="check" type="checkbox" value="'.$row['title'].'" >&nbsp;'.$row['title'].'</input></li><br>';    
}

?>

Does this help?
Edited by pr0bie - 2/1/12 at 1:14pm
post #4 of 6
I'm afraid I'm still lost. Sorry, I can't help.

Bump for more helpers smile.gif
Daily
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K @ 4.8GHz, 1.4V Maximus IV Extreme GTX 560 DCII TOP 4x4GB Kingston HyperX 1866MHz CL11 
Hard DriveHard DriveCoolingCooling
Kingston HyperX 3K 120GB Samsung 640GB Thermochill PA120.2 Jingway DB-1 Pump 
CoolingCoolingOSMonitor
2x Kaze Jyuni 1900RPM EK Supreme HF Copper plexi Arch Linux x64 Acer X223HQ 1920x1080 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750 Modded Corsair Carbide 500R Logitech M500 
  hide details  
Reply
Daily
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K @ 4.8GHz, 1.4V Maximus IV Extreme GTX 560 DCII TOP 4x4GB Kingston HyperX 1866MHz CL11 
Hard DriveHard DriveCoolingCooling
Kingston HyperX 3K 120GB Samsung 640GB Thermochill PA120.2 Jingway DB-1 Pump 
CoolingCoolingOSMonitor
2x Kaze Jyuni 1900RPM EK Supreme HF Copper plexi Arch Linux x64 Acer X223HQ 1920x1080 
KeyboardPowerCaseMouse
Microsoft Sidewinder X4 Corsair HX750 Modded Corsair Carbide 500R Logitech M500 
  hide details  
Reply
post #5 of 6
Thread Starter 
Thanks Anyway smile.gif
post #6 of 6
try adding this to your checkboxes. they need id's especially if all of the checkboxes have the same name. you're putting them into an array.
Code:
name="buylist[]"
instead of
Code:
name="buylist"
Ol' Betsy (Main)
(16 items)
 
 
CPUMotherboardGraphicsRAM
AMD FX-8150 Gigabyte GA-78LMT-USB3 ATI Radeon HD 3000 G.Skill Value Series 4GB (x4) 
Hard DriveCoolingOSMonitor
Western Digital Red 2TB (x4) Cooler Master GeminII S524 VMWare ESXi 5.1 Asus VS238H-P 
KeyboardPowerCaseMouse
Ducky Shine White LED Corsair CX500 Lian Li PC-A04B Logitech G9  
AudioOtherOther
Realtek ALC887 Adaptec RAID 6405 Intel Gigabit CT NIC 
  hide details  
Reply
Ol' Betsy (Main)
(16 items)
 
 
CPUMotherboardGraphicsRAM
AMD FX-8150 Gigabyte GA-78LMT-USB3 ATI Radeon HD 3000 G.Skill Value Series 4GB (x4) 
Hard DriveCoolingOSMonitor
Western Digital Red 2TB (x4) Cooler Master GeminII S524 VMWare ESXi 5.1 Asus VS238H-P 
KeyboardPowerCaseMouse
Ducky Shine White LED Corsair CX500 Lian Li PC-A04B Logitech G9  
AudioOtherOther
Realtek ALC887 Adaptec RAID 6405 Intel Gigabit CT NIC 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › PHP While Loop's and $_POST - Help