Storing form input in phpMyAdmin database with PHP

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 "Folgende Daten wurden übermittelt: "; echo "

"; foreach($data_array as $key => $val){ echo "". $key. ": ". "". $val. ""; } 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? s9fashion.com

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

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

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.

No! You shouldn’t simply assign a POST (or GET) variable to a local variable without performing some form of sanitisation and/or validation.

Don’t use quotation marks around table name, you can use backticks instead of quotation marks or just write table name.

Also your query is opened for SQL injection attack, use prepared statements.

Change INSERT INTO 'data' to INSERT INTO data.

Why are you sending a self created array to your function? You already have the global POST array available, just use it.

Your query is failing. Turn on error reporting and check your error log. Php is more than happy to tell you EXACTLY what is wrong if you ask it nicely.

I recommend you use PDO. You are not correctly using Mysqli anyways. Here is a good PDO tutorial to get you going. https://phpdelusions.net/pdo

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.