-1

I just started with PHP and SQL and wanted to insert a submitted form into a mysql table. I got a lot of things from already answered questions here, but now encountered the problem that the data is not inserted into the table and I do not get any errormessage. Checking it with affected_rows results in -1 but I am unable to find my mistake.

The form

<form method="post" action="anmeld.php">
<table>
    <tr>
        <td>
            Familie/Name:
        </td><td>
            <input type="text" name="name" required>
        </td>
    </tr>

    <tr>
        <td>
            Wir kommen  Ja&nbsp;<input type="radio" name="anwesend" value="ja" checked>
            &nbsp;Nein&nbsp;<input type="radio" name="anwesend" value="nein">
        </td>
    </tr>

    <tr>
        <td>
            Wir sind 
        </td>
        <td><input type="number" name="adult">&nbsp;Erwachsene.
        </td>
    </tr>
    <tr>
        <td><br> Unsere Kinder sind<br>
            Kind <br>Kind<br>Kind<br>Kind
        </td>
        <td><br><br>
            <input type="number" name="child1"> Jahre alt. <br><input type="number" name="child2"> Jahre alt. <br><input type="number" name="child3"> Jahre alt. <br><input type="number" name="child4"> Jahre alt.
        </td>
    </tr>

    <tr>
        <td>
            <input type="submit" name="submit" value="Abschicken">
        </td>
    </tr>

</table>

php insert

    <?php

$mysqli = new mysqli("127.0.0.1", "user", "userpw", "dbname");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_err;
}

if (!empty($_POST["submit"])) {
    $familie = $_POST["name"];
    $anwesend = $_POST["anwesend"];
    $adult = $_POST["adult"];
    $child1 = $_POST["child1"];
    $child2 = $_POST["child2"];
    $child3 = $_POST["child3"];
    $child4 = $_POST["child4"];

    $sql = "INSERT INTO 'guests' ('id', 'familie', 'anwesend', 'adult', 'child1', 'child2', 'child3', 'child4')
 VALUES ('', '$familie', '$anwesend', '$adult', '$child1', '$child2', '$child3', '$child4')";

    $mysqli->query($sql);

    printf("Affected rows (INSERT): %d\n", $mysqli->affected_rows);
}
?>

I think it's something really obvious, I simply overlook but I hope that you guys could help me.

EDIT: yeah it's been the ` and '. Now works fine. Thank you guys

Keksekopf
  • 1
  • 1
  • SQL Injection...use prepared statements... – Rafael Jan 28 '15 at 14:42
  • is id auto increment/primary key? if so, you might want to remove the field all together as well as the `'',` in values. Are any of the other fields numeric values? if so they don't need apostrophe's around them. Also look at prepared statements vs straight queries to increase security. have you tried echoing the error if it exists? oh and `'guests'` shouldn't have the apostrophes either – Dave Goten Jan 28 '15 at 14:44
  • Protip example: `$sql = "Your SQL statement here"; $result = mysqli_query($sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error(), E_USER_ERROR);` which you're not doing. Or simply `echo $mysqli->error;` after your query. – Funk Forty Niner Jan 28 '15 at 15:03

1 Answers1

0

You are using the incorrect identifiers for your table and columns, being quotes.

Remove the quotes around your table and columns:

INSERT INTO guests (id, familie, anwesend, adult, child1, child2, child3, child4)

For more information on identifier qualifiers, consult:

Use echo $mysqli->error; after your query, which would have signaled the syntax errors.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Rafael
  • 7,605
  • 13
  • 31
  • 46