如何在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!

dongqian0763
dongqian0763 感谢您的建议。我会记得当我切换到生产时。
11 个月之前 回复
donglian1384
donglian1384 作为一个侧面问题,您很容易受到SQL注入攻击。你应该看看什么是SQL注入?
11 个月之前 回复
dtjkl42086
dtjkl42086 好吧,这可能是原因。但实现这一目标的解决方法是什么?
11 个月之前 回复
douyi8315
douyi8315 执行sql查询时应检查错误。
11 个月之前 回复
douxiegan6468
douxiegan6468 谢谢和羞辱我,但不幸的是它没有解决我的问题
11 个月之前 回复
dongpa6867
dongpa6867 toolong
11 个月之前 回复

2个回答

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>

First of all make sure that your html form is sending the data to your php file by adding this line to your php file just after the php tag

    echo '<pre>';
    print_r($_POST);

And you will see that you are receiving three variables i.e

name, lastname and email

now check your php code you're requesting the name as firstname.

duanhao9176
duanhao9176 通常我使用命令行来仅在PHP内的查询中期望SQL语法错误。 当我在浏览器中运行它时,除了“此页面不起作用”之外,我没有收到任何错误消息
11 个月之前 回复
drui0508
drui0508 所以你得到一个空数组,你也必须在浏览器中运行它现在终端,终端不发送任何数据到您的PHP代码。
11 个月之前 回复
douchen7366
douchen7366 你的意思是'<pre>'作为占位符吗? 无论如何,我把它贴在第二行,它仍然无法正常工作。 当我在命令行中运行它时,它输出<pre> Array()
11 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问