无法通过PHP将数据添加到我的MySQL数据库[重复]

时间:2022-09-22 16:03:40

This question already has an answer here:

这个问题在这里已有答案:

I have this PHP script to insert data in my MySQL Database:

我有这个PHP脚本在我的MySQL数据库中插入数据:

<?php


$servername = "..."; // Host name
$username   = "..."; // Mysql username
$password   = "..."; // Mysql password
$dbname     = "..."; // Database name


$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}


$barcode   = $_POST['barcode'];
$name      = $_POST['name'];
$kategorie = $_POST['kategorie'];
$preis     = $_POST['preis'];
$b1        = addslashes($_POST['b1']);
$b1_1      = addslashes($_POST['b1_1']);



$sql = "INSERT INTO produkte (barcode,name,kategorie,preis,b1,b1_1) VALUES ('$barcode', '$name', '$kategorie', '$preis', '$b1', '$b1_1')";

if ($conn->multi_query($sql) === TRUE) {
    echo "New records created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}




$conn->close();

?>

The form :

表格 :

<html>
    <head>
        <meta charset="utf-8">
        <title>Produkt hinzuf&uuml;gen</title>
    </head>
    <body>
        <form action="eintragen.php" action="POST"/> 
            Barcode: <input type="text" name="barcode"/><br/> 
            Name: <input type="text" name="name"/><br/> 
            Kategorie: <input type="text" name="kategorie"/><br/> 
            Preis:<input type="text" name="preis"/><br/> 
            Beschreibungstext 1: <input type="text" name="b1" /><br/>
            Beschreibungstext 1.1: <input type="text" name="b1_1"/><br/> 
            <input type="submit" value="Absenden"/>
        </form>
    </body>
</html>

When I insert all the data in the html file and submit it, the PHP Script tells me that new records were created successfully.

当我在html文件中插入所有数据并提交它时,PHP脚本告诉我成功创建了新记录。

But it only creates a new row with no data inside...

但它只创建一个没有数据的新行......

Would be nice if you could help me...

如果你能帮助我会很好......

Cheers, Till

干杯,直到

4 个解决方案

#1


4  

Add method in the place of action

在行动地点添加方法

<form action="eintragen.php" method="POST"/> 

Try this it will help

试试这个会有所帮助

#2


3  

Set your Form like this,

像这样设置你的表格,

<form action="eintragen.php" method="POST"/> 

And if you are using one query then you can use it like,

如果您使用一个查询,那么您可以使用它,如,

mysqli_query($conn,$sql);

#3


0  

You don't check if $_POST contains the data you need before assigning them to variables.

在将它们分配给变量之前,不要检查$ _POST是否包含所需的数据。

If you did, you would have noticed that your $_POST is empty because your wrote action="POST" instead of method="POST". Correct that and it should be just fine.

如果你这样做了,你会注意到你的$ _POST是空的,因为你的写操作=“POST”而不是method =“POST”。纠正这一点,应该没问题。

#4


-1  

Hello till36,

你好到36岁,

Definition and Usage of POST/GET

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute).

method属性指定如何发送表单数据(表单数据发送到action属性中指定的页面)。

The form-data can be sent as URL variables (with method="get") or as HTTP post transaction (with method="post").

表单数据可以作为URL变量(使用method =“get”)发送,也可以作为HTTP post transaction发送(使用method =“post”)。

Notes on GET:

关于GET的说明:

Appends form-data into the URL in name/value pairs The length of a URL is limited (about 3000 characters) Never use GET to send sensitive data! (will be visible in the URL) Useful for form submissions where a user want to bookmark the result GET is better for non-secure data, like query strings in Google

将表单数据附加到名称/值对的URL中URL的长度是有限的(大约3000个字符)绝不使用GET发送敏感数据! (将在URL中显示)对于表单提交非常有用,其中用户想要为结果添加书签GET更适合非安全数据,例如Google中的查询字符串

Notes on POST:

关于POST的说明:

Appends form-data inside the body of the HTTP request (data is not shown is in URL) Has no size limitations Form submissions with POST cannot be bookmarked

将表单数据附加到HTTP请求的正文中(数据未显示在URL中)没有大小限制使用POST的表单提交无法加入书签

Suggestion

When you at a time execute one query so don't need write "mysqli_multi_query()" but use "mysqli_query()".

当你一次执行一个查询所以不需要写“mysqli_multi_query()”但使用“mysqli_query()”。

  1. mysqli_multi_query()
    The mysqli_multi_query() function performs one or more queries against the database. The queries are separated with a semicolon.
  2. mysqli_multi_query()mysqli_multi_query()函数对数据库执行一个或多个查询。查询以分号分隔。

Try this code,

试试这个代码,

1. File_Name.php

1. File_Name.php

