【问题标题】:Simple PHP help (not inserting into array)简单的 PHP 帮助(不插入数组)
【发布时间】:2012-07-12 19:44:38
【问题描述】:

我找了又找,没有找到任何东西,虽然这可能是因为我什至不知道是什么导致了这个错误,更不用说如何修复它了。

首先,我是个新手。我知道 PHP 的基础知识,但有很多我不知道,所以如果这个问题的答案很简单(或者如果你看不懂我的代码,因为它太乱了!),请原谅。

我想作为我的第一个应用程序之一,我会制作一个简单的电子邮件脚本,用户可以在其中输入他们的姓名、主题、消息和电子邮件地址。

这是表单页面的相关位:http://pastebin.com/UhQukUuB(抱歉,不太清楚如何嵌入代码...)。

<form action="send.php" method="post">
    Name: <input type="text" name="name" size="20" /><br />
    Subject: <input type="text" name="subject" size="20" /><br />
    Message:<br /><textarea name="message" rows="12" cols="55"></textarea><br />
    Your email address: <input type="text" name="emailAddress" size="20" /><br />
    <input type="submit" value="Send" />
</form>

这在 send.php 中:http://pastebin.com/nky0L1dT。

<?php
    $name=$_POST['name'];
    $subject=$_POST['subject'];
    $message=$_POST['message'];
    $emailAddress=$_POST['emailAddress'];
    //It's receiving the variables correctly, I've checked by printing the variables.
    $errors=array(); //Creates empty array with 0 indexes. This will now be filled with error messages (if there are any errors).
    if($name=="" || $subject=="" || $message=="" || $emailAddress=""){
        if($name==""){
            $errors[0]="You did not supply a name.";
        }
        if($subject==""){
            $errors[count($errors)]="You did not supply a subject."; //I'm using count($errors) so it will create a new index at the end of the array, regardless of how many indexes it currently has (if that makes sense, it's hard to explain)
        }
        if($message==""){
            $errors[count($errors)]="You did not supply a message.";
        }
        if($emailAddress==""){
            $errors[count($errors)]="You did not supply an email address.";
        }
    }
    //Were there any errors?
    if(!count($errors)==0){
        print "The following errors were found:<br />";
        for($i=0; $i<count($errors); $i++){
            print $errors[$i]."<br />";
        }
        die ();
    }
    //Rest of email script, which I'll write when the stupid bug is fixed. :(
?>

会发生以下情况:当您错过名称、主题或消息时,错误检测器会正常工作并显示“您没有提供名称/主题/消息”。当您错过电子邮件地址时,什么也不会发生。我知道它被存储在数组中并且被正确接收,因为如果您错过了名称/主题/消息和电子邮件地址,它会显示“您没有提供名称/主题/消息。您没有提供电子邮件地址”。我盯着屏幕看了半个小时,只是想弄清楚它为什么会这样?

谢谢。

【问题讨论】:

    标签: php arrays forms insert


    【解决方案1】:

    这里有两个问题,其中一个是你的否定:

    if(!count($errors)==0){
    

    一元 ! 适用于 count($errors),而不是 count($errors)==0。请改用!=:

    if(count($errors) != 0) {
    

    第二个错误是这里使用了赋值 (=) 而不是比较 (==):

    if($name=="" || $subject=="" || $message=="" || $emailAddress=""){
    

    附带说明,您不需要使用$errors[count($errors)] 将项目添加到数组的末尾。 $errors[] 可以。对于迭代,使用foreach 循环也比您当前所做的要好得多。

    【讨论】:

      【解决方案2】:

      改变

      if($name=="" || $subject=="" || $message=="" || $emailAddress=""){
      

      到

      if($name=="" || $subject=="" || $message=="" || $emailAddress==""){
      

      您无意中在if 语句中将$emailAddress 设置为""

      ... || $emailAddress=""){
      

      【讨论】:

        【解决方案3】:

        在你的 if 语句中,你使用的是赋值,而不是比较

        if($name=="" || $subject=="" || $message=="" || $emailAddress=""){
        

        代替

        if($name=="" || $subject=="" || $message=="" || $emailAddress==""){
        

        【讨论】:

          猜你喜欢
          • 2010-12-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多