douji0588 2013-07-28 20:46
浏览 32

麻烦我的PHP验证脚本

So I have created a PHP validation script. On test I filled and submitted the forms but so far $error returns undefined index and no data is set into the database. Can anyone take a look and give a second opinion on why its not functioning as intended? To my eye it all looks OK.

Otherwise my script runs OK (Insert into DB) it's just something about my validation script breaks it.

 <?php  

    if (isset($_POST['Submit'])) {  

        if ($_POST['name'] != "") {  
            $_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);  
            if ($_POST['name'] == "") {  
                $errors .= 'Please enter a valid name.<br/><br/>';  
            }  
        } else {  
            $errors .= 'Please enter a name.<br/>';  
        }  

           if (isset($_POST['Submit'])) {  

        if ($_POST['address'] != "") {  
            $_POST['address'] = filter_var($_POST['address'], FILTER_SANITIZE_STRING);  
            if ($_POST['address'] == "") {  
                $errors .= 'Please enter a valid address<br/><br/>';  
            }  
        } else {  
            $errors .= 'Please enter a address.<br/>';  
        }  

           if (isset($_POST['postcode'])) {  

        if ($_POST['postcode'] != "") {  
            $_POST['postcode'] = filter_var($_POST['postcode'], FILTER_SANITIZE_STRING);  
            if ($_POST['postcode'] == "") {  
                $errors .= 'Please enter a valid name.<br/><br/>';  
            }  
        } else {  
            $errors .= 'Please enter a name.<br/>';  
        }  

        if (!$errors) {  



$name = $_POST['name'];
$address = $_POST['address'];
$postcode = $_POST['postcode'];
$photo = $_POST['photo'];
$db1 = new dbmember();
$db1->openDB();
$numofrows = $db1->insert_member('', $name, $address, $postcode, $photo);
echo "Success. Number of rows affected:
<strong>{$numofrows}<strong>";



$sql="SELECT * from member";
$result=$db1->getResult($sql);
echo "<table class='table table-hover'>";
echo "<tr><th>Member ID</th><th>Name</th><th>Address</th><th>Postcode</th><th>Photo</th></tr>";

  while($row = mysqli_fetch_assoc($result))
{
 echo "<tr>";
 echo "<td>{$row['mid']}</td><td>{$row['name']}</td>";
 echo "<td>{$row['address']}";
echo "<td>{$row['postcode']}";
 echo"<td><img height='80' width='120' src='{$row['photo'] }' /></td>";  
 echo "</tr>";
 }
 echo "</table>";


$db1->closeDB();
}


        }
        }
        }
            echo "Records updated!<br/><br/>";  
        } else {  
            echo '<div style="color: red">' . $errors . '<br/></div>';  
        }  

?>  

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name="myform" class = "well" id="myform" onsubmit="return validateForm(  );">

Please fill in the fields to add a new member
<p></p>
<input type="text" class="span3" placeholder="Enter member name"name="name" id="name" /><br />
<input type="text" class="span3"placeholder="Enter an address"name="address" id="address" /><br />
<input type="text" class="span3"placeholder="Enter a postcode"name="postcode" id="postcode" /><br />
<input type="text"class="span3" placeholder="Enter a picture (optional)"name="photo" /><br />
<p>
<button class="btn btn-primary" type="submit" value="Save" >Submit  </button>
</p>

</form>
  • 写回答

1条回答 默认 最新

  • dongxuxian1123 2013-07-28 20:52
    关注

    Your button doesn't have a name="Submit" attribute. Your php code can't find the $_POST['Submit'] because it doesn't exist.

    Consequently, the if (isset($_POST['Submit'])) { condition will return false meaning the validation is never performed and the $error variable never set to a value.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?