我想玩弄數據庫和動態地使用php插入數據。 目前我有一個'後'方法的形式,一切似乎符合邏輯,但它並沒有插入數據到表中。POST方法不插入數據到數據庫表
代碼附在下面,如果有人能指出我正確的方向,將不勝感激。
的index.php:
<form action="index.php" method="post">
<label for="name">Name</label>
<input type="text" name="name" required>
<label for="breed">Breed</label>
<input type="text" name="breed">
<label for="age">Age</label>
<input type="text" name="age">
<input type="submit" name="submit" value="Submit">
</form>
<?php
require "connect.php";
if('submit') {
$name = $_POST['name'];
$breed = $_POST['breed'];
$age = $_POST['age'];
$newdog = mysqli_query('INSERT INTO `dogs`(`name`, `breed`, `age`) VALUES ([$name],[$breed],[$age)');
if ($newdog) {
echo "$name has been added to the database";
} else {
echo "$name has not been added to database.";
};
};
?>
connect.php:
<?php
$connect = mysqli_connect('localhost', 'max', 'password', 'db_test');
?>
請使用*準備的語句*,您的代碼是開放的SQL注入。 – ccKep