-php, PDO, prepared statement, html, bootstrap, mysql database-
每当我点击提交按钮,我都会得到空白的php页面,即:profile.php.i看不到错误或成功插入记录。请有人帮我调试这段代码------------------------------------------ -----------------------------
<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中的这个数据库连接代码和profile.php中的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();
}
?>
答案 0 :(得分:0)
一个可能的问题是您在没有先检查是否已正确创建的情况下调用访问$ conn。这是一个糟糕的编程习惯,可能会导致问题。在假设它是一个可以使用的对象之前,你应该添加一个if子句来检查$ conn是什么。
你做的第二个类似的假设就是打电话 内爆( “”,$颜色)
$ color是通过$ _REQUEST接收的变量,但你认为它是一个数组。做出这样的假设,特别是来自表单提交的数据也是一个坏习惯。
我会开始调试此问题,方法是在使用之前添加一个检查来查看$ conn。