我正在使用表单将数据插入到mysql数据库中,在插入数据时,它给出了一个未定义索引的错误消息 有人可以帮忙吗 提前谢谢你
<?php
$servername = "localhost";
$username = "jad";
$password = "jad";
$dbname = "info";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Attempt insert query execution
if (isset($_POST['submit'])) {
$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$sql = "INSERT INTO names (first_name, last_name) VALUES ('$_POST[fname]', '$_POST[lname]')";
if(mysqli_query($conn, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
}
// Close connection
mysqli_close($conn);
?>
<html>
<body>
<form action = "" method = "POST">
first name : <input type = "text" name = "fname">
<br><br>
last name : <input type = "text" name = "lname">
<br><br>
<input type = "submit" name="submit" value="submit">
</form>
</body>
</html>
答案 0 :(得分:0)
这应该有效,但请注意你很容易受到sql注入,这非常糟糕。
$sql = "INSERT INTO names (first_name, last_name) VALUES ('$fname', '$lname')";
我建议您阅读以下内容并熟悉准备好的陈述: http://php.net/manual/en/mysqli.prepare.php