简体   繁体   English

在PHP中将表单数据插入MySQL

[英]Insert Form data to MySQL in Php

I have a form in Html like this: 我在HTML中有这样的表格:

 <!--HTML Form input--> <div class = "login-block"> <form id="form1" style="display: block" method="POST" action="Insert.php"> <!--Input link api--> <b>Link: </b><input type="text" id="link" name="apilink"><br> <br> <!--Chart Type--> <b>Chart Type:</b> <label class="custom-select"> <select id="chartType" name="chartType"> <option value="">Select</option> <option value="pie">Pie Chart</option> <option value="column">Column Chart</option> <option value="bar">Bar Chart</option> </select> </label> <br><br> <!--Button create chart--> <div class ="wrapper"> <button type="button" name="create" onClick="drawChart();">Create</button> <br><br> </div> </form> </div> 

I create a php file to insert what user type in form to MySQL Database, this is my Insert.php: 我创建了一个php文件,以将哪种用户类型插入到MySQL数据库中,这是我的Insert.php:

<!--Insert Form Data to MySQL-->
<?php 
    $con = mysql_connect("localhost","root","123456");
    if (!$con)
    {
        die('Could not connect: ' . mysqli_error());
    }

    mysql_select_db("activiti_report");

    if(isset($_POST['create'])) {
        $sql = "INSERT INTO chartinfo (link, typeChart) VALUES ('$apilink', '$chartType')";

        $result = mysql_query($sql);
    }

?>

But when I run my page, after type some text and choose drop down list, I press Create button and nothing happen. 但是,当我运行页面时,键入一些文本并选择下拉列表后,我按创建按钮,但没有任何反应。

UPDATE: 更新:

我的表结构

You're open to SQL injection. 您可以使用SQL注入。 I recommend you to use prepared statement. 我建议您使用准备好的语句。

<?php
if(isset($_POST['create'])){
    $chartType = $_POST['chartType'];
    $apilink = $_POST['apilink'];
    $conn = new mysqli("localhost", "root", "123456", "activiti_report");
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    $stmt = $conn->prepare("INSERT INTO chartinfo (link, typeChart) VALUES (?,?)");
    $stmt->bind_param("ss", $apilink, $chartType);
    $stmt->execute();
    $stmt->close();
    $conn->close();
}else{
    echo "Form not sended";
}

It looks like you are firstly not getting the data from the form. 看来您首先没有从表单中获取数据。

To get the actual data you would need to get them from post like this. 要获取实际数据,您需要像这样从帖子中获取它们。

$apilink = $_POST["apilink"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM