Bootstrap Modal -> Datenbank

RedlightX

Aktiver NGBler
Registriert
18 Juli 2013
Beiträge
1.094
Hallo zusammen,

ich möchte eine kleine Webanwendung zur Comicverwaltung schreiben, die auf meinem NAS abgelegt wird.

Um einen neuen Datensatz anzulegen, nutze ich ein Modal. Die eingetragenen Daten sollen dann in einer Datenbank abgespeichert werden. - Allerdings liegt genau da das Problem -> Sie werden nicht gespeichert :(

[src=html5]
<!-- Modal -->
<div class="modal fade" id="newcomic" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="form" method="post">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">
Neuen Comic hinzufügen
</h4>

</div>
<div class="modal-body">
<form action="addcomic.php">
<div class="form-group">
<label for="inputTitle">Titel</label>
<input type="text" class="form-control" name="inputTitle" id="inputTitle" placeholder="Titel eingeben">
</div>
<div class="form-group">
<label for="inputSeries">Serie</label>
<input type="text" class="form-control" name="inputSeries "id="inputSeries" placeholder="Serie eingeben">
</div>
<div class="form-group">
<label for="inputNumber">Nummer</label>
<input type="text" class="form-control" name="inputNumber" id="inputNumber" placeholder="Nummer des Comics eingeben">
</div>
<div class="form-group">
<label for="inputRelease">Erscheinungsjahr</label>
<input type="text" class="form-control" name="inputRelease "id="inputRelease" placeholder="Erscheinungsjahr eingeben">
</div>
<div class="form-group">
<label for="inputPublisher">Verlag</label>
<input type="text" class="form-control" name="inputPublisher" id="inputPublisher" placeholder="Verlag eingeben">
</div>
<div class="form-group">
<label for="inputBookcondition">Zustand</label>
<input type="text" class="form-control" name="inputBookcondition" id="inputBookcondition" placeholder="Zustand eingeben">
</div>
<div class="form-group">
<label for="inputPrice">Preis</label>
<input type="text" class="form-control" name="inputPrice" id="inputPrice" placeholder="Preis eingeben">
</div>


<div class="form-group">
<label for="inputImage">Bild hochladen</label>
<input type="file" name="inputImage" id="inputImage">
</div>

<div class="form-group">
<label for="inputNotes">Notizen</label>
<input type="text" class="form-control" name="inputNotes "id="inputNotes" placeholder="Notizen eingeben">
</div>
<button type="button" class="btn btn-danger" data-dismiss="modal">Abbrechen</button>
<button type="submit" name="submit" id="submit" class="btn btn-success" value="submit">In Datenbank speichern</button>
</form>
</div>

</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->[/src]

Beim Absenden, soll die addcomic.php die Daten entsprechend weiterverarbeiten und sie dann in die Datenbank ablegen...

[src=php]<?php

//Variablen
$inputTitle = $POST['inputTitle'];
$inputSeries = $POST['inputSeries'];
$inputNumber = $POST['inputNumber'];
$inputRelease = $POST['inputRelease'];
$inputPublisher = $POST['inputPublisher'];
$inputBookcondition = $POST['inputBookcondition'];
$inputPrice = $POST['inputPrice'];
$inputImage = $POST['inputImage'];
$inputNotes = $POST['inputNotes'];

//DB
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "comics";


if(isset($_POST["submit"]) == "submit")
{


try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO comic (title, series, comicnumber, yearpublished, publisher, bookcondition, price, image, notes) VALUES (:inputTitle,:inputSeries,:inputNumber,:inputRelease,:inputPublisher,:inputBookcondition,:inputPrice,:inputImage,:inputNotes)";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}

$conn = null;



}
?>[/src]

Ich bin echt ratlos und habe auch schon das Web durchforstet :(
 
Dein Problem ist, du definierst Platzhalter im SQL Befehl (z.B. :inputTitle), sagst PDO aber nicht, was du dort einsetzen willst. Das hätte eigentlich auch eine Exception auswerfen sollen.

Schau dir mal diese Doku genauer an:


Nach deiner Zeile 29 sollte es so aussehen:
[src=php]
$sth = $conn->prepare($sql);
$sth->bindValue(':inputTitle', $_POST['inputTitle'], PDO::PARAM_STR);
$sth->bindValue(...); // Deine restlichen Felder, das PDO::PARAM_STR solltest du je nach Datentyp austauschen
$sth->execute();
//Zeile 31 fällt dann weg
[/src]
 
Zuletzt bearbeitet:
  • Thread Starter Thread Starter
  • #3
Hallo,

danke für deinen Beitrag!

Ich habe mir nun die Doku durchgelesen und auch das umgesetzt, was du geposted hast, leider noch immer ohne Erfolg... Eine Exception wird ebenfalls nicht ausgeworfen.
Bei der Dateneingabe im Modal wird "ordnungsgemäß" auf die addcomic.php weitergeleitet und hat auch sämtliche Parameter in der URL. Ein Eintrag in der Datenbank erfolg jedoch nicht :(


[src=php]if(isset($_POST["submit"]) == "submit")
{


try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO comic (title, series, comicnumber, yearpublished, publisher, bookcondition, price, image, notes) VALUES (:inputTitle,:inputSeries,:inputNumber,:inputRelease,:inputPublisher,:inputBookcondition,:inputPrice,:inputImage,:inputNotes)";

$sth = $conn->prepare($sql);
$sth->bindValue(':inputTitle', $POST['inputTitle'], PDO::PARAM_STR);
$sth->bindValue(':inputSeries', $POST['inputSeries'], PDO::PARAM_STR);
$sth->bindValue(':inputNumber', $POST['inputNumber'], PDO::PARAM_INT);
$sth->bindValue(':inputRelease', $POST['inputRelease'], PDO::PARAM_INT);
$sth->bindValue(':inputPublisher', $POST['inputPublisher'], PDO::PARAM_STR);
$sth->bindValue(':inputBookcondition', $POST['inputBookcondition'], PDO::PARAM_STR);
$sth->bindValue(':inputPrice', $POST['inputPrice'], PDO::PARAM_STR);
$sth->bindValue(':inputImage', $POST['inputImage'], PDO::PARAM_STR);
$sth->bindValue(':inputNotes', $POST['inputNotes'], PDO::PARAM_STR);

$sth->execute();


echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}

$conn = null;



}
?>[/src]
 
Dein Fehler dürfte schon ganz oben in der ersten Zeile beim Call zu isset() sein. Die Funktion returnt TRUE oder FALSE, du prüfst aber ob sie den String "submit" zurückgibt.

Sollt eher so sein:

[src=php]
if(isset($_POST["submit"]) && $_POST["submit"] == "submit")
{
....
}
[/src]
 
Damit ist auch klar wieso keine Exception ausgeworfen wurde, auf die Zeile hatte ich gar nicht geachtet.
 
  • Thread Starter Thread Starter
  • #6
Hallo ihr beiden,

leider hat auch Larry's Beitrag es nicht zum Laufen gebracht :(

Beim Submit bleibt die Seite weiss und es erfolgen keine Eintragungen in die Datenbank :(


Zwecks besserer Übersicht, hier nochmals die aktuelle addcomic.php

[src=php]if(isset($_POST["submit"]) && $_POST["submit"] == "submit")
{


try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO comic(title, series, comicnumber, yearpublished, publisher, bookcondition, price, image, notes) VALUES (:inputTitle,:inputSeries,:inputNumber,:inputRelease,:inputPublisher,:inputBookcondition,:inputPrice,:inputImage,:inputNotes)";

$sth = $conn->prepare($sql);
$sth->bindValue(':inputTitle', $POST['inputTitle'], PDO::PARAM_STR);
$sth->bindValue(':inputSeries', $POST['inputSeries'], PDO::PARAM_STR);
$sth->bindValue(':inputNumber', $POST['inputNumber'], PDO::PARAM_INT);
$sth->bindValue(':inputRelease', $POST['inputRelease'], PDO::PARAM_INT);
$sth->bindValue(':inputPublisher', $POST['inputPublisher'], PDO::PARAM_STR);
$sth->bindValue(':inputBookcondition', $POST['inputBookcondition'], PDO::PARAM_STR);
$sth->bindValue(':inputPrice', $POST['inputPrice'], PDO::PARAM_STR);
$sth->bindValue(':inputImage', $POST['inputImage'], PDO::PARAM_STR);
$sth->bindValue(':inputNotes', $POST['inputNotes'], PDO::PARAM_STR);

$sth->execute();


echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}

$conn = null;


}
?>[/src]

Ich verstehe nur nicht, wieso überhaupt keine Exception ausgeworfen wird...
 
Das klingt jetzt aber - wenn du nur eine blanke Seite siehst - eher nach einem syntaktischen Fehler (z. B. Klammer oder Strichpunkt vergessen). Das würd auch erklären, warum noch immer keine Exception geworfen wird.

Schau doch einmal ob du in der Apache Error Log einen entsprechenden PHP Error findest:

[src=bash]tail -f /var/log/apache2/error.log[/src]

Schau dir auch einmal genau an, was in deinem $_POST Array so drin steht. Mach dafür vor deinem if() soetwas wie:

[src=php]print "<pre>";
print_r($_POST);
print "</pre>";[/src]

Prüf hier, ob $_POST['submit'] überhaupt den Wert "submit" hat.

Weiter unten hast du auch $_POST mit $POST verwechselt, hier zum Beispiel:

[src=php]$sth->bindValue(':inputTitle', $POST['inputTitle'], PDO::PARAM_STR);[/src]

Auch solltest du prüfen, ob du alle notwendigen includes richtig eingebunden hast. Wenn alles nichts hilft, würd ich empfehlen am Server zu installieren, damit du anständig debuggen kannst.
 
Zuletzt bearbeitet:
Momentan fängst du nur PDOExceptions ab, unten nach Zeile 30 kannst du daher folgendes anhängen um andere Exceptions abzufangen:
[src=php]
catch(Exception $e)
{
echo $e->getMessage();
}
[/src]

Solltest du auch nicht an den Server-log kommen füge ganz oben folgendes hinzu damit die Fehler direkt ausgegeben werden:
[src=php]ini_set('display_errors', 1);
error_reporting(E_ALL);[/src]
 
Zuletzt bearbeitet:
  • Thread Starter Thread Starter
  • #9
Das Array, welches übergeben wird, ist leer :(

"Array
(
)
"

In die Logs schaue ich morgen Mittag rein - Fehler von oben habe ich angepasst.

Danke für eure Hilfe!
 
Dürfte daran liegen, dass du aktuell ein GET machst, damit sollte es besser gehen:
<form action="addcomic.php" method="post">
 
  • Thread Starter Thread Starter
  • #11
Es funktioniert :T

Für die Nachwelt (Ja, ich muss sorgfältiger sein)

- > Anführtungszeichen in HTML richtig positionieren
- > POST vergessen
- > etc

Ich danke euch, ich hab hier wirklich was gelernt!

[src=html5]<form action="addcomic.php" method="post">
<div class="form-group">
<label for="inputTitle">Titel</label>
<input type="text" class="form-control" name="inputTitle" id="inputTitle" placeholder="Titel eingeben">
</div>
<div class="form-group">
<label for="inputSeries">Serie</label>
<input type="text" class="form-control" name="inputSeries" id="inputSeries" placeholder="Serie eingeben">
</div>
<div class="form-group">
<label for="inputNumber">Nummer</label>
<input type="text" class="form-control" name="inputNumber" id="inputNumber" placeholder="Nummer des Comics eingeben">
</div>
<div class="form-group">
<label for="inputRelease">Erscheinungsjahr</label>
<input type="text" class="form-control" name="inputRelease" id="inputRelease" placeholder="Erscheinungsjahr eingeben">
</div>
<div class="form-group">
<label for="inputPublisher">Verlag</label>
<input type="text" class="form-control" name="inputPublisher" id="inputPublisher" placeholder="Verlag eingeben">
</div>
<div class="form-group">
<label for="inputBookcondition">Zustand</label>
<input type="text" class="form-control" name="inputBookcondition" id="inputBookcondition" placeholder="Zustand eingeben">
</div>
<div class="form-group">
<label for="inputPrice">Preis</label>
<input type="text" class="form-control" name="inputPrice" id="inputPrice" placeholder="Preis eingeben">
</div>


<div class="form-group">
<label for="inputImage">Bild hochladen</label>
<input type="file" name="inputImage" id="inputImage">
</div>

<div class="form-group">
<label for="inputNotes">Notizen</label>
<input type="text" class="form-control" name="inputNotes" id="inputNotes" placeholder="Notizen eingeben">
</div>
<button type="button" class="btn btn-danger" data-dismiss="modal">Abbrechen</button>
<button type="submit" name="submit" id="submit" class="btn btn-success" value="submit">In Datenbank speichern</button>
</form>[/src]



[src=php]if(isset($_POST["submit"]) && $_POST["submit"] == "submit")
{



try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO comic(title, series, comicnumber, yearpublished, publisher, bookcondition, price, image, notes) VALUES (:inputTitle,:inputSeries,:inputNumber,:inputRelease,:inputPublisher,:inputBookcondition,:inputPrice,:inputImage,:inputNotes)";

$sth = $conn->prepare($sql);
$sth->bindValue(':inputTitle', $_POST['inputTitle'], PDO::PARAM_STR);
$sth->bindValue(':inputSeries', $_POST['inputSeries'], PDO::PARAM_STR);
$sth->bindValue(':inputNumber', $_POST['inputNumber'], PDO::PARAM_STR);
$sth->bindValue(':inputRelease', $_POST['inputRelease'], PDO::PARAM_STR);
$sth->bindValue(':inputPublisher', $_POST['inputPublisher'], PDO::PARAM_STR);
$sth->bindValue(':inputBookcondition', $_POST['inputBookcondition'], PDO::PARAM_STR);
$sth->bindValue(':inputPrice', $_POST['inputPrice'], PDO::PARAM_STR);
$sth->bindValue(':inputImage', $_POST['inputImage'], PDO::PARAM_STR);
$sth->bindValue(':inputNotes', $_POST['inputNotes'], PDO::PARAM_STR);

$sth->execute();


echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}

catch(Exception $e)
{
echo $e->getMessage();
}

$conn = null;


}[/src]
 
  • Thread Starter Thread Starter
  • #12
Ich hoffe, ich darf nochmals nerven :D

Es werden ja nun erfreulicherweise via POST sämtliche Inputs ans PHP-Script gesendet. (Wunderprächtig :T)
Allerdings bin ich (Zugegebenermaßen seit heute Vormittag) dabei, den Imageupload hinzukriegen, doch es will nicht. Sämtliche Snippets im Netz wollten nicht laufen.

Im Grunde soll folgendes passieren:

Im Modal ist ein Input-File-Feld. Dort -> Bild auswählen -> Zusammen mit den anderen Daten via POST an PHP -> Bild in den Ordner "cover" und Pfad in die Datenbank schreiben.


Wenn ich nun aber 'inputImage' weiterverarbeiten möchte, geht es nicht, da folgendes Meldung kommt:

Notice: Undefined index: inputImage in /volume1/web/comics/addcomic.php on line 39 Notice: Undefined index: inputImage in /volume1/web/comics/addcomic.php on line 44 Warning: getimagesize(): Filename cannot be empty in /volume1/web/comics/addcomic.php on line 44 File is not an image.

Das Snippet habe ich in das obige Script mit reingepackt, und sieht folgendermaßen aus:

[src=php]// Check for errors
if($_FILES['inputImage']['error'] > 0){
die('An error ocurred when uploading.');
}

if(!getimagesize($_FILES['inputImage']['tmp_name'])){
die('Please ensure you are uploading an image.');
}

// Check filetype
if($_FILES['inputImage']['type'] != 'image/png'){
die('Unsupported filetype uploaded.');
}

// Check filesize
if($_FILES['inputImage']['size'] > 500000){
die('File uploaded exceeds maximum upload size.');
}

// Check if the file exists
if(file_exists('upload/' . $_FILES['inputImage']['name'])){
die('File with that name already exists.');
}

// Upload file
if(!move_uploaded_file($_FILES['inputImage']['tmp_name'], 'cover/' . $_FILES['inputImage']['name'])){
die('Error uploading file - check destination is writeable.');
}

die('File uploaded successfully.');[/src]

Scheinbar scheint er mit inputImage nicht wirklich viel anfangen zu können...
Hat jemand eine Idee?
 
Zuletzt bearbeitet:
In deinem $_FILES Array ist inputImage nicht gesetzt. Hast du in deiner HTML Form sicher ein ein Field in dieser Art:

[src=html4strict]<input type="file" name="inputImage" size="40">[/src]
 
  • Thread Starter Thread Starter
  • #14
Der HTML Ausschnit, welches sich natürlich ebenfalls im POST-Formular befindet, sieht folgend aus:

Oben am Form-Beginn:

[src=html5] <form action="addcomic.php" method="post" enctype='multipart/form-data'>[/src]


[src=html5]<div class="form-group">
<label for="inputImage">Bild hochladen</label>
<input type="file" name="inputImage" id="inputImage" class="form-control">

</div>[/src]


Das Array gibt nun folgende Werte aus:

[src=php]Array
(
[inputTitle] => zzzz
[inputSeries] => zzzz
[inputNumber] => 565656
[inputRelease] => 565656
[inputPublisher] => 5666
[inputBookcondition] => 56566
[inputPrice] => 5656
[inputNotes] => 5656665655
[addComic] => submit
)
Array
(
[inputImage] => Array
(
[name] => IMG_0218.JPG
[type] => image/jpeg
[tmp_name] => /volume1/@tmp/phpeayhcp
[error] => 0
[size] => 3174545
)

)[/src]


Das entsprechende PHP-Script sieht so aus: (Keine Errors, Datenbankeinträge erfolgen - Beim Bild jedoch nur der Name ohne PFad und das Bild erscheint nicht im Ordner "cover")


[src=php]<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);


print "<pre>";
print_r($_POST);
print "</pre>";
print "<pre>";
print_r($_FILES);
print "</pre>";

include ("includes/config.php");





if (isset($_POST["addcomic"]) && $_FILES["inputImage"] =="submit") {
// Check for errors
if ($_FILES['inputImage']['error'] > 0) {
die('An error ocurred when uploading.');
}

if (!getimagesize($_FILES['inputImage']['tmp_name'])) {
die('Please ensure you are uploading an image.');
}

// Check filetype
if ($_FILES['inputImage']['type'] != 'image/png') {
die('Unsupported filetype uploaded.');
}

// Check filesize
if ($_FILES['inputImage']['size'] > 500000) {
die('File uploaded exceeds maximum upload size.');
}

// Check if the file exists
if (file_exists('upload/' . $_FILES['inputImage']['name'])) {
die('File with that name already exists.');
}

// Upload file
if (!move_uploaded_file($_FILES['inputImage']['tmp_name'], 'cover/' . $_FILES['inputImage']['name'])) {
die('Error uploading file - check destination is writeable.');
}

die('File uploaded successfully.');


}


if(isset($_POST["addComic"]) && $_POST["addComic"] == "submit")
{


try {



$conn = new PDO("mysql:host=$DB_HOST;dbname=$DB_NAME", $DB_USER, $DB_PASS);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO comic(title, series, comicnumber, yearpublished, publisher, bookcondition, price, image, notes) VALUES (:inputTitle,:inputSeries,:inputNumber,:inputRelease,:inputPublisher,:inputBookcondition,:inputPrice,:inputImage,:inputNotes)";

$sth = $conn->prepare($sql);
$sth->bindValue(':inputTitle', $_POST['inputTitle'], PDO::PARAM_STR);
$sth->bindValue(':inputSeries', $_POST['inputSeries'], PDO::PARAM_STR);
$sth->bindValue(':inputNumber', $_POST['inputNumber'], PDO::PARAM_STR);
$sth->bindValue(':inputRelease', $_POST['inputRelease'], PDO::PARAM_STR);
$sth->bindValue(':inputPublisher', $_POST['inputPublisher'], PDO::PARAM_STR);
$sth->bindValue(':inputBookcondition', $_POST['inputBookcondition'], PDO::PARAM_STR);
$sth->bindValue(':inputPrice', $_POST['inputPrice'], PDO::PARAM_STR);
$sth->bindValue(':inputImage', $_FILES['inputImage']['name'], PDO::PARAM_STR);
$sth->bindValue(':inputNotes', $_POST['inputNotes'], PDO::PARAM_STR);

$sth->execute();




}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}

catch(Exception $e)
{
echo $e->getMessage();
}

$conn = null;


// header("location: /comics");
//die;

}



?>[/src]


Bei der Zeile 77 bin ich mir nicht sicher :(
 
Der Fehler dürfte in Zeile 20 sein:
[src=php]if (isset($_POST["addcomic"]) && $_FILES["inputImage"] =="submit") {[/src]
Das muss addComic heißen.

Zeile 77 sieht ok aus falls du dort nur den Namen speichern möchtest, soll aber der Pfad auch dazu:
[src=php]$sth->bindValue(':inputImage', 'cover/' . $_FILES['inputImage']['name'], PDO::PARAM_STR);[/src]
 
Zuletzt bearbeitet:
  • Thread Starter Thread Starter
  • #16
Ich danke dir :T
In de Datenbank wird nun der richtige Pfad gespeichert (cover/<Bildname>)

Allerdings erscheint das Bild noch immer nicht im Ordner cover.
Ordner ist vorhanden und beschreibbar :(
 
sehe es gerade erst, das sollte bestimmt nicht so heißen:
[src=php] if (isset($_POST["addcomic"]) && $_FILES["inputImage"] =="submit") {[/src]

sondern so:
[src=php] if (isset($_POST["addComic"]) && $_POST["addComic"] =="submit") {[/src]
 
Zuletzt bearbeitet:
Zurück
Oben