我有一个关于如何将文本字段中的数据添加到数据库中SQL表中的问题。我希望文本字段需要某种数据,以便表中没有 blank 数据。但是,我似乎无法使其正常工作。
下面的代码显示了我的问题。
创建用户的页面
<html>
<head>
<title>Eksamensprojekt</title>
<style>
.outer {
display: table;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.login-form {
margin-left: auto;
margin-right: auto;
width: 400px;
font-family: Tahoma, Geneva, sans-serif;
}
.login-form h1 {
text-align: center;
color: #4d4d4d;
font-size: 24px;
padding: 20px 0 20px 0;
}
.login-form input[type="password"],
.login-form input[type="text"] {
width: 100%;
padding: 15px;
border: 1px solid #dddddd;
margin-bottom: 15px;
box-sizing:border-box;
}
.login-form input[type="submit"] {
width: 100%;
padding: 15px;
background-color: #535b63;
border: 0;
box-sizing: border-box;
cursor: pointer;
font-weight: bold;
color: #ffffff;
margin: 0;
}
</style>
</head>
<body>
<div class="outer">
<div class="middle">
<div class="login-form">
<h1>Associhunt</h1>
<form method="get">
<input type="text" name="firstname" placeholder="First name"><br>
<span class="error">* <?php echo $nameErr;?></span>
<input type="text" name="email" placeholder="E-mail"><br>
<input type="text" name="tlfnr" placeholder="Phone"><br>
<input type="text" name="position" placeholder="Current job position"><br>
<input type="text" name="username" placeholder="Username"><br>
<input type="text" name="password" placeholder="Password"><br>
<input type="submit" value="Opret bruger" name="opretbruger">
</form>
</div>
<div class="login-form">
<form method="get" action="../login/">
<input type="submit" value="Already have an account?">
</form>
</div>
</div>
</div>
<?php
$conn = new mysqli($servername, $username, $password,$dbnavn);
if ($conn->connect_error) die("Fejl: " . $conn->connect_error);
if (isset($_GET['opretbruger']))
{
$firstname=$_GET['firstname'];
if ($firstname===NULL) die();
$lastname=$_GET['lastname'];
if ($lastname===NULL) die();
$email=$_GET['email'];
if ($email===NULL) die();
$tlfnr=$_GET['tlfnr'];
if ($tlfnr===NULL) die();
$position=$_GET['position'];
if ($position===NULL) die();
$username=$_GET['username'];
if ($username===NULL) die();
$password=$_GET['password'];
if ($password===NULL) die();
$sql="INSERT INTO UserData
(
firstname,
lastname,
email,
tlfnr,
position,
username,
password
)
VALUES ('".$firstname."','".$lastname."','".$email."','".$tlfnr."','".$position."','".$username."','".$password."')";
$conn->query($sql);
if ($conn->affected_rows >0 )
{
echo "Bruger oprettet!";
}
else
{
echo "Bruger ikke oprettet!";
};
};
?>
</body>
</html>
表创建:
<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to create table
$sql = "CREATE TABLE UserData (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL,
tlfnr INT(8) NOT NULL,
position VARCHAR(50),
username VARCHAR(40) NOT NULL,
password VARCHAR(40) NOT NULL,
reg_date TIMESTAMP
)";
if ($conn->query($sql) === TRUE) {
echo "Table UserData created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>
答案 0 :(得分:3)
您可以使用HTML输入必填属性-例如,更改:
<input type="text" name="email" placeholder="E-mail">
到
<input type="text" name="email" placeholder="E-mail" required>
答案 1 :(得分:0)
您查看过required属性吗?
在代码下方添加了属性:
<html>
<head>
<title>Eksamensprojekt</title>
<style>
.outer {
display: table;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.login-form {
margin-left: auto;
margin-right: auto;
width: 400px;
font-family: Tahoma, Geneva, sans-serif;
}
.login-form h1 {
text-align: center;
color: #4d4d4d;
font-size: 24px;
padding: 20px 0 20px 0;
}
.login-form input[type="password"],
.login-form input[type="text"] {
width: 100%;
padding: 15px;
border: 1px solid #dddddd;
margin-bottom: 15px;
box-sizing:border-box;
}
.login-form input[type="submit"] {
width: 100%;
padding: 15px;
background-color: #535b63;
border: 0;
box-sizing: border-box;
cursor: pointer;
font-weight: bold;
color: #ffffff;
margin: 0;
}
</style>
</head>
<body>
<div class="outer">
<div class="middle">
<div class="login-form">
<h1>Associhunt</h1>
<form method="get">
<input type="text" name="firstname" required placeholder="First name"><br>
<span class="error">* <?php echo $nameErr;?></span>
<input type="text" name="email" required placeholder="E-mail"><br>
<input type="text" name="tlfnr" required placeholder="Phone"><br>
<input type="text" name="position" required placeholder="Current job position"><br>
<input type="text" name="username" required placeholder="Username"><br>
<input type="text" name="password" required placeholder="Password"><br>
<input type="submit" value="Opret bruger" name="opretbruger">
</form>
</div>
<div class="login-form">
<form method="get" action="../login/">
<input type="submit" value="Already have an account?">
</form>
</div>
</div>
</div>
<?php
$conn = new mysqli($servername, $username, $password,$dbnavn);
if ($conn->connect_error) die("Fejl: " . $conn->connect_error);
if (isset($_GET['opretbruger']))
{
$firstname=$_GET['firstname'];
if ($firstname===NULL) die();
$lastname=$_GET['lastname'];
if ($lastname===NULL) die();
$email=$_GET['email'];
if ($email===NULL) die();
$tlfnr=$_GET['tlfnr'];
if ($tlfnr===NULL) die();
$position=$_GET['position'];
if ($position===NULL) die();
$username=$_GET['username'];
if ($username===NULL) die();
$password=$_GET['password'];
if ($password===NULL) die();
$sql="INSERT INTO UserData
(
firstname,
lastname,
email,
tlfnr,
position,
username,
password
)
VALUES ('".$firstname."','".$lastname."','".$email."','".$tlfnr."','".$position."','".$username."','".$password."')";
$conn->query($sql);
if ($conn->affected_rows >0 )
{
echo "Bruger oprettet!";
}
else
{
echo "Bruger ikke oprettet!";
};
};
?>
</body>
</html>