我有一个名为personal_detail
的表。表单用于使用用户创建的条目填充此表。查询未成功执行,我无法在查询中找到错误的确切位置。按下提交按钮后我收到的消息未创建。这是由我在php文件的末尾提供的。如果stmt - > execute()返回false。
这是personal_detail
表结构:
这是表格代码:
<form action="personal.php" method="post">
Name: <input name="name" type="text" size="20" maxlength="40" ><br>
CNIC : <input name="cnic" type="text" size="20" maxlength="15"><br>
Date: <input name="booking-date" type="date" size="20" ><br>
Ocassion: <select name="ocassion" size="1">
<option value="">Barat</option>
<option value="">Walima</option>
<option value="">option3</option>
<option value="">option4</option>
<option value="">option5</option>
<option value="">option6</option>
</select><br>
Address:<input name ="address" type="text" size="20" maxlength="50"><br>
Phone-No:<input name="phone-no" type="text" size="20" maxlength="11"><br>
Bride-Mobile:<input name="bride-mobile" size="20" maxlength="11"><br>
Groom-Mobile:<input name="groom-mobile" size="20" maxlength="11"><br>
Family-Mobile:<input name="family-mobile" size="20" maxlength="11"><br>
E-mail:<input name="email" type="text" size="20" maxlength="30"><br>
Who may i thank for refering you?:<input name="refering" type="text" size="20" maxlength="40"><br>
Do you provide consent to share images on our official web page:<br>
<input type="radio" name="share" value="Yes">Yes <br>
<input type="radio" name="share" value="No">No<br>
If yes:<br>
With identity: <br><input type="radio" name="permission" value="Yes">Yes<br>
<input type="radio" name="permission" value="No">No<br>
<input class="btn btn-primary btn-large" type="submit" value="Submit" name="Submit-Personal">
<input class="btn btn-primary btn-large" type="reset" value="Reset">
这是php文件:
$server="localhost";
$user="root";
$password="";
$database="camouflage_studio";
$con = mysqli_connect($server,$user,$password,$database);
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// prepare and bind
$stmt = $con->prepare("insert into personal_detail (Name, CNIC, Date, Ocassion, Address, Phone_No, Bride_Mobile, Groom_Mobile, Family_Mobile,EMail,Referring,Share,Permission) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssssiiissss", $name, $cnic, $date, $ocassion, $daddress, $phoneno, $bridemobile, $groommobile, $familymobile, $email, $refering, $share, $permission);
$name = $_POST['name'];
$cnic = $_POST['cnic'];
$date = $_POST['booking-date'];
$ocassion = $_POST['ocassion'];
$address = $_POST['address'];
$phoneno = $_POST['phone-no'];
$bridemobile = $_POST['bride-mobile'];
$groommobile = $_POST['groom-mobile'];
$familymobile = $_POST['family-mobile'];
$email = $_POST['email'];
$refering = $_POST['refering'];
$share = $_POST['share'];
$permission = $_POST['permission'];
$stmt->execute();
$result = mysqli_stmt_execute($stmt);
if($result)
echo "New records created successfully";
else
echo "not created";
// close connection
$stmt->close();
$con->close();
答案 0 :(得分:0)
这是一个代码块,可以执行完整的错误检查。
Date
进行反对。$_POST
到bind_param()
的值的不必要的变量声明。execute()
一直隐藏在以bind_param()
开头的条件结束时。原因/结果是,如果bind_param()
返回false
,则甚至不会调用execute()
,并且会显示错误消息。if(!$con=new mysqli($server,$user,$password,$database)){
echo "Connection Error: ",$con->connect_error; // do not echo when live
}elseif($stmt=$con->prepare("INSERT INTO `personal_detail` (`Name`,`CNIC`,`Date`,`Ocassion`,`Address`,`Phone_No`,`Bride_Mobile`,`Groom_Mobile`,`Family_Mobile`,`EMail`,`Referring`,`Share`,`Permission`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")){
if($stmt->bind_param('ssssssiiissss',$_POST['name'],$_POST['cnic'],$_POST['booking-date'],$_POST['ocassion'],$_POST['address'],$_POST['phone-no'],$_POST['bride-mobile'],$_POST['groom-mobile'],$_POST['family-mobile'],$_POST['email'],$_POST['refering'],$_POST['share'],$_POST['permission']) && $stmt->execute()){
echo "New record created successfully"; // New ID# is ",$stmt->insert_id;
}else{
echo "New record not created";
echo "Statement Error: ",$stmt->error; // do not echo when public
}
$stmt->close();
}else{
echo "Prepare Error: ",$con->error; // do not echo when public
}
$con->close();