当一个html表单字段被提交为空时,所有被请求列的默认值都为空,如何更新现有的mysql表?

ujv3wf0j  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(387)

登录后,用户需要填写另一张表格。这个网站只有一个表叫做 users 列命名为-

Ten_School_name, Ten_Board, Ten_Percentage, Twl_School_Name, Twl_Board, Twl_Percentage
``` `Ten_School_name` ,  `Twl_School_Name` 设置为 `VARCHAR(70)` 默认值为
NULL `Ten_Board` ,  `Twl_Board` 设置为 `VARCHAR(10)` 默认值为
NULL `Ten_Percentage` ,  `Twl_Percentage` 设置为 `int(3)` 默认值为 `NULL` 这个 `Username` ,  `Email` 以及 `Password` 是另外三个列,当用户在网站上注册时,这些列被占用,而rest被设置为null作为默认值。当我通过填写所有六个字段提交表单时,表单成功提交,但如果任何字段留空,则会显示处理脚本时的错误 `Incorrect integer value: '' for column 'Twl_Percentage' at row 2` 我已经浪费了很多时间寻找解决办法。我希望这里有人能解决我的问题。
这是我的表格-
<label>10th Board</label>
<select name="tenth-board">
    <option name="tenth-board1" value="icse">ICSE</option>
    <option name="tenth-board2" value="cbse">CBSE</option>
    <option name="tenth-board3" value="up-board">UP Board</option>
</select>
<br />
<label>10th Percentage</label>
<input type="number" name="tenth-percent">
<br />

<!--12th Class Details-->
<label>12th School Name</label><input type="text" name="twl-school"><br />

<label>12th Board</label>
<select name="twl-board">
    <option name="twl-board1" value="icse">ISE</option>
    <option name="twl-board2" value="cbse">CBSE</option>
    <option name="twl-board3" value="up-board">UP Board</option>
</select>
<br />
<label>12th Percentage</label>
<input type="number" name="twl-percent">

<input type="submit" name="submita" id="" class="" value="Save" /></form>
下面是我处理数据的php脚本-

if (isset($_POST['submita'])) {

$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "jobin";

$con = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

if (isset($_SESSION['username'])) {
$user = $_SESSION['username'];
echo $user;
$eml = $_SESSION['cand_email'];
echo $eml;
}

$tenthSchool = mysqli_real_escape_string($con, $_POST['tenth-school']);
$tenthBoard = mysqli_real_escape_string($con, $_POST['tenth-board']);
$tenthPercent = mysqli_real_escape_string($con, $_POST['tenth-percent']);
$twlSchool = mysqli_real_escape_string($con, $_POST['twl-school']);
$twlBoard = mysqli_real_escape_string($con, $_POST['twl-board']);
$twlPercent = mysqli_real_escape_string($con, $_POST['twl-percent']);

$sqlsd = "UPDATE users
SET
Ten_School_Name = '$tenthSchool',
Ten_Board = '$tenthBoard',
Ten_Percentage = '$tenthPercent',
Twl_School_Name = '$twlSchool',
Twl_Board = '$twlBoard',
Twl_Percentage = '$twlPercent'
WHERE
Email = '$eml';";

if(!$con)
{
die('Could not connect: ' . mysqli_connect_error());
}
$resultsd = mysqli_query($con, $sqlsd);

if(!$resultsd)
{
die('Could not update data: ' . mysqli_connect_error());
}
if ($resultsd) {
header("Location: ../candidate-registration.php?sd=success");
}

} else {
echo "nothing";
}

nbnkbykc

nbnkbykc1#

我会将列从null更改为notnull,并将默认值设置为表上的空字符串“”。这样,您就可以使用更安全的预处理语句,如下所示:

$stmt = $link->prepare("UPDATE users SET Ten_School_Name = ?, Ten_Board = ?, Ten_Percentage = ?, Twl_School_Name = ?, Twl_Board = ?, Twl_Percentage = ? WHERE Email = ?");
$stmt->bind_param("ssissis", $tenthSchool, $tenthBoard, $tenthPercent, 
$twlSchool, $twlBoard, $twlPercent, $eml);
$stmt->execute();
$stmt->close();

如果无法更改列,则只能使用非空字段编写查询。这样地:

$sqlsd2 = "";
if ($tenthSchool != "") { $sqlsd2 .= "Ten_School_Name = '$tenthSchool', ";
if ($tenthBoard != "") { $sqlsd2 .= "Ten_Board = '$tenthBoard', ";
if ($tenthPercent != "") { $sqlsd2 .= "Ten_Percentage = $Ten_Percentage, ";
if ($twlSchool != "") { $sqlsd2 .= "Twl_School_Name = '$twlSchool', "
if ($twlBoard != "") { $sqlsd2 .= "Twl_Board = '$twlBoard', "
if ($twlPercent != "") { $sqlsd2 .= "Twl_Percentage = $twlPercent ";

if($sqlsd2 !="")
{ //update)
 $sqlsd = "UPDATE users SET " . $sqlsd2 . "WHERE Email = '$eml'";
}
else
{
//nothing to update
}

注意,第二种方法不如第一种方法安全,因为mysqli\u real\u escape\u string不能完全避免mysql注入。你必须使用事先准备好的陈述。
有一种使用call\u user\u func\u array()生成动态准备语句的方法,有时我无法避免这样做,但它更复杂。我建议你简单地改变你的专栏,做准备好的陈述。

相关问题