duancaozen6066 2018-12-03 15:32
浏览 72

设置简单表单提交到mySQL数据库[重复]

I'm trying to set up a very simple form to submit data to a MySQL database (I've never actually set up a MySQL database or the connections required to interact with it via a web form myself before).

I have a simple HTML form in my web page, to allow the user to enter a name, email address, and a question that they want to ask:

<!DOCTYPE html>
<html>
    <head>
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
        <meta content="utf-8" http-equiv="encoding">
    Page title
    </head>
    <body>
        <h1>Customer Information</h1>

        <form id="customerInfoForm" accept-charset = "utf-8" action = "insert.php" method = "post" >
            <table id = "customerInfoTable">
                <tr><td>Name: </td><td><input type = "text" name = "customerName" ></input></td></tr>
                <tr><td>Email: </td><td><input type = "text" name = "customerEmail"></input></td></tr>
                <tr><td>Enquiry: </td><td><input type = "text" name = "enquiryText"></input></td></tr>
            </table>
            <br>
            <input type = "submit" value = "Sumit"></input>
        </form>
    </body>
</html>

<?php
$con = mysql_connect('localhost','[retracted]','[retracted]');

if(!$con) {
    echo 'Not connected to server!';
}

$Name = $_POST['customerName'];
$Email = $_POST['customerEmail'];
$Query = $_POST['enquiryText'];

$sql = "INSERT INTO Enquiries(Name, Email, Query) VALUES ('$Name', '$Email', '$Query')";
if(!mysql_query($con, $sql)) {
    echo 'Data not inserted';
} else {
    echo 'Data inserted';
}

?>

In the insert.php file that's called when clicking the 'Submit' button on the form, I have the following:

<?php
    //Check if data has been entered
    if( isset( $_POST['data'] ) && !empty( $_POST['data'] ) )
    {
        $data = $_POST['data'];
    } else {
        header( 'location: form.html' );
        exit();
    }

    //set up mysql
    $sql_server = 'localhost';
    $sql_user = 'user';
    $sql_pwd = 'password';
    $sql_db = 'database';

    //Connect to sql database
    $mysqli = new mysqli( $sql_server, $sql_user, $sql_pwd, $sql_db) or die( $mysqli->error );

    //Insert details into table
    $insert = $mysqli->query( "INSERT INTO Enquiries ('data') VALUE ( '$data' )" );

    //Close mysqli connection
    $mysqli->close;
?>

However, currently, when I click the 'Submit' button on my form, I get an error displayed in my browser:

error ); //Insert details into table $insert = $mysqli->query( "INSERT INTO Enquiries ('data') VALUE ( '$data' )" ); //Close mysqli connection $mysqli->close; ?>

i.e. the above error is displayed as the content of the webpage, so it seems I'm doing something wrong with the database connection, but I'm not sure what... Can anyone point me in the right direction?

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能
    • ¥15 jmeter脚本回放有的是对的有的是错的
    • ¥15 r语言蛋白组学相关问题