MsKazza Posted July 11, 2011 Share Posted July 11, 2011 Hi, I am doing a coupon app for my sister. First it's supposed to check for existance of coupon if its been used then it says sorry been used, if not been used it inserts it into database. after that it checks if its the winning coupon or not. Now the second bit works fine (i think). However the first part does check for the coupon number but won't insert into database, i know its my syntax but i have been looking so long i can see any more!! Obviously if the coupon has been used then the second part of the code doesn't need to run. If anyone can offer any suggestions or corrections would be much appreciated. thanks, MsKazza <?php //Get Values from Coupon.php Form $fname = $_POST['fname']; $sname = $_POST['sname']; $email = $_POST['email']; $phone = $_POST['phone']; $coupon = $_POST['coupon_code']; // Database connect $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("coupons", $con); // Check if unique code $query = mysql_query("SELECT * FROM coupons WHERE coupon_code = '". $coupon ."'"); if (mysql_num_rows($query) > 0) { echo 'This Coupon code has already been used. Please try again.'; end else { // Enter details into database $sql="INSERT INTO coupons (coupon_code, fname, sname, email, phone) VALUES ('$_POST[coupon_code]','$_POST[fname]','$_POST[sname]','$_POST[email]','$_POST[phone]')"; } //if (!mysql_query($sql,$con)) // { // die('Error: ' . mysql_error()); // } //echo "1 record added"; mysql_close($con) ?> <?php // Check if winning code or not if ( $coupon == D11223 ) { echo 'Thank you '. $fname . ' you entered coupon code number: ' . $coupon . '.<br />'; echo 'You are a Winner!! Please click <a href="here.php"> here</a> to claim your prize'; } else { echo 'Sorry '. $fname . ' you entered coupon code number: ' . $coupon . '.<br />'; echo "Sorry you have not won this time, please try again."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241709-code-in-wrong-order-i-think-lol/ Share on other sites More sharing options...
KevinM1 Posted July 11, 2011 Share Posted July 11, 2011 Try: $sql="INSERT INTO coupons (coupon_code, fname, sname, email, phone) VALUES ('{$_POST['coupon_code']}','{$_POST['fname']}','{$_POST['sname']}','{$_POST['email']}','{$_POST['phone']}')"; You need to wrap array variables in {} if you're trying to interpolate them in a string. Also, you need quotes around their keys (e.g., $_POST['phone']). Quote Link to comment https://forums.phpfreaks.com/topic/241709-code-in-wrong-order-i-think-lol/#findComment-1241405 Share on other sites More sharing options...
Kustom_Vegas Posted July 11, 2011 Share Posted July 11, 2011 if (mysql_num_rows($query) > 0) { echo 'This Coupon code has already been used. Please try again.'; } else {//...... Quote Link to comment https://forums.phpfreaks.com/topic/241709-code-in-wrong-order-i-think-lol/#findComment-1241410 Share on other sites More sharing options...
MsKazza Posted July 11, 2011 Author Share Posted July 11, 2011 thanks for that, i'm getting the error : Parse error: syntax error, unexpected T_ELSE in C:\xampp\htdocs\couponsite\process.php on line 41 which is the else { line Quote Link to comment https://forums.phpfreaks.com/topic/241709-code-in-wrong-order-i-think-lol/#findComment-1241420 Share on other sites More sharing options...
Psycho Posted July 11, 2011 Share Posted July 11, 2011 The problem is you have an opening curly bracket for the IF condition and no closing bracket before the else condition. But, there are other problems with the code. Here is a revision that fixes many issues <?php // Database connect $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } if(!mysql_select_db("coupons", $con)) { die('Could not select database: ' . mysql_error()); } //Parse Values from Coupon.php Form $fname = mysql_real_escape_string(trim($_POST['fname'])); $sname = mysql_real_escape_string(trim($_POST['sname'])); $email = mysql_real_escape_string(trim($_POST['email'])); $phone = mysql_real_escape_string(trim($_POST['phone'])); $coupon = mysql_real_escape_string(trim($_POST['coupon_code'])); // Check if unique code $query = "SELECT * FROM coupons WHERE coupon_code = '{$coupon}'" $result = mysql_query($query); if(!$result) { //Error running query $htmlOutput = "Error running query: {$query}<br><br>Error: " . mysql_error(); } elseif (mysql_num_rows($query) > 0) { //Duplicate coupon code $htmlOutput = "This Coupon code has already been used. Please try again."; } else { // Enter details into database $query = "INSERT INTO coupons (coupon_code, fname, sname, email, phone) VALUES ('{$coupon}','{$fname}','{$sname}','{$email}','{$phone}')"; $result = mysql_query($query); if(!$result) { //Error running query $htmlOutput = "Error running query: {$query}<br><br>Error: " . mysql_error(); } else { // Check if winning code or not if ( $coupon == D11223 ) { $htmlOutput = "Thank you {$fname} you entered coupon code number: {$coupon}.<br />\n"; $htmlOutput .= "You are a Winner!! Please click <a href=\"here.php\">here</a> to claim your prize\n"; } else { $htmlOutput = "Sorry {$fname} you entered coupon code number: {$coupon}.<br />\n";; $htmlOutput .= "Sorry you have not won this time, please try again.\n"; } } } ?> <html> <body> <?php echo $htmlOutput; ?> </body> </html>[/email] Quote Link to comment https://forums.phpfreaks.com/topic/241709-code-in-wrong-order-i-think-lol/#findComment-1241422 Share on other sites More sharing options...
Kustom_Vegas Posted July 11, 2011 Share Posted July 11, 2011 yeah, refer to the code I posted above, you simply forgot a closing curly bracket Quote Link to comment https://forums.phpfreaks.com/topic/241709-code-in-wrong-order-i-think-lol/#findComment-1241465 Share on other sites More sharing options...
Andy11548 Posted July 11, 2011 Share Posted July 11, 2011 @Kustom_Vegas, clearly you didn't read the code correct, as they did close the bracket: if (mysql_num_rows($query) > 0) { echo 'This Coupon code has already been used. Please try again.'; end else { // Enter details into database $sql="INSERT INTO coupons (coupon_code, fname, sname, email, phone) VALUES ('$_POST[coupon_code]','$_POST[fname]','$_POST[sname]','$_POST[email]','$_POST[phone]')"; } Have you tried the checking code like this: if (mysql_num_rows($query) > 0) { echo 'This Coupon code has already been used. Please try again.'; } else { // Enter details into database $sql="INSERT INTO coupons (coupon_code, fname, sname, email, phone) VALUES ('$_POST[coupon_code]','$_POST[fname]','$_POST[sname]','$_POST[email]','$_POST[phone]')"; } Quote Link to comment https://forums.phpfreaks.com/topic/241709-code-in-wrong-order-i-think-lol/#findComment-1241474 Share on other sites More sharing options...
MsKazza Posted July 11, 2011 Author Share Posted July 11, 2011 Thank you all so much for your responses. I made the changes that you suggested mjdamato When i run your revision i get the error : Warning: mysql_num_rows() expects parameter 1 to be resource, string given in C:\xampp\htdocs\couponsite\process.php on line 30 which is : elseif (mysql_num_rows($query) > 0) but it is entering info in database and checking winning coupon Quote Link to comment https://forums.phpfreaks.com/topic/241709-code-in-wrong-order-i-think-lol/#findComment-1241477 Share on other sites More sharing options...
Kustom_Vegas Posted July 11, 2011 Share Posted July 11, 2011 remove the complex syntax as it is not needed.. $query = "SELECT * FROM coupons WHERE coupon_code = '$coupon'" Quote Link to comment https://forums.phpfreaks.com/topic/241709-code-in-wrong-order-i-think-lol/#findComment-1241478 Share on other sites More sharing options...
MsKazza Posted July 11, 2011 Author Share Posted July 11, 2011 oh and i get this when a duplicate key instead of saying the coupon you have entered is already been used : Warning: mysql_num_rows() expects parameter 1 to be resource, string given in C:\xampp\htdocs\couponsite\process.php on line 30 Error running query: INSERT INTO coupons (coupon_code, fname, sname, email, phone) VALUES ('d11223','Jessica','Liddy','jess@gmail.com','') Error: Duplicate entry 'd11223' for key 'coupon_code' Kustom what do you mean remove the complex syntax? Quote Link to comment https://forums.phpfreaks.com/topic/241709-code-in-wrong-order-i-think-lol/#findComment-1241482 Share on other sites More sharing options...
Kustom_Vegas Posted July 11, 2011 Share Posted July 11, 2011 in your insert statement, you are telling it to insert 5 columns, but only specify 4 Quote Link to comment https://forums.phpfreaks.com/topic/241709-code-in-wrong-order-i-think-lol/#findComment-1241484 Share on other sites More sharing options...
MsKazza Posted July 11, 2011 Author Share Posted July 11, 2011 // Enter details into database $query = "INSERT INTO coupons (coupon_code, fname, sname, email, phone) VALUES ('{$coupon}','{$fname}','{$sname}','{$email}','{$phone}')"; i count five on each coupon, fname, sname, email and phone Quote Link to comment https://forums.phpfreaks.com/topic/241709-code-in-wrong-order-i-think-lol/#findComment-1241485 Share on other sites More sharing options...
Solution Andy11548 Posted July 11, 2011 Solution Share Posted July 11, 2011 Try this: <?php // Database connect $con = mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("coupons", $con) or die(mysql_error()); //Get Values from Coupon.php Form $fname = mysql_real_escape_string(trim($_POST['fname'])); $sname = mysql_real_escape_string(trim($_POST['sname'])); $email = mysql_real_escape_string(trim($_POST['email'])); $phone = mysql_real_escape_string(trim($_POST['phone'])); $coupon = mysql_real_escape_string(trim($_POST['coupon_code'])); // Check if unique code $query = mysql_query("SELECT * FROM coupons WHERE coupon_code = '". $coupon ."'"); if (mysql_num_rows($query) > 0) { echo 'This Coupon code has already been used. Please try again.'; exit; } else { // Enter details into database $sql= mysql_query("INSERT INTO coupons (coupon_code, fname, sname, email, phone) VALUES ('$coupon','$fname','$sname','$email','$phone')"); } mysql_close($con) ?> <?php // Check if winning code or not if ( $coupon == 'D11223' ) { echo 'Thank you '. $fname . ' you entered coupon code number: ' . $coupon . '.<br />'; echo 'You are a Winner!! Please click <a href="here.php"> here</a> to claim your prize'; } else { echo 'Sorry '. $fname . ' you entered coupon code number: ' . $coupon . '.<br />'; echo "Sorry you have not won this time, please try again."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241709-code-in-wrong-order-i-think-lol/#findComment-1241505 Share on other sites More sharing options...
MsKazza Posted July 11, 2011 Author Share Posted July 11, 2011 great thanks all working yey!!! Thanks all for your help Quote Link to comment https://forums.phpfreaks.com/topic/241709-code-in-wrong-order-i-think-lol/#findComment-1241513 Share on other sites More sharing options...
Andy11548 Posted July 11, 2011 Share Posted July 11, 2011 No problem . Good luck with your future coding, ~Andy. Quote Link to comment https://forums.phpfreaks.com/topic/241709-code-in-wrong-order-i-think-lol/#findComment-1241517 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.