-1

所以我创建了一个 PHP 验证脚本。在测试中,我填写并提交了表格,但到目前为止 $error 返回未定义的索引并且没有数据设置到数据库中。任何人都可以看一下并就为什么它没有按预期运行给出第二个意见吗?在我看来,这一切看起来都不错。

否则我的脚本运行正常(插入数据库)这只是我的验证脚本破坏了它。

 <?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>
4

1 回答 1

1

您的按钮没有name="Submit"属性。您的 php 代码找不到,$_POST['Submit']因为它不存在。

因此,if (isset($_POST['Submit'])) {条件将返回 false,这意味着永远不会执行验证,并且 $error 变量永远不会设置为值。

于 2013-07-28T20:52:56.110 回答