Jump to content

Code in wrong order i think!! lol


MsKazza
Go to solution Solved by Andy11548,

Recommended Posts

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.";
}

?>

Link to comment
Share on other sites

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']).

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

@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]')";
}

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

    // 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

Link to comment
Share on other sites

  • Solution

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.";
}

?>

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.