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
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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.