drcmg28484 2019-08-07 09:54
浏览 111
已采纳

如何在PHP中使用HTML表单中的变量?

I just want to receive the input from a HTML form in PHP. I did research for almost 2 hours trying every code I got, but it doesn't work. Here's my code:

HTML:

<!DOCTYPE html>
<html>
<head>
<title>Form site</title>
</head>
<body>
    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
        <tr>
           <td>
              <form name="form1" method="post" action="connect.php">
              <table width="100%" border="0" cellspacing="1" cellpadding="3">
                 <tr>
                    <td colspan="3">Insert Data Into mySQL Database </td>
                 </tr>
                 <tr>
                    <td width="71">Name</td>
                    <td width="6">:</td>
                    <td width="301"><input name="name" type="text" id="name"></td>
                 </tr>
                 <tr>
                    <td>Lastname</td>
                    <td>:</td>
                    <td><input name="lastname" type="text" id="lastname"></td>
                 </tr>
                 <tr>
                    <td>Email</td>
                    <td>:</td>
                    <td><input name="email" type="text" id="email"></td>
                 </tr>
                 <tr>
                    <td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
                 </tr>
              </table>
              </form>
           </td>
        </tr>
     </table></body>
</html>

PHP:

<?php
$mysqli = new mysqli("localhost", "root", "", "test");

if ($mysqli->connect_errno) {
    printf("Connect failed: %s
", $mysqli->connect_error);
    exit();
}
$tbl_name="employees"; // Table name

// Get values from form
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];


// if successfully insert data into database, displays message "Successful".
$sql = "INSERT INTO $tbl_name (name, lastname, email) VALUES ('$name', '$lastname', '$email')";
$mysqli->query($sql);
?>

So, when I run the HTML file in Chrome everything's normal, but if I then fill the form an click the Submit button Chrome says 'This page doesn't work'. The connection to the database works definitely. I read that it may help to set the AllowOverride option in the Apache config file (using XAMPP) to 'ALL'. It didn't. The filename specified in the HTML code matches the filename of the PHP file I'm actually using for this.

PHP is a server-side language, isn't it? So I suspect that I have to configure something to run the PHP file. PHP is already installed on my computer I checked the database connection using PHP in the command-line. Obviously I'm missing something. Does anyone have an idea?

Thanks in advance!

  • 写回答

2条回答 默认 最新

  • 普通网友 2019-08-07 14:26
    关注

    Solution: I used Apache as the webserver, but the HTML and PHP files were in another location, so PHP hadn't any webserver to run in.

    Here's the working code: PHP:

    ?php
    $mysqli = new mysqli("localhost", "root", "", "test");
    
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s
    ", $mysqli->connect_error);
        exit();
    }
    $tbl_name="employees"; // Table name
    
    // Get values from form
    $name = $_POST['name'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];
    
    // if successfully insert data into database, displays message "Successful".
    $sql = "INSERT INTO $tbl_name (name, lastname, email) VALUES (?, ?, ?)";
    $statement = $mysqli->prepare($sql);
    $statement->bind_param('sss', $name, $lastname, $email);
    
    // $statement->execute();
    
    if(!$statement->execute()) {
        echo "Query fehlgeschlagen: ".$statement->error;
    }
    else echo "All done!";
    

    HTML:

    <!DOCTYPE html>
    <html>
    <head>
    <title>Form site</title>
    </head>
    <body>
        <table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
           <tr>
           <td>
              <form name="form1" method="post" action="connect.php">
              <table width="100%" border="0" cellspacing="1" cellpadding="3">
                 <tr>
                    <td colspan="3">Insert Data Into mySQL Database </td>
                 </tr>
                 <tr>
                    <td width="71">Name</td>
                    <td width="6">:</td>
                    <td width="301"><input name="name" type="text" id="name"></td>
                 </tr>
                 <tr>
                    <td>Lastname</td>
                    <td>:</td>
                    <td><input name="lastname" type="text" id="lastname"></td>
                 </tr>
                 <tr>
                    <td>Email</td>
                    <td>:</td>
                    <td><input name="email" type="text" id="email"></td>
                 </tr>
                 <tr>
                    <td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
                 </tr>
               </table>
               </form>
            </td>
            </tr>
         </table></body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