将数据从表单插入数据库PHP MYSQL

时间:2015-12-23 12:21:01

标签: php mysql database forms

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

形式:

<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();
    }
}

1 个答案:

答案 0 :(得分:3)

如果idauto increment,则将其从NULL中添加values()的查询中删除,同时引用也未妥善管理

$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中运行,检查运行打印查询后是否有任何错误。