使用PHP PDO

时间:2015-07-20 07:03:26

标签: php html mysql pdo

我有一个html表单,我希望3个字段是必填字段。如果用户没有填写其中任何一个字段,则表单不应该提交,并且应该告诉用户填写强制性表单。我使用过PDO而且我不知道该怎么做。如果有人可以帮助我。下面我已经给出了我的html和php文件。

HTML:

<html>
<head>
<title>Data Insertion</title>
</head>
<body>
<p><span class="Error">* required field.</span></p>
<form method="post" action="su.php"> 
<h2>Please Fill In Details</h2>
<label for="name">Name </label>
<input type="text" Placeholder="Enter your name" name="name" id="name" />
<span class="Error">*</span>
<br />
<br />
<label for="age">Age </label>
<input type="text" name="age" id="age" placeholder="Enter your age"  />
<br />
<br />
<label for="mailid">MailId </label>
 <input type="text" name="mailid" id="mailid" placeholder="Enter your Mail   Id" />
<span class="Error">*</span>
<br />
<br />
<label for="gender">Gender </label>
<br />
<label for="male">Male </label>
<input type="radio" name="gender" id="gender" value="Male" id="male" />
<label for="female">Female </label>
<input type="radio" name="gender" id="gender" value="Female" id="female" />
<br />
<br />
<label for="qualification">Qualification </label>
<select name="qualification" value="Qualification" id="qualification">
<option value="B.E">SSLC</option>
<option value="P.G">HSC</option>
<option value="SSLC">UG</option>
<option value="HSC">PG</option>
</select>
<br /><br />
<label for="hobbies">Hobbies </label>
<br />
<input type="checkbox" name="hobbies" id="hobbies" value="Cricket" />Cricket
<input type="checkbox" name="hobbies" id="hobbies" value="Music" />Music
<input type="checkbox" name="hobbies" id="hobbies" value="Swimming" />Swimming
<br /><br />
<label for="textarea">Address </label>
<br />
<textarea name="address" id="textarea" rows="15" cols="30"></textarea>
<span class="Error">*</span>
<br /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

PHP:

<?php
    $servername = 'localhost';
    $username = 'root';
    $password = '';
    try {
        $conn = new PDO("mysql:host=$servername;dbname=testing", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        if(isset($_POST['submit'])){
            $name = $_POST['name'];
            $age = $_POST['age'];
            $mailid = $_POST['mailid'];
            $gender = $_POST['qualification'];
            $hobbies = $_POST['address'];
            if($name !='' || $mailid !='' || $address !=''){
        $sql = "Insert into user (Name, Age, MailId, Gender, Qualification, Hobbies, Address) 
        values ('".$_POST["name"]."', '".$_POST["age"]."', '".$_POST["mailid"]."', '".$_POST["gender"]."', '".$_POST["qualification"]."', '".$_POST["hobbies"]."', '".$_POST["address"]."')";
        $conn->exec($sql);
        echo "Thank you for registering";
            } else {
                echo "<p>Insertion failed <br/> Please enter the required fields !";
            }}
    }
catch(PODException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}
?>

2 个答案:

答案 0 :(得分:0)

尝试在所有必需的输入元素上添加html 5属性“required”。例如

<input type="text" Placeholder="Enter your name" name="name" id="name" required />

你也应该检查php代码中的POST变量,因为这并不能真正阻止某人滥用你的服务。实施例

if(!isset($_POST['somevar'])) {
    // Do insert
}

答案 1 :(得分:0)

在html中您应该使用javascript(例如jQuery)来控制onsubmit事件并验证必填字段是否填充了正确的值,请查看以下链接:jQuery.submit()

在php中你应该在创建和执行查询之前检查并验证每个var。关于它的简单文章在这里:Sanitize and Validate Data with PHP Filters 这对我一开始就有好处;)

查询字符串应包含占位符,然后应准备执行语句,请检查此链接: PDOStatement::bindParam