2

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

<?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.

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

Would be nice if you could help me...

Cheers, Till

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
till36
  • 75
  • 1
  • 8
  • 3
    show your html form here – RAUSHAN KUMAR Jul 19 '17 at 09:59
  • Barcode:
    Name:
    Kategorie:
    Preis:
    Beschreibungstext 1:
    Beschreibungstext 1.1:
    – till36 Jul 19 '17 at 10:01
  • Check your `var_dump($_POST);` – Virb Jul 19 '17 at 10:02
  • 2
    change `action="POST"` to `method="POST"` – RAUSHAN KUMAR Jul 19 '17 at 10:02
  • 1
    Your form is using GET method and you are using $_POST – Masivuye Cokile Jul 19 '17 at 10:04
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Jul 19 '17 at 10:06
  • You have two actions in your form. change action="post" to method="post" – mapmalith Jul 19 '17 at 10:10

4 Answers4

4

Add method in the place of action

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

Try this it will help

akshay saxena
  • 447
  • 2
  • 11
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);
Virb
  • 1,639
  • 1
  • 16
  • 25
0

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

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.

Sarkouille
  • 1,275
  • 9
  • 16
-1

Hello till36,

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).

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

Notes on 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

Notes on 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

Suggestion

When you at a time execute one query so don't need write "mysqli_multi_query()" but use "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.

Try this code,

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

<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.

Mayur Vora
  • 922
  • 2
  • 14
  • 25