Sponsor-Board.de
PHP Formular in Mysql

+- Sponsor-Board.de (https://www.sponsor-board.de)
+-- Forum: Community (/forumdisplay.php?fid=56)
+--- Forum: Hilfe (/forumdisplay.php?fid=102)
+---- Forum: Scripting (/forumdisplay.php?fid=108)
+---- Thema: PHP Formular in Mysql (/showthread.php?tid=65139)


PHP Formular in Mysql - spela - 19.06.2018 10:44

Hallo, ich möchte ein Formular erstellen das wenn man es abschickt die Daten in eine MySQL Datenbank speichert.

zb Formular fragt Alter & Name
in der Datenbank wird dann ein neuer Datensatz erstellt mit dem Namen & Alter

Ich habe Probleme mit dem übernehmen in die Datenbank.

Würde mich freuen wenn mir jemand hilft:

Code:
<?php
require 'inc/db.php';

$erg = $db->query("SELECT * FROM test");


?>

<html>
<style>
input[type=text], select {
    width: 100%;
    padding: 12px 20px;
    margin: 8px 0;
    display: inline-block;
    border: 1px solid #ccc;
    border-radius: 4px;
    box-sizing: border-box;
}

input[type=submit] {
    width: 100%;
    background-color: #4CAF50;
    color: white;
    padding: 14px 20px;
    margin: 8px 0;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

input[type=submit]:hover {
    background-color: #45a049;
}

div {
    border-radius: 5px;
    background-color: #f2f2f2;
    padding: 20px;
}
</style>
<body>

<h3>Using CSS to style an HTML Form</h3>

<div>
  <form action="/action_page.php">
    <label for="fname">Alter</label>
    <input type="text" id="fname" name="alter" placeholder="zb 12">

    <label for="lname">Nummer</label>
    <input type="text" id="lname" name="nummer" placeholder="zb 5">

  
    <input type="submit" value="Submit">
  </form>
</div>

</body>
</html>




RE: PHP Formular in Mysql - Jerr0w - 19.06.2018 11:09

PHP-Code:
<?php
$mysql 
= new mysqli('localhost''username''password''database');

//Überprüfe, ob die Verbindung erfolgreich war
if ($mysql->connect_errno) {
  
printf("Couldn't connect to MySQL Server. Error: %s\n"$mysql->connect_errno);
  exit();
}

//Validierungschecks für die Eingabefelder
if (array_key_exists('alter'$_POST) && array_key_exists('nummer'$_POST) {
  
$alter $_POST['alter'];
  
$nummer $_POST['nummer'];

  
//Mit diesem Query legst du die Daten in der Datenbank ab
  
$query $mysql->query('INSERT INTO daten (alter, nummer) values (?, ?)', [
    
$alter,
    
$nummer
  
]);
}

$mysql->close();
?>

<html>
<style>
input[type=text], select {
    width: 100%;
    padding: 12px 20px;
    margin: 8px 0;
    display: inline-block;
    border: 1px solid #ccc;
    border-radius: 4px;
    box-sizing: border-box;
}

input[type=submit] {
    width: 100%;
    background-color: #4CAF50;
    color: white;
    padding: 14px 20px;
    margin: 8px 0;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

input[type=submit]:hover {
    background-color: #45a049;
}

div {
    border-radius: 5px;
    background-color: #f2f2f2;
    padding: 20px;
}
</style>


<body>
<h3>Using CSS to style an HTML Form</h3>
<div>
  <form action="" method="post" id="form">
    <label for="name">Alter</label>
    <input type="text" id="name" name="alter" placeholder="zb 12">

    <label for="name">Nummer</label>
    <input type="text" id="name" name="nummer" placeholder="zb 5">

    <input type="submit" id="submit" value="Submit">
  </form>
</div>
</body>
</html> 


Probier mal den Code. Sollte eigentlich ohne weiteres klappen

Edit: Code bisschen verfeinert.