【问题标题】:Inserting data from form into database PHP MYSQL将表单中的数据插入数据库PHP MYSQL
【发布时间】:2015-12-23 12:21:00
【问题描述】:

我正在尝试将表单中的数据发送到我的数据库中,但我似乎没有工作。表格字段是正确的。 s我有人能看出发生了什么问题吗?

表格:

<form class="myForm" role="form" action= "idea.php" method ="POST">
    <h1 style="margin-top:50px; margin-bottom:30px; text-align:center; color:#0f4155;">Idea Form</h1>

    <p1>Name:</p1>
    <input type="text" class="form-control" name="name" placeholder="Name">

    <p1>Originator:</p1>
    <input type="text" class="form-control" name="originator" placeholder="Originator">

    <p1>Alternative Contact</p1>
     <input type="text" class="form-control" name="altcontact" placeholder="Alternative Contact">

    <p1>Problem to Solve</p1>
     <input type="text" class="form-control" name="problem" placeholder="Problem to Solve">

    <p1>Description</p1>
    <textarea class="form-control" rows="3" name="description" placeholder="Description"></textarea>

    <p1>PO</p1>
    <input type="text" class="form-control" name="po" placeholder="PO">

    <p1>Archetypical Client</p1>
    <input type="text" class="form-control" name="archclient" placeholder="Arcetypical Client">

    <p1>Urgency</p1>
    <input type="text" class="form-control" name="urgency" placeholder="Urgency" style="margin-bottom: 20px">

    <p1>Technology/Platform</p1>
    <input type="text" class="form-control" name="technology" placeholder="Technology/Platform">

    <p1>Number of Sprints</p1>
    <input type="number" class="form-control" name="sprints" placeholder="Number of Sprints">

    <p1>Progress</p1>
    <input type="text" class="form-control" name="progress" placeholder="Progress">

    <input class="submit" name="submit" type="submit" value="Submit">

</form>  

PHP:

if ($_SERVER["REQUEST_METHOD"] == "POST") { //if new idea is being added

    $id = '';
    $name = $_POST['name'];
    $originator = $_POST['originator'];
    $altcontact = $_POST['altcontact'];
    $problem = $_POST['problem']; 
    $description = $_POST['description']; 
    $po = $_POST['po'];
    $archclient = $_POST['archclient']; 
    $urgency = $_POST['urgency']; 
    $technology = $_POST['technology']; 
    $sprints = $_POST['sprints']; 
    $progress = $_POST['progress']; 
    $status = "submitted"; 


    $strsq0 = "INSERT INTO idea (`id`,`name`, `originator`, `alternative_contact`, `problem`, `description`, `po`, `arch_client`, `urgency`, `technology`, `sprints`, `progress`, `status`) VALUES ('" . $name . "," . $name . "," . $originator . "," . $altcontact . "," . $problem . ", " . $description . ", " . $po . "," . $archclient . ", " . $urgency . ", " . $technology . ", " . $sprints . ", " . $progress . ", " . $status . "');"; //query to insert new idea
    if ($mysqli->query($strsq0)) {
        echo "Insert success!";
    } else {
        echo "Cannot insert into the data table; check whether the table is created, or the database is active. "  . mysqli_error();
    }
}

【问题讨论】:

  • 一方面,您没有在 SQL 查询的 VALUES 部分引用您的值,因此至少会失败。阅读准备好的陈述。 (您只需将值作为一个巨大的单列字符串)。
  • 插入两次VALUES ('" . $name . "," . $name name
  • 从插入到idea中删除id
  • 请在您的应用程序中添加一些细节。在启动查询 sql 之前,您已连接到数据库?
  • 请转义用户提供的值。或者绑定你的参数。否则你很容易被 SQL 注入。

标签: php mysql database forms


【解决方案1】:

如果idauto increment,则将其从values() 中添加NULL 的查询中删除,引号也未正确管理

$strsq0 = "INSERT INTO idea (`name`, `originator`, `alternative_contact`, `problem`, `description`, `po`, `arch_client`, `urgency`, `technology`, `sprints`, `progress`, `status`) VALUES ('$name','$originator','$altcontact ','$problem', '$description', '$po','$archclient', '$urgency', '$technology', '$sprints','$progress', '$status')"; //query to insert new idea

更多调试

echo "INSERT INTO idea (`name`, `originator`, `alternative_contact`, `problem`, `description`, `po`, `arch_client`, `urgency`, `technology`, `sprints`, `progress`, `status`) VALUES ('$name','$originator','$altcontact ','$problem', '$description', '$po','$archclient', '$urgency', '$technology', '$sprints','$progress', '$status')";
exit;
$strsq0 = "INSERT INTO idea (`name`, `originator`, `alternative_contact`, `problem`, `description`, `po`, `arch_client`, `urgency`, `technology`, `sprints`, `progress`, `status`) VALUES ('$name','$originator','$altcontact ','$problem', '$description', '$po','$archclient', '$urgency', '$technology', '$sprints','$progress', '$status')"; //query to insert new idea

然后复制这个打印的查询并在phpMyadmin运行,运行打印查询后检查是否有错误。

【讨论】:

  • 没错,虽然只是问题的一部分:)
  • 我也认为你在值部分检查中包含了 2 次名称
  • 我不确定引号,因为不确定 DB 中字段的数据类型是什么
  • 好多了。现在如果只有 op 会使用准备好的语句。
  • 不,不是。您只发送一个列值。查询应该是INSERT INTO test (name, originator) VALUES ('killer', 'instinct'); 注意引号。
猜你喜欢
  • 2013-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-04
  • 1970-01-01
  • 2017-08-08
  • 2019-01-12
相关资源
最近更新 更多