我尝试了以下代码,但它不起作用。
代码
<?php
if (!empty($_POST)) {
$mysqli = new mysqli('localhost', 'root', '123', 'users');
if ($mysqli->connect_error) {
die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
}
$sql = "INSERT INTO users ( username, email ) VALUES ( '{$mysqli->real_escape_string($_POST['username'])}', '{$mysqli->real_escape_string($_POST['email'])}' )";
$insert = $mysqli->query($sql);
if ($insert) {
echo "Success! Row ID: {$mysqli->insert_id}";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
$mysqli->close();
}
?>
<form action="main.php" method="post">
<input type="text" id="SIMOMO" name="username" minlegth="5" maxlength="30" class="pad" required>
<input type="text" ID="FLOBIT"name="email" class="pad" required >
<input type="submit">
</form>
答案 0 :(得分:-1)
试试这个:
<?php
if ($_POST['sub']) {
$server = "localhost";
$username = "yourusernamehere";
$pass = "yourpasswordhere";
$db = "users";
// Create connection
$mysqli = new mysqli($server, $username, $pass, $db);
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
echo "Connected successfully";
$uname = mysql_escape_string($_POST['username'])
$mail = mysql_escape_string($_POST['email']);
$sql = "INSERT INTO users (username, email) VALUES ('$uname', '$mail')";
$insert = $mysqli->query($sql);
if ($insert){
echo "Success! Row ID: {$mysqli->insert_id}";
header("Location:main.php");
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
$mysqli->close();
}
?>
<!DOCTYPE html>
<html>
<body>
<form action = "" method = "POST">
<label>Enter your Username:</label>
<input type="text" name="username" required>
<label>Enter your Email:</label>
<input type="email" name="email" required >
<input type="hidden" name = "check">
<input type="submit" name = "sub" value = "INSERT">
</form>
</body>
</html>
在第三行查看的数据库名称为
'users'
等等 是您尝试INSERT INTO
进入的表格的名称"users'
第10行。你确定你没有弄乱吗?