gpt4 book ai didi

php - 当我尝试添加选项值时 SQL 空值

转载 作者:行者123 更新时间:2023-11-29 10:11:17 26 4
gpt4 key购买 nike

大家好,这是我的 php 表单。整个表格连接到MySQL。一切正常。但是当我用输入“zameranie”填充选项值时,它在数据库中显示为0值。所有其他值,如姓名、密码、电子邮件、性别工作,但教师和方向则不然。

Fakulta = 教师Zameranie = 方向

抱歉没有翻译成代码。但我认为没有问题。

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Form</title>
</head>
<body>
<form action="insert.php" method="POST">
<table>
<tr>
<td>Meno :</td>
<td><input type="text" name="username" required></td>
</tr>
<tr>
<td>Heslo :</td>
<td><input type="password" name="password" required></td>
</tr>
<tr>
<td>Pohlavie :</td>
<td>
<input type="radio" name="gender" value="m" required>Muž
<input type="radio" name="gender" value="f" required>Žena
</td>
</tr>
<tr>
<td>Email :</td>
<td><input type="email" name="email" ></td>
</tr>
<tr>
<td>Fakulta + zameranie</td>
<td>
<select name="fakulta" required>
<option selected hidden value="">Fakulta</option>
<option value="fri">FRI</option>
<option value="fhv">FHV</option>
<option value="sjf">SjF</option>
<option value="pedas">PEDAS</option>
<option value="svf">SvF</option>
<option value="ef">EF</option>
</select>
<input type="text" name="zameranie" required>
</td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>

<?php
$username = $_POST['username'];
$password = $_POST['password'];
$gender = $_POST['gender'];
$email = $_POST['email'];
$fakulta = $_POST['fakulta'];
$zameranie = $_POST['zameranie'];
if (!empty($username) || !empty($password) || !empty($gender) || !empty($email) ||
!empty($fakulta) || !empty($zameranie)) {
$host = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbname = "youtube";
//create connection
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
if (mysqli_connect_error()) {
die('Connect Error('. mysqli_connect_errno().')'. mysqli_connect_error());
} else {
$SELECT = "SELECT email From register Where email = ? Limit 1";
$INSERT = "INSERT Into register (username, password, gender, email, fakulta, zameranie) values(?, ?, ?, ?, ?, ?)";
//Prepare statement
$stmt = $conn->prepare($SELECT);
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->bind_result($email);
$stmt->store_result();
$rnum = $stmt->num_rows;
if ($rnum==0) {
$stmt->close();
$stmt = $conn->prepare($INSERT);
$stmt->bind_param("ssssii", $username, $password, $gender, $email, $fakulta, $zameranie);
$stmt->execute();
echo "Úspešne pridané do databázy";
} else {
echo "Niekto používa tento mail";
}
$stmt->close();
$conn->close();
}
} else {
echo "Všetky položky sú povinné";
die();
}
?>

Result in database when I fill form

Here is database

最佳答案

查找代码片段:

//...<br>
$stmt = $conn->prepare($INSERT);<br>
$stmt->bind_param("ssssii", $username, $password, $gender, $email, $fakulta, $zameranie);<br>
//...<br><br>

$stmt->bind_param("ssssii", ...) 中,您指示 PHP 绑定(bind) $fakulta$zameranie code> 作为整数而不是字符串。使用 $stmt->bind_param("ssssss", ...) 代替。这应该可以解决您的问题。

您可以在此处阅读有关绑定(bind)函数的更多信息:http://php.net/manual/en/mysqli-stmt.bind-param.php

关于php - 当我尝试添加选项值时 SQL 空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50925498/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com