php 连接PHP源代码并提交表单到MySQL数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24244203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:14:12  来源:igfitidea点击:

Connecting PHP source code and submit form to MySQL Database

phphtmlmysqlforms

提问by user3717206

I'm trying to learn PHP and I'm trying to connect a MySQL database with my PHP code to make a submit form that lets me input data into the database. My problem is that the source code is connecting but the HTML isn't posting the variables to the PHP file. I could really use some help.

我正在尝试学习 PHP,并且我正在尝试将 MySQL 数据库与我的 PHP 代码连接起来,以制作一个提交表单,让我将数据输入到数据库中。我的问题是源代码正在连接,但 HTML 没有将变量发布到 PHP 文件。我真的可以使用一些帮助。

This is my HTML source code

这是我的 HTML 源代码

<html>

<head>
<title>Form Input Data</title>
</head>

<body>

<table border="1">
  <tr>
    <td align="center">Form Input Employees Data</td>
  </tr>
  <tr>
  <td>
  <table>
    <form action="input.php" method="POST">
    <tr>
      <td>Name</td>
      <td><input type="text" name="name" size="20">
      </td>
    </tr>
    <tr>
      <td>Address</td>
      <td><input type="text" name="address" size="40">
      </td>
    </tr>
    <tr>
      <td></td>
      <td align="right"><input type="submit" 
      name="submit" value="Sent"></td>
    </tr>
    </table>
  </td>
</tr>
</table>

And this is my PHP source code

这是我的 PHP 源代码

<?php

$user_name = "fees0_14446440";
$password = "********";
$database = "fees0_14446440_addressbook";
$server = "sql107.0fees.net";

mysql_connect("$server","$user_name","$password");

mysql_select_db("$database");


$order = "INSERT INTO Trial

        (name, address)

        VALUES

        ('$name',

        '$address')";


$result = mysql_query($order);

if($result){

    echo("<br>Input data is succeed");

} else{

    echo("<br>Input data is fail");

}
?>

采纳答案by aplewandowski

You can find the POST data in PHP's $_POSTvariable. It should hold all the values that were passed via the POST method.

您可以在 PHP 的$_POST变量中找到 POST 数据。它应该保存所有通过 POST 方法传递的值。

$name = $_POST["name"];

$name = $_POST["name"];

回答by Bibek Jana

put POST values into variable

将 POST 值放入变量中

$name=$_POST['name'];
$address=$_POST['address'];

before sql query and write sql query as

在 sql 查询之前并将 sql 查询写为

$order = "INSERT INTO Trial

$order = "插入试用版

    (name, address)

    VALUES

    ('".$name."',

    '".$address."')";

回答by JustStarting

Try the following:

请尝试以下操作:

<?php

$user_name = "fees0_14446440";
$password = "********";
$database = "fees0_14446440_addressbook";
$server = "sql107.0fees.net";

mysql_connect("$server","$user_name","$password");

mysql_select_db("$database");

if (isset($_POST['submit'])) {
$name = $_POST['name'];
$address = $_POST['address'];

$order = mysql_query("INSERT INTO Trial (name, address) VALUES ('$name', '$address')");

if ($order) {
    echo '<br>Input data is successful';
} else {
    echo '<br>Input data is not valid';
}
}
?>

回答by Khushboo

Try this

尝试这个

<html>

<head>
<title>Form Input Data</title>
</head>

<body>
<form action="input.php" method="POST">
<table border="1">
  <tr>
    <td align="center">Form Input Employees Data</td>
  </tr>
  <tr>
  <td>
  <table>

    <tr>
      <td>Name</td>
      <td><input type="text" name="name" size="20">
      </td>
    </tr>
    <tr>
      <td>Address</td>
      <td><input type="text" name="address" size="40">
      </td>
    </tr>
    <tr>
      <td></td>
      <td align="right"><input type="submit" 
      name="submit" value="Sent"></td>
    </tr>
    </table>
  </td>
</tr>
</table>
</form>

In PHP code

在 PHP 代码中

$order = "INSERT INTO Trial

        (name, address)

        VALUES

        ('$_POST["name"]',

        '$_POST["address"]')";