dousha2020 2011-04-22 07:56
浏览 76
已采纳

当用户输入错误代码时,不会弹出消息

I made a form to allow an user to submit and update their advertisement when they enter the right coupon code but I can't get the message "Invalid coupon code." to echo out when they enter the wrong coupon code.

heres my code:

<? require("connect.php"); ?>

<form action="advertisement.php" method="POST" enctype="multipart/form-data">
<table width="50%" border="0" cellspacing="0" cellpadding="8">
    <tr> 
       <td><div align="right"> Apartment:</div></td> 
       <td><select name="apartment" id="apartment"> 
        <option>Place Ad In Which Apartment...</option> 
        <option value="0">Archstone La Jolla</option>
        <option value="1">Archstone La Jolla Colony</option>
        <option value="2">Archstone UTC</option>
        <option value="3">Canyon Park</option>
        <option value="4">Costa Verde Village</option>
        <option value="5">Costa Verde Towers</option>
        <option value="6">La Jolla Crossroads</option>
        <option value="7">La Jolla Del Sol</option>
        <option value="8">La Jolla International Garden</option>
        <option value="9">La Jolla Palms</option>
        <option value="10">La Mirada</option>
        <option value="11">La Regencia</option>
        <option value="12">La Scala</option>
        <option value="13">Las Flores</option>
        <option value="14">The Villas</option>
        <option value="15">Nobel Court</option>
        <option value="16">Pacific Gardens</option>
        <option value="17">Regents Court</option>
        <option value="18">Regents La Jolla</option>
        <option value="19">Trieste</option>
        <option value="20">Valentia</option>
        <option value="21">Whispering Pines</option></select></td>
   </tr>
    <tr>
       <td><div align="right">Description:</div></td>
       <td><textarea name="description" id="description" cols="55" rows="7" wrap="VIRTUAL"></textarea></td>
    </tr>
    <tr>
       <td><div align="right">Coupon Code:</div></td>
       <td><input type="text" name="code" size="60"></td>
    </tr>
    <tr> 
       <td></td>
       <td><div align="left">
        <input type="reset" value="Clear">     
        <input type="submit" name="submit" value="Submit"></div></td>
    </tr>
  </table>
</form>

<?php

$description = $_POST['description'];
$apartment = $_POST['apartment'];
$code = $_POST['code'];

//retrieve data from password table
$query = mysql_query ("SELECT * FROM coupon WHERE code = '$code' ");

//get number of rows in table
$numrows = mysql_num_rows ($query);

if ($numrows !=0)
{
    // gather all codes
    while ($row = mysql_fetch_assoc ($query))
    {
        //retrieve code from database to match with the code that was put into field
        $dbcode = $row['code'];
    }

    //check to see if they match
    if ($code == $dbcode )
        {
            //check to see if coupon code is in both consumer and coupon tables
            $query2 = mysql_query ("SELECT * FROM consumer WHERE code = '$code' ");

            $numrows2 = mysql_num_rows ($query2);


        while ($row2 = mysql_fetch_assoc ($query2))
        {
            $consumercode = $row2['code'];
        }
            if( $dbcode == $consumercode)
            {

                $update = mysql_query ("UPDATE consumer SET description = '$description' WHERE code ='$code'  ");           




                echo "Advertisement successfully updated.";


            }
            //if coupon not in both tables then they haven't posted advertisement yet
            else
             {


$time = time();

$day =  30;

$exp = $time + ($day * 86400);


        mysql_query ("INSERT INTO consumer VALUES ('','$description', '$exp', '$apartment', '$code')");     

        mysql_query ("UPDATE coupon SET exp = '$exp' WHERE code ='$code'  ");       


                echo "Your advertisement has been successfully submitted.";
             }

}
else
    echo "Invalid coupon code.";
}
else 
    echo "";



 ?>
  • 写回答

1条回答 默认 最新

  • dpbe81245 2011-04-22 08:05
    关注
    while ($row = mysql_fetch_assoc ($query))
    {
        //retrieve code from database to match with the code that was put into field
        $dbcode = $row['code'];
    }
    

    This block of code isn't gathering all codes. It's just overwriting $dbcode with every code, and at the end of the block there is only the last row.

    I am assuming that the rest of the code should be inside of this block.

    }
    else
        echo "Invalid coupon code.";
    }
    else 
        echo "";
    

    This second echo statement at the end should also have a message, which I think would be the invalid code message, since it's based on if ($numrows !=0)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大