Jump to content

Adding Session value from SQL


petenaylor

Recommended Posts

Hi all

 

I am trying to write a piece of code that takes the value from a form and checks it via an SQL table then adds the session value and the value from the SQL table into the rest of the code as a string. It is for a voucher code in a shopping basket:

 

if(isset($_POST['voucher_code'])) {

 

$vouchercode = $_POST['voucher_code'];

 

$sqlvoucher_code = mysql_query(" SELECT * FROM `voucher_codes` WHERE name = ".$vouchercode."");

$getvoucher_code = mysql_fetch_array($sqlvoucher_code);

 

$_SESSION['voucher_code'] = $_POST['voucher_code'];

 

$voucher_code = $getvoucher_code['value'];

 

header("Location: basket.php");

 

exit;

 

}

 

Every time I add a value into the form that I know is in the DB it returns a value of 0 even though the value in the DB is 15.95.

 

Please help!

 

Thanks

Pete

Link to comment
https://forums.phpfreaks.com/topic/219562-adding-session-value-from-sql/
Share on other sites

There's no specific reason to use mysql_fetch_row(), and in this case I'd suggest mysql_fetch_assoc(), since the code that follows is already written to work with an associative array. The code you have up there should work, but It looks like the query string is malformed. Also, you should explicitly specify the fields you want to retrieve in the query rather than use the * wildcard.

$sqlvoucher_code = mysql_query(" SELECT `value` FROM `voucher_codes` WHERE `name` = '$vouchercode' LIMIT 1");
$getvoucher_code = mysql_fetch_assoc($sqlvoucher_code);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.