petenaylor Posted November 23, 2010 Share Posted November 23, 2010 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 Quote Link to comment Share on other sites More sharing options...
revraz Posted November 23, 2010 Share Posted November 23, 2010 You are fetching an array, but treating it as a single value. You are also not checking to see how many rows are being returned. Quote Link to comment Share on other sites More sharing options...
petenaylor Posted November 23, 2010 Author Share Posted November 23, 2010 OK, so how is that best solved? Quote Link to comment Share on other sites More sharing options...
revraz Posted November 23, 2010 Share Posted November 23, 2010 Depends on what you expect the results to be.. should it be 1 row or many? Quote Link to comment Share on other sites More sharing options...
petenaylor Posted November 23, 2010 Author Share Posted November 23, 2010 Hi mate Just one row. I need to add the value from the 'value' cell in the table based on the 'name' posted from the form. Cheers Quote Link to comment Share on other sites More sharing options...
revraz Posted November 23, 2010 Share Posted November 23, 2010 Check to make sure the number of rows = 1, then use mysql_fetch_row. http://php.net/manual/en/function.mysql-fetch-row.php Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 23, 2010 Share Posted November 23, 2010 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); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.