0

I got a little form:

<form id="plannerform" action="save.php" method="post">
    <input id="plannername" placeholder=" " type="text" autocomplete="off" name="plannername">
    <input id="plannersubmit" type="submit" value="eintragen">
</form>

As you can see there is the action="save.php" and method="post" on the text-input there is name="plannername".

And thats my php:

$con = mysql_connect("myHost","myUser","myPW");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("myDB", $con);

$sql="INSERT INTO anmeldungen (FR_PM)
VALUES ('$_POST[plannername]')";

if (!mysql_query($sql,$con))
{
  die('Error: ' . mysql_error());
}
echo "1 record added";

The FR_PM is one column of my table. But when I press submit, not even a new row gets created. Nothing happens. But when I call my php with "mywebsite.com/save.php" it adds a new row in my table (with no value at "FR_PM", what's pretty obvious)

What do I do wrong?

Pathik Vejani
  • 4,263
  • 8
  • 57
  • 98
Tobias Glaus
  • 3,008
  • 3
  • 18
  • 37
  • ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Feb 22 '17 at 12:53
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Feb 22 '17 at 12:53

4 Answers4

3

one of the things that you need to learn if you are a beginner, you should try by all means to stay away from using mysql_* function this is depreciated and its no longer supported in php. instead use mysqli_* with prepared statements, or use PDO prepared statements.

prepared statments make you code looks clean and its easy to debug.

this is you example with prepared statements.

<form id="plannerform" action="save.php" method="post">
    <input id="plannername" placeholder=" " type="text" autocomplete="off" name="plannername">
    <input id="plannersubmit" type="submit" value="eintragen" name="submit">
 </form>

save.php

<?php
$servername = "localhost";
$username   = "root";
$password   = "";
$dbname     = "myDB";

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

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

if (isset($_POST['submit'])) {

    if (empty($_POST['plannername'])) {

        die("Enter plannername");
    } else {
        // prepare and bind
        $stmt = $conn->prepare("INSERT INTO anmeldungen (FR_PM) VALUES (?)");
        $stmt->bind_param("s", $_POST['plannername']);

        if ($stmt->execute()) {

            echo "New records created successfully";

        } else {

            echo "Could not insert record";
        }

        $stmt->close();

    }
}
?>

The reason I used prepared statements :

  • Prepared statements reduces parsing time as the preparation on the query is done only once (although the statement is executed multiple times)
  • Bound parameters minimize bandwidth to the server as you need send only the parameters each time, and not the whole query
  • Prepared statements are very useful against SQL injections, because parameter values, which are transmitted later using a different protocol, need not be correctly escaped. If the original statement template is not derived from external input, SQL injection cannot occur.

But when I call my php with "mywebsite.com/save.php" it adds a new row in my table (with no value at "FR_PM", what's pretty obvious)

What do I do wrong?

Well do prevent that from happening you need to check if the form was submitted before you can actual process any thing.

Note: If we want to insert any data from external sources (like user input from a form ), it is very important that the data is sanitized and validated. always treat input from a form as if its from a very dangerous hacker

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
  • I am using your code, but still. nothing is happening when I submit. – Tobias Glaus Feb 22 '17 at 12:30
  • did you update your form and add the name attribute to your button? this code should work. @TobiasGlaus – Masivuye Cokile Feb 22 '17 at 12:33
  • @MasivuyeCokile Ah, half of the problem solved. But now something strange happens. Can you come into this chat? http://chat.stackoverflow.com/rooms/42482/web-developers I post an image there – Tobias Glaus Feb 22 '17 at 12:37
0

change your insert query:

$sql="INSERT INTO anmeldungen (FR_PM) VALUES ('".$_POST["plannername"]."')";

Or

$plannername = $_POST["plannername"];
$sql="INSERT INTO anmeldungen (FR_PM) VALUES ('".$plannername."')";
Pathik Vejani
  • 4,263
  • 8
  • 57
  • 98
0

Also, use "name"= and not "id"= in the HTML form. This is usually misleading when working with forms and HTTP POST method.

NotANumber
  • 144
  • 9
-1

you may try

$value = $_POST['plannername'];

$sql="INSERT INTO anmeldungen (FR_PM) VALUES ('{$value}')";