1

I'm trying to learn how to use databases in PHP.

My main html file creates somewhat like a login form. The data which was used as input there is redirected to the PHP file, where it should be printed and also stored in the database.

<?php error_reporting(E_ALL);

        function db_con($data_array){
            $db = mysqli_connect("localhost", "root", "", "form") or die("Fehler beim Herstellen der Verbindung zur Datenbank!");
            mysqli_set_charset($db, 'utf8');

            mysqli_query($db, "
                INSERT INTO 'data'
                (
                    'E-Mail', 'Name', 'Bewertung', 'Kommentar', 'Thema'
                )
                VALUES
                (
                    '".$data_array['E-Mail']."',
                    '".$data_array['Name']."',
                    '".$data_array['Buchbewertung']."',
                    '".$data_array['Kommentar']."',
                    '".$data_array['Lieblingsthema']."'
                )
            ") or die("Fehler beim Schreiben der Datensätze!");
        }

            if(isset($_POST['name'], $_POST['email'], $_POST['rating'], $_POST['comment'], $_POST['interest'])){
            $data_array = array(
                    "Name" => $_POST['name'],
                    "E-Mail" => $_POST['email'],
                    "Buchbewertung" => $_POST['rating'],
                    "Kommentar" => $_POST['comment'],
                    "Lieblingsthema" => $_POST['interest']
                );

            echo "<th colspan='2'>Folgende Daten wurden übermittelt: </th>";
            echo "<br><br>";

                foreach($data_array as $key => $val){
                    echo "<tr><td><b>". $key. ": ". "</b></td><td>". $val. "</td></tr>";
                }

                db_con($data_array);
            }
        ?>

I don't get why it doesn't work. There is no error, but the script stops after mysqli_query (last output: Fehler beim Schreiben der Datensätze!).

Can someone help me why all those data sets won't be stored in the database and how to fix it?

halfer
  • 19,824
  • 17
  • 99
  • 186
Stöger
  • 155
  • 1
  • 11
  • Try defining your query as a variable first like `$query="INSERT...`, then you can execute the query like `mysqli_query($db,$query)`. this also allows you to do something like `echo $query;` to display the actual query on your page and see if there are any errors with it. – WheatBeak Feb 23 '16 at 21:06
  • Have you checked your error logs? – Jay Blanchard Feb 23 '16 at 21:06
  • PHPMyAdmin *is not* a database. It is a web interface for your MySQL database. – Jay Blanchard Feb 23 '16 at 21:07
  • Add error reporting to the top of your file(s) right after your opening ` – Jay Blanchard Feb 23 '16 at 21:07
  • 1
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). – Jay Blanchard Feb 23 '16 at 21:07
  • @JayBlanchard yea, I'm using a database which I created by using this webinterface. But still. – Stöger Feb 23 '16 at 21:33

3 Answers3

1

Guten Abend Stoger

Biggest problem with arrays is no output and debug Use this:

<pre>
  <?php print_r($_POST); print_r($_GET); ?>
</pre>

This will show you POST and GET values/ Also can use print_r($data_array). Use the pre tags so it formats. First step is to make sure something is in POST. I don;t see a form or where you get your user inputs from. Normally a form asks this and then you assign the POST values to variable

i.e. $name = $_POST['name']);

So add those lines in a php page and then see if array empty or not. Likely it is. If not then you need to see if the fail is mysql insert and you can set some debug there to fail if no insert. I am new but still your layout and detail can't be answered without more. Gruss!

Kevin Morris
  • 115
  • 1
  • 1
  • 9
  • Thanks for your Answer! I already fixed my problem, the right code was: `$sql = "INSERT INTO `data`(`E-Mail`,`Name`,`Bewertung`,`Kommentar`,`Interesse`) VALUES ('$email','$_POST["name"]','$rating','$comment','$interest')";` with setting the variables like shown some answers below. – Stöger Feb 24 '16 at 08:34
0

I think that you don't have correct credentials from 'root', you should try to make the same but with other user of database.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    [It's not hard to earn enough rep to make comments.](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead) – Jay Blanchard Feb 23 '16 at 21:37
0
if(isset($_POST['name'], $_POST['email'], $_POST['rating']$_POST['comment'], $_POST['interest'])){

                    $name = $_POST['name']);
                    $email = $_POST(['email']);
                    $rating = $_POST['rating']);
                    $comment = $_POST(['comment']);
                    $interest = $_POST(['interest']);


$sql = "INSERT INTO data ". "(E-Mail, Name, Bewertung, Kommentar, Thema) " . "VALUES('$email', '$name', '$rating', '$comment', '$e', '$interest')";
mysql_select_db('DATABASE NAME');
            $retval = mysql_query($sql, $conn); //$conn for your connection to database

            if(! $retval) {die('Could not enter data: ' . mysql_error());}}
}
Zeshan Sajid
  • 559
  • 3
  • 14
  • Thanks for your code. I put it in mine which got me to at least not having the "error" anymore. But still no data sets are in the database, do you know anything more about it how I could solve that or what the problem could be? – Stöger Feb 23 '16 at 21:35
  • echo the variables to see what you are feeding in database. – Zeshan Sajid Feb 24 '16 at 08:04