POST方法不将数据插入数据库表

时间:2017-06-03 15:47:08

标签: php mysql database phpmyadmin mamp

我正在尝试使用数据库并使用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');

?>

3 个答案:

答案 0 :(得分:0)

更改if(isset($_POST['submit'])){//check if it is set }

$newdog = mysqli_query('INSERT INTO

同时更改此行:

( {狗{1}} {名称{1}} {繁殖{1}}岁,

, {狗{1}} {名称{1}} {繁殖{1}}岁) VALUES ([$name],[$breed],[$age)');

您的代码非常容易受到SQL注入攻击

使用准备好的陈述,

$newdog = mysqli_query($connect, 'INSERT INTO

答案 1 :(得分:0)

[Table("A")]
public class A
{
    [Key]       
    public int A_Id { get; set; }       

    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Display(Name = "Last Name")]
    public string LastName { get; set; }
}

[Table("A_History")]
public class A_History
{
    [Key]   
    public int A_History_Id { get; set; }       

    public int A_Id { get; set; }       

    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Display(Name = "Last Name")]
    public string LastName { get; set; }
}

答案 2 :(得分:0)

自己回答:想出来,我不得不配置PHPStorm使用MAMP Apache服务器而不是内部服务器,因为那个显然不喜欢$ _POST []请求