php表单不提交,只使用pdo准备语句获取空白页

ep6jt1vc  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(332)
-php, PDO, prepared statement, html, bootstrap, mysql database-

每当我点击提交按钮,我就会看到一个空白的php页面,即:profile.php。我没有看到任何错误或成功插入的记录。请有人能帮我调试这个代码

hpcdzsge

hpcdzsge1#

-。

<form action="profile.php" method="GET"> <!--enctype="multipart/form-data"-->
    <h1 >Profile </h1>
     <div class="form-row">        
        <div class="form-group col-md-4">        
            <label>Name :</label>
            <input type="text" class="form-control" required="required" name="fname" />
        </div>

        <div class="form-group col-md-4">        
            <label>Age :</label>
            <input type="number" class="form-control" name="age" />
        </div>

        <div class="form-group col-md-4">
            <label>Gender :</label>            
            <select  class="form-control" name="gender">
                <option value="">Select</option>
                <option value="male">Male</option>
                <option value="female">Female</option>
            </select>
        </div>

        <div class="form-group col-md-6">
            <label>Email :</label>
            <input type="email" class="form-control" name="email" />
        </div>

        <div class="form-group col-md-6"> 
            <label>Phone No :</label>            
            <input type="tel" class="form-control" required="" pattern="[0-9]{10}" name="phone" />
        </div>

        <div class="form-group col-md-6"> 
            <label>Address :</label>           
            <textarea type="text" class="form-control" name="address"> </textarea>
        </div>

        <div class="form-group col-md-6">
            <label>Comments :</label>
            <textarea type="text" class="form-control" name="comments"> </textarea>
        </div>      

        <div class="form-group col-md-6">
            <label>Favorite Color :</label> 
            <br>            
            <input type="checkbox"  value="red" name="color[]" />Red
            <input type="checkbox" value="green" name="color[]" />Green
            <input type="checkbox" value="blue" name="color[]" />Blue
            <input type="checkbox" value="yellow" name="color[]" />Yellow
        </div>

    <!--    <div class="form-group col-md-6"> 
            <label>Photo :</label>            
            <input type="file"  name="photo" />
        </div>-->

        <div class="form-group col-md-6">
            <input type="submit"   name="submit"/>

        </div>
    </div>
</form>

这个数据库连接代码在dbconn.php中,而php代码在profile.php中

<?php 
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "pritesh";

        try {
            $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
            // set the PDO error mode to exception
            //$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
            //echo "Connected successfully"; 
            }
        catch(PDOException $e)
            {
            echo "Connection failed: " . $e->getMessage();
            }
 ?>

<?php    
header('Access-Control-Allow-Origin: *');
error_reporting(E_ALL);
ini_set('display_errors', 1);

if(isset($_POST["submit"])){

include("dbconn.php");

$fname=$_REQUEST["fname"];
$age=$_REQUEST["age"];
$email=$_REQUEST["email"];          
$phone=$_REQUEST["phone"];
$address=$_REQUEST["address"];
$gender=$_REQUEST["gender"];
$color=$_REQUEST["color"];
$comments=$_REQUEST["comments"];

$sql = "insert into profile (name, age, email, phone, address, gender, color, comments) values (?, ?, ?, ?, ?, ?, ?, ?)";

$stmt = $conn->prepare($sql);   
$stmt->bind_param($fname, $age ,$email, $phone, $address, $gender, implode(",",$color), $comments);

$stmt->execute();
$stmt->close();

}

?>
7bsow1i6

7bsow1i62#

一个可能的问题是,您调用accessing$conn时没有首先检查它是否已正确创建。这是一个糟糕的编程实践,可能会导致问题。在假设$conn是可以使用的对象之前,应该添加if子句来检查它是什么。
第二个类似的假设是调用内爆(“,”,$color)
$color是通过$\请求接收的变量,但您假定它是一个数组。做出这样的假设,尤其是从表单提交的数据中,也是一个坏习惯。
我将在使用$conn之前添加一个检查来调试这个问题。

相关问题