【发布时间】:2018-08-12 21:41:35
【问题描述】:
我无法弄清楚我的代码有什么问题。数据未存储在 phpmyadmin 中。可能是语法错误。如果有人能找到我的代码的问题,我会很高兴。请帮我解决这个问题!
这是一个简单的表格,询问需要血液的人的详细信息。我需要将详细信息存储到名为“blood_share_system”的数据库和表名“acceptors”中。
我的 html 文件:
<form action="needblood.inc.php"method="post">
<fieldset>
<legend>Personal Information</legend>
<label>FirstName</label><br>
<input type="text"name="first"required><br>
<label>LastName</label><br>
<input type="text"name="last"required><br>
<label>PatientFirstName</label><br>
<input type="text"name="pfirst" required><br>
<lable>PatientLastName</label><br>
<input type="text"name="plast"<br>
</legend>
</fieldset>
<fieldset>
<legend>Contact Information</legend>
<lable>Phone Number</label><br>
<input type="tel"placeholder="10-digit PhoneNumber"name="phno" required><br>
<lable>Email Address</label><br>
<input type="email" placeholder="Valid email address"name="email"><br>
</fieldset>
<fieldset>
<legend>Blood Group Information</legend>
<label>Blood Group</label><br>
<select name="bgroup">
<option value="1">O-positive</option>
<option value="2">O-negative</option>
<option value="3">A-positive</option>
<option value="4">A-negative</option>
<option value="5">B-positive</option>
<option value="6">B-negative</option>
<option value="7">AB-positive</option>
<option value="8">AB-negative</option>
</select><br>
<label>Quantity required(1Unit:350ml):</label><br>
<button class="nbtn"id="snbtn"onclick="nbuttonClick()">-</button>
<input type="number" readonly="true" id="inc" value="0"name="quantity"required>
<button class="pbtn"id="sbtn"onclick="buttonClick()">+</button>Unit</h4><br>
<input type="number" id="result">ml
</fieldset>
<fieldset>
<legend>Location Details</legend>
<label>Locality</lable><br>
<input type="text"name="loc"required><br>
<label>Pincode</label><br>
<input type="number"name="pincode"required>
</fieldset>
<input type="submit" name="submit"value="Submit">
</form>
我的 php 文件:
<?php
//first if
if(isset($_POST['submit'])){
$dbserverName = "localhost";
$dbuserName = "root";
$dbpassword = "";
$dbname = "blood_share_system";
$conn = mysqli_connect( $dbserverName , $dbuserName , $dbpassword , $dbname);
//2nd if
if(!$conn){
echo "connection was not established with server";
}//2nd if close
//3rd if
if(!mysqli_select_db($conn,'blood_share_system')){
echo "not connected to the database";
}//3rd if close
$first=mysqli_real_escape_string($conn,$_POST['first']);
$last=mysqli_real_escape_string($conn,$_POST['last']);
$pfirst=mysqli_real_escape_string($conn,$_POST['pfirst']);
$plast=mysqli_real_escape_string($conn,$_POST['plast']);
$phno=mysqli_real_escape_string($conn,$_POST['phno']);
$email=mysqli_real_escape_string($conn,$_POST['email']);
$bgroup=mysqli_real_escape_string($conn,$_POST['bgroup']);
$quantity=mysqli_real_escape_string($conn,$_POST['quantity']);
$locality=mysqli_real_escape_string($conn,$_POST['loc']);
$pincode=mysqli_real_escape_string($conn,$_POST['pincode']);
if (!preg_match("/^[a-zA-Z]*$/" , $first) || !preg_match("/^[a-zA-Z]*$/" , $last)||!preg_match("/^[a-zA-Z]*$/" , $pfirst)||!preg_match("/^[a-zA-Z]*$/" , $plast) ){
trigger_error('Enter valid names!',E_USER_ERROR);
} else {
if(!preg_match("/^[0-9]{10}$/", $phno)){
trigger_error('Enter 10 digit phone number!');
} else{
if (!filter_var( $email , FILTER_VALIDATE_EMAIL )){
trigger_error('Enter valid email address!',E_USER_ERROR);
} else{
if(!preg_match("/^[0-9]{6}$/", $pincode)){
trigger_error('Enter valid pincode!',E_USER_ERROR);
} else {
$sql="INSERT INTO acceptors('$first','$last','$pfirst','$plast','$phno','$email','$bgroup','$quantity','$locality','$pincode');";
if(!mysqli_query($conn, $sql)){
echo "Not inserted";
} else {
echo"Inserted";
}
}
}
}
}
}
【问题讨论】:
-
任何错误信息,如无效的 csrf 令牌?
-
trigger_error.. 检查你的错误日志,它说了什么?
-
初始化 $sql 后 echo 它以字符串形式获取 SQL 查询。拥有它后,您可以在 phpmyadmin 中执行查询以获取错误。
-
查看 SQL 查询,我可以想到您可能放错了列值或没有为所有列分配值。使用“insert into values”语句总是安全的,而不是简单的“insert into”语句。
-
尝试删除所有 if 并直接跳转到插入而不检查任何 preg_match,看看是否有效,如果有效,则逐个添加每个 if 和验证。此外,避免嵌套 if/else 语句......这使得阅读和维护代码变得非常困难。我建议在每一个之后使用它们,并在任何东西被破坏的情况下返回错误。