<?php

    $servername = "..."; // Host name
    $username   = "..."; // Mysql username
    $password   = "..."; // Mysql password
    $dbname     = "..."; // Database name


    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    if(isset($_POST['barcode']) && isset($_POST['name']) && isset($_POST['kategorie']) && isset($_POST['preis']) && isset($_POST['b1']) && isset($_POST['b1_1'])
    {

        $barcode   = $_POST['barcode'];
        $name      = $_POST['name'];
        $kategorie = $_POST['kategorie'];
        $preis     = $_POST['preis'];
        $b1        = addslashes($_POST['b1']);
        $b1_1      = addslashes($_POST['b1_1']);



        $sql = "INSERT INTO produkte (barcode,name,kategorie,preis,b1,b1_1) VALUES ('$barcode', '$name', '$kategorie', '$preis', '$b1', '$b1_1')";

        if ($conn->mysqli_query($sql) === TRUE) {
            echo "New records created successfully";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }

    $conn->close();
?>

2.File_Name.html

2.File_Name.html

<html>
    <head>
        <meta charset="utf-8">
        <title>Produkt hinzuf&uuml;gen</title>
    </head>
    <body>
        <form action="eintragen.php" method="POST"/> 
            Barcode: <input type="text" name="barcode"/><br/> 
            Name: <input type="text" name="name"/><br/> 
            Kategorie: <input type="text" name="kategorie"/><br/> 
            Preis:<input type="text" name="preis"/><br/> 
            Beschreibungstext 1: <input type="text" name="b1" /><br/>
            Beschreibungstext 1.1: <input type="text" name="b1_1"/><br/> 
            <input type="submit" value="Absenden"/>
        </form>
    </body>
</html>

I hope my answer is helpful. If any query so comment please.

我希望我的回答很有帮助。如果有任何疑问请评论。

#1


4  

Add method in the place of action

在行动地点添加方法

<form action="eintragen.php" method="POST"/> 

Try this it will help

试试这个会有所帮助

#2


3  

Set your Form like this,

像这样设置你的表格,

<form action="eintragen.php" method="POST"/> 

And if you are using one query then you can use it like,

如果您使用一个查询,那么您可以使用它,如,

mysqli_query($conn,$sql);

#3


0  

You don't check if $_POST contains the data you need before assigning them to variables.

在将它们分配给变量之前,不要检查$ _POST是否包含所需的数据。

If you did, you would have noticed that your $_POST is empty because your wrote action="POST" instead of method="POST". Correct that and it should be just fine.

如果你这样做了,你会注意到你的$ _POST是空的,因为你的写操作=“POST”而不是method =“POST”。纠正这一点,应该没问题。

#4


-1  

Hello till36,

你好到36岁,

Definition and Usage of POST/GET

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute).

method属性指定如何发送表单数据(表单数据发送到action属性中指定的页面)。

The form-data can be sent as URL variables (with method="get") or as HTTP post transaction (with method="post").

表单数据可以作为URL变量(使用method =“get”)发送,也可以作为HTTP post transaction发送(使用method =“post”)。

Notes on GET:

关于GET的说明:

Appends form-data into the URL in name/value pairs The length of a URL is limited (about 3000 characters) Never use GET to send sensitive data! (will be visible in the URL) Useful for form submissions where a user want to bookmark the result GET is better for non-secure data, like query strings in Google

将表单数据附加到名称/值对的URL中URL的长度是有限的(大约3000个字符)绝不使用GET发送敏感数据! (将在URL中显示)对于表单提交非常有用,其中用户想要为结果添加书签GET更适合非安全数据,例如Google中的查询字符串

Notes on POST:

关于POST的说明:

Appends form-data inside the body of the HTTP request (data is not shown is in URL) Has no size limitations Form submissions with POST cannot be bookmarked

将表单数据附加到HTTP请求的正文中(数据未显示在URL中)没有大小限制使用POST的表单提交无法加入书签

Suggestion

When you at a time execute one query so don't need write "mysqli_multi_query()" but use "mysqli_query()".

当你一次执行一个查询所以不需要写“mysqli_multi_query()”但使用“mysqli_query()”。

  1. mysqli_multi_query()
    The mysqli_multi_query() function performs one or more queries against the database. The queries are separated with a semicolon.
  2. mysqli_multi_query()mysqli_multi_query()函数对数据库执行一个或多个查询。查询以分号分隔。

Try this code,

试试这个代码,

1. File_Name.php

1. File_Name.php

<?php

    $servername = "..."; // Host name
    $username   = "..."; // Mysql username
    $password   = "..."; // Mysql password
    $dbname     = "..."; // Database name


    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    if(isset($_POST['barcode']) && isset($_POST['name']) && isset($_POST['kategorie']) && isset($_POST['preis']) && isset($_POST['b1']) && isset($_POST['b1_1'])
    {

        $barcode   = $_POST['barcode'];
        $name      = $_POST['name'];
        $kategorie = $_POST['kategorie'];
        $preis     = $_POST['preis'];
        $b1        = addslashes($_POST['b1']);
        $b1_1      = addslashes($_POST['b1_1']);



        $sql = "INSERT INTO produkte (barcode,name,kategorie,preis,b1,b1_1) VALUES ('$barcode', '$name', '$kategorie', '$preis', '$b1', '$b1_1')";

        if ($conn->mysqli_query($sql) === TRUE) {
            echo "New records created successfully";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }

    $conn->close();
?>

2.File_Name.html

2.File_Name.html

<html>
    <head>
        <meta charset="utf-8">
        <title>Produkt hinzuf&uuml;gen</title>
    </head>
    <body>
        <form action="eintragen.php" method="POST"/> 
            Barcode: <input type="text" name="barcode"/><br/> 
            Name: <input type="text" name="name"/><br/> 
            Kategorie: <input type="text" name="kategorie"/><br/> 
            Preis:<input type="text" name="preis"/><br/> 
            Beschreibungstext 1: <input type="text" name="b1" /><br/>
            Beschreibungstext 1.1: <input type="text" name="b1_1"/><br/> 
            <input type="submit" value="Absenden"/>
        </form>
    </body>
</html>

I hope my answer is helpful. If any query so comment please.

我希望我的回答很有帮助。如果有任何疑问请评论。