PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Hilfe bei einem PHP Formular mit MySQL Datenbank



#cousin#
11-11-2009, 17:39
Hi Zusammen,

ich habe ein PHP-Formular generiert welches die eingegebenen Felder in eine Datenbank schrieben soll. Leider gibt es ein Problem, mein Formular schreibt nur den ersten Wert in die DB und an Stelle der andren Variablen habe ich nur Leerzeichen in der Datenbanktabelle.

Hier mal der recht lange PHP-Teil der Datei:


<?php
############################
### IP Auslesen ###
############################
$ip ="$REMOTE_ADDR";



############################
### Zufallsnummern ###
############################
$report_text_id = rand(1,3);



############################
### DB Zugriff ###
############################
$host = "localhost";
$user = "gutachten";
$password = "XXXX";
$dbname = "gutachten";

$dbverbindung = mysql_connect ($host, $user, $password);

############################
### Variablendeklaration ###
############################
if (isset($_POST['checkbox_m']))
{
$sex = "Mr";
}
if (isset($_POST['checkbox_w']))
{
$sex = "Mrs";
}
$name = $_POST['name'];
$name = str_replace("<","",$name);
$name = str_replace("\'","",$name);
$name = str_replace("'","",$name);
$name = str_replace("ä","ae",$name);
$name = str_replace("ö","oe",$name);
$name = str_replace("ü","ue",$name);
$name = str_replace("ß","ss",$name);
$name = str_replace("Ä","Ae",$name);
$name = str_replace("Ö","Oe",$name);
$name = str_replace("Ü","Ue",$name);
$first_name = $_POST['vorname'];
$first_name = str_replace("<","",$first_name);
$first_name = str_replace("\'","",$first_name);
$first_name = str_replace("'","",$first_name);
$first_name = str_replace("ä","ae",$first_name);
$first_name = str_replace("ö","oe",$first_name);
$first_name = str_replace("ü","ue",$first_name);
$first_name = str_replace("ß","ss",$first_name);
$first_name = str_replace("Ä","Ae",$first_name);
$first_name = str_replace("Ö","Oe",$first_name);
$first_name = str_replace("Ü","Ue",$first_name);
$mat_nr = $_POST['matnr'];
$mat_nr = str_replace("<","",$mat_nr);
$mat_nr = str_replace("\'","",$mat_nr);
$mat_nr = str_replace("'","",$mat_nr);
$birth_date = $_POST['"geb_tag" . "geb_monat" . "geb_jahr"'];
$birth_date = str_replace("<","",$birth_date);
$birth_date = str_replace("\'","",$birth_date);
$birth_date = str_replace("'","",$birth_date);
$birth_city = $_POST['geb_ort'];
$birth_city = str_replace("\'","",$birth_city);
$birth_city = str_replace("'","",$birth_city);
$birth_city = str_replace("ä","ae",$birth_city);
$birth_city = str_replace("ö","oe",$birth_city);
$birth_city = str_replace("ü","ue",$birth_city);
$birth_city = str_replace("ß","ss",$birth_city);
$birth_city = str_replace("Ä","Ae",$birth_city);
$birth_city = str_replace("Ö","Oe",$birth_city);
$birth_city = str_replace("Ü","Ue",$birth_city);
$birth_country = $_POST['geb_land'];
$birth_country = str_replace("\'","",$birth_country);
$birth_country = str_replace("'","",$birth_country);
$birth_country = str_replace("ä","ae",$birth_country);
$birth_country = str_replace("ö","oe",$birth_country);
$birth_country = str_replace("ü","ue",$birth_country);
$birth_country = str_replace("ß","ss",$birth_country);
$birth_country = str_replace("Ä","Ae",$birth_country);
$birth_country = str_replace("Ö","Oe",$birth_country);
$birth_country = str_replace("Ü","Ue",$birth_country);
$tel_nr = $_POST['tel_nr'];
$tel_nr = str_replace("<","",$tel_nr);
$tel_nr = str_replace("\'","",$tel_nr);
$tel_nr = str_replace("'","",$tel_nr);
$email = $_POST['email'];
$email = str_replace("<","",$email);
$email = str_replace("\'","",$email);
$email = str_replace("'","",$email);
$study_course = $_POST['studiengang'];
$study_begin = $_POST['"immat_monat" . "immat_jahr"'];
$study_begin = str_replace("<","",$study_begin);
$study_begin = str_replace("\'","",$study_begin);
$study_begin = str_replace("'","",$study_begin);
$study_end = $_POST['"end_monat" . "end_jahr"'];
$study_end = str_replace("<","",$study_end);
$study_end = str_replace("\'","",$study_end);
$study_end = str_replace("'","",$study_end);
$graduate = $_POST['abschluss'];
$report_type = $_POST['gutachten_typ'];
$comment = $_POST['comments'];
$comment = str_replace("<","",$comment);
$comment = str_replace("\'","",$comment);
$comment = str_replace("'","",$comment);
$comment = str_replace("ä","ae",$comment);
$comment = str_replace("ö","oe",$comment);
$comment = str_replace("ü","ue",$comment);
$comment = str_replace("ß","ss",$comment);
$comment = str_replace("Ä","Ae",$comment);
$comment = str_replace("Ö","Oe",$comment);
$comment = str_replace("Ü","Ue",$comment);
$uni_name = $_POST['uni_name'];
$uni_name = str_replace("<","",$uni_name);
$uni_name = str_replace("\'","",$uni_name);
$uni_name = str_replace("'","",$uni_name);
$uni_name = str_replace("ä","ae",$uni_name);
$uni_name = str_replace("ö","oe",$uni_name);
$uni_name = str_replace("ü","ue",$uni_name);
$uni_name = str_replace("ß","ss",$uni_name);
$uni_name = str_replace("Ä","Ae",$uni_name);
$uni_name = str_replace("Ö","Oe",$uni_name);
$uni_name = str_replace("Ü","Ue",$uni_name);
$uni_street = $_POST['uni_strasse'];
$uni_street = str_replace("<","",$uni_street);
$uni_street = str_replace("\'","",$uni_street);
$uni_street = str_replace("'","",$uni_street);
$uni_street = str_replace("ä","ae",$uni_street);
$uni_street = str_replace("ö","oe",$uni_street);
$uni_street = str_replace("ü","ue",$uni_street);
$uni_street = str_replace("ß","ss",$uni_street);
$uni_street = str_replace("Ä","Ae",$uni_street);
$uni_street = str_replace("Ö","Oe",$uni_street);
$uni_street = str_replace("Ü","Ue",$uni_street);
$uni_plz = $_POST['uni_plz'];
$uni_plz = str_replace("<","",$uni_plz);
$uni_plz = str_replace("\'","",$uni_plz);
$uni_plz = str_replace("'","",$uni_plz);
$uni_city = $_POST['uni_stadt'];
$uni_city = str_replace("<","",$uni_city);
$uni_city = str_replace("\'","",$uni_city);
$uni_city = str_replace("'","",$uni_city);
$uni_city = str_replace("ä","ae",$uni_city);
$uni_city = str_replace("ö","oe",$uni_city);
$uni_city = str_replace("ü","ue",$uni_city);
$uni_city = str_replace("ß","ss",$uni_city);
$uni_city = str_replace("Ä","Ae",$uni_city);
$uni_city = str_replace("Ö","Oe",$uni_city);
$uni_city = str_replace("Ü","Ue",$uni_city);
$uni_country = $_POST['uni_land'];
$uni_country = str_replace("<","",$uni_country);
$uni_country = str_replace("\'","",$uni_country);
$uni_country = str_replace("'","",$uni_country);
$uni_country = str_replace("ä","ae",$uni_country);
$uni_country = str_replace("ö","oe",$uni_country);
$uni_country = str_replace("ü","ue",$uni_country);
$uni_country = str_replace("ß","ss",$uni_country);
$uni_country = str_replace("Ä","Ae",$uni_country);
$uni_country = str_replace("Ö","Oe",$uni_country);
$uni_country = str_replace("Ü","Ue",$uni_country);
$count = $_POST['anzahl'];


############################
###DB Eintraege vornehmen###
############################
if(isset($name))
{
$db_abfrage_1 = "INSERT INTO userdata VALUES ('$id', '$name', '$first_name', '$mat_nr', '$sex', '$birth_date', '$birth_city', '$birth_country', '$tel_nr', '$email', '$study_course', '$study_begin', '$study_end', '$graduate', '$report_type', '$ip', '$comment')";
$db_abfrage_2 = "INSERT INTO report_address VALUES ('$id', '$uni_name', '$uni_street', '$uni_plz', '$uni_city', '$uni_country', '$count')";
mysql_db_query ($dbname, $db_abfrage_1, $dbverbindung);
mysql_db_query ($dbname, $db_abfrage_2, $dbverbindung);
mysql_close( $dbverbindung );
}
################################################## ################################################## ############
?>

#cousin#
11-11-2009, 17:39
Und hier der HTML-Code des Formulars, der normal direkt darunter steht:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>

<script language="JavaScript" type="text/JavaScript">
<!--
function chkFormular() {
if (document.Form.name.value == "") {
alert("Bitte teilen Sie uns Ihren Namen mit damit wir Ihre Anfrage bearbeiten können");
document.Form.name.focus();
return false;
}
if (document.Form.vorname.value == "") {
alert("Bitte teilen Sie uns Ihren Vornamen mit damit wir Ihre Anfrage bearbeiten können");
document.Form.vorname.focus();
return false;
}
if (document.Form.email.value == "") {
alert("Bitte teilen Sie uns Ihre Email-Adresse mit damit wir Ihre Anfrage bearbeiten können");
document.Form.email.focus();
return false;
}
if (document.Form.email.value.indexOf('@') == -1) {
alert("Bitte teilen Sie uns Ihre korrekte Email-Adresse mit damit wir Ihre Anfrage bearbeiten können");
document.Form.email.focus();
return false;
}
if (document.Form.matnr.value == "") {
alert("Bitte teilen Sie uns Ihre Matrikelnummer mit damit wir Ihre Anfrage bearbeiten können");
document.Form.matnr.focus();
return false;
}
if (document.Form.geb_ort.value == "") {
alert("Bitte teilen Sie uns Ihren Geburtsort mit damit wir Ihre Anfrage bearbeiten können");
document.Form.geb_ort.focus();
return false;
}
if (document.Form.geb_land.value == "") {
alert("Bitte teilen Sie uns Ihr Geburtsland mit damit wir Ihre Anfrage bearbeiten können");
document.Form.geb_land.focus();
return false;
}
if (document.Form.geb_jahr.value == "") {
alert("Bitte teilen Sie uns Ihr Geburtsdatum mit damit wir Ihre Anfrage bearbeiten können");
document.Form.geb_jahr.focus();
return false;
}
if (document.Form.geb_tag.value == "") {
alert("Bitte teilen Sie uns Ihr Geburtsdatum mit damit wir Ihre Anfrage bearbeiten können");
document.Form.geb_tag.focus();
return false;
}
if (document.Form.geb_monat.value == "") {
alert("Bitte teilen Sie uns Ihr Geburtsdatum mit damit wir Ihre Anfrage bearbeiten können");
document.Form.geb_monat.focus();
return false;
}
if (document.Form.tel_nr.value == "") {
alert("Bitte teilen Sie uns Ihre Telefonnummer mit damit wir Ihre Anfrage bearbeiten können");
document.Form.tel_nr.focus();
return false;
}
if (document.Form.studiengang.value == "") {
alert("Bitte teilen Sie uns Ihren Studiengang mit damit wir Ihre Anfrage bearbeiten können");
document.Form.studiengang.focus();
return false;
}
if (document.Form.immat_monat.value == "") {
alert("Bitte teilen Sie uns Ihre Immatrikulationsdatum mit damit wir Ihre Anfrage bearbeiten können");
document.Form.immat_monat.focus();
return false;
}
if (document.Form.immat_jahr.value == "") {
alert("Bitte teilen Sie uns Ihre Immatrikulationsdatum mit damit wir Ihre Anfrage bearbeiten können");
document.Form.immat_jahr.focus();
return false;
}
if (document.Form.end_monat.value == "") {
alert("Bitte teilen Sie uns Ihr voraussichtliches Ende des Studiums mit damit wir Ihre Anfrage bearbeiten können");
document.Form.end_monat.focus();
return false;
}
if (document.Form.end_jahr.value == "") {
alert("Bitte teilen Sie uns Ihr voraussichtliches Ende des Studiums mit damit wir Ihre Anfrage bearbeiten können");
document.Form.end_jahr.focus();
return false;
}
if (document.Form.abschluss.value == "") {
alert("Bitte teilen Sie uns Ihren angestrebten Abschluss mit damit wir Ihre Anfrage bearbeiten können");
document.Form.abschluss.focus();
return false;
}
if (document.Form.gutachten_typ.value == "") {
alert("Bitte teilen Sie uns Ihr gewünschtes Gutachten mit damit wir Ihre Anfrage bearbeiten können");
document.Form.gutachten_typ.focus();
return false;
}
}
//-->
</script>
</head>

<body>
<form name="Form" id="Form" action="gutachten.php" method="POST" onsubmit="return chkFormular()">
<table width="100%" border="0" cellspacing="10" cellpadding="0">
<tr>
<td width="26%">Name:</td>
<td width="74%"><label>
<input type="text" name="name" id="name" />
</label></td>
</tr>
<tr>
<td>First Name:</td>
<td><label>
<input type="text" name="vorname" id="vorname" />
</label></td>
</tr>
<tr>
<td>Matriculation Number:</td>
<td><label>
<input type="text" name="matnr" id="matnr" />
</label></td>
</tr>
<tr>
<td>Sex:</td>
<td>male
<input type="checkbox" name="checkbox_m" id="checkbox_m" />
<br />
female
<label>
<input type="checkbox" name="checkbox_w" id="checkbox_w" />
</label></td>
</tr>
<tr>
<td>Date of Birth:</td>
<td><label>
<input name="geb_tag" type="text" id="geb_tag" size="4" maxlength="2" />
</label>
<select name="geb_monat" id="geb_monat">
<option value="">- please choose -</option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<label>
<input name="geb_jahr" type="text" id="geb_jahr" size="6" maxlength="4" />
</label>
(dd-month-yyyy)</td>
</tr>
<tr>
<td>Place of Birth:</td>
<td><label>
<input type="text" name="geb_ort" id="geb_ort" />
</label></td>
</tr>
<tr>
<td>GebCountry of Birth:</td>
<td><label>
<input type="text" name="geb_land" id="geb_land" />
</label></td>
</tr>
<tr>
<td>Phone Number:</td>
<td><label>
<input type="text" name="tel_nr" id="tel_nr" />
</label></td>
</tr>
<tr>
<td>Email-Address:</td>
<td><label>
<input type="text" name="email" id="email" />
</label></td>
</tr>
<tr>
<td>Course of Study:</td>
<td><select name="studiengang" id="studiengang">
<option value="">- please choose -</option>
<option value="Business Administration">Business Administration</option>
<option value="Business Informatics">Business Informatics</option>
<option value="Culture and Economy">Culture and Economy</option>
<option value="Economic and Business Education">Economic and Business Education</option>
</select></td>
</tr>
<tr>
<td>Matriculated since:</td>
<td><select name="immat_monat" id="immat_monat">
<option value="">- please choose -</option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<label>
<input name="immat_jahr" type="text" id="immat_jahr" size="6" maxlength="4" />
</label>
(month-yyyy)</td>
</tr>
<tr>
<td>Expected date of Degree:</td>
<td><select name="end_monat" id="end_monat">
<option value="">- please choose -</option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<label>
<input name="end_jahr" type="text" id="end_jahr" size="6" maxlength="4" />
</label>
(month-yyyy)</td>
</tr>
<tr>
<td>Degree:</td>
<td><select name="abschluss" id="abschluss">
<option value="">- please choose -</option>
<option value="Bachelor of Science">Bachelor of Science</option>
<option value="Master of Science">Master of Science</option>
<option value="Diplom">Diplom</option>
</select></td>
</tr>
<tr>
<td>Letter of Appraisal for:</td>
<td><select name="gutachten_typ" id="gutachten_typ" onchange="update()">
<option value="">- please choose -</option>
<option value="Partneruniversität">Partneruniversität</option>
<option value="Freemover">Freemover</option>
<option value="Stipendium">Stipendium</option>
<option value="Masterstudiengang">Masterstudiengang</option>
</select></td>
</tr>
<tr>
<td>Name of University / Foundation:</td>
<td><label>
<input name="uni_name" type="text" id="uni_name"/>
</label></td>
</tr>
<tr>
<td>Address of University / Foundation<br />
Street:</td>
<td><br /><label>
<input name="uni_strasse" type="text" id="uni_strasse"/>
</label></td>
</tr>
<tr>
<td>Zipcode:</td>
<td><label>
<input name="uni_plz" type="text" id="uni_plz"/>
</label></td>
</tr>
<tr>
<td>City:</td>
<td><label>
<input name="uni_stadt" type="text" id="uni_stadt"/>
</label></td>
</tr>
<tr>
<td>Country:</td>
<td><label>
<input name="uni_land" type="text" id="uni_land"/>
</label></td>
</tr>
<tr>
<td>Number of required letters of appraisal:</td>
<td><select name="anzahl" id="anzahl">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select></td>
</tr>
<tr>
<td>Comments:</td>
<td><textarea name="comments" id="comments" cols="40" rows="10"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="ip" id="ip" type="hidden" value="<?php echo $ip; ?>" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><label>
<input type="submit" name="send" id="send" value="Request Letter of Appraisal" />
</label></td>
</tr>
</table>
</form>
</body>
</html>



Ich wäre euch echt für jede Hilfe dankbar!

elrond
18-11-2009, 10:25
hai,

mein tipp, um die der Lösung des Problems zu nähern ist folgender:

erweitere den Part om Debuginfos (im einfachsten Fall echo $db_abfrage_1) und sieh Dir das SQL-Statement an. Wenn da ordentliche Daten drin stehen, versuch's in den DB auszuführen. Wenn nicht, bleiben Deine Daten irgendwo auf der Strecke.

Noch ein Tipp zum Style: Es ist immer falsch abschnitte wie


$uni_country = $_POST['uni_land'];
$uni_country = str_replace("<","",$uni_country);
$uni_country = str_replace("\'","",$uni_country);
$uni_country = str_replace("'","",$uni_country);
$uni_country = str_replace("ä","ae",$uni_country);
$uni_country = str_replace("ö","oe",$uni_country);
$uni_country = str_replace("ü","ue",$uni_country);
$uni_country = str_replace("ß","ss",$uni_country);
$uni_country = str_replace("Ä","Ae",$uni_country);
$uni_country = str_replace("Ö","Oe",$uni_country);
$uni_country = str_replace("Ü","Ue",$uni_country);


zu kopieren. besser ist:



$uni_country = ersetzteSonderzeichen($_POST['uni_land']);

function ersetzteSonderzeichen($string) {

$string = str_replace("<","",$string);
$string = str_replace("\'","",$string);
$string = str_replace("'","",$string);
$string = str_replace("ä","ae",$string);
$string = str_replace("ö","oe",$string);
$string = str_replace("ü","ue",$string);
$string = str_replace("ß","ss",$string);
$string = str_replace("Ä","Ae",$string);
$string = str_replace("Ö","Oe",$string);
$string = str_replace("Ü","Ue",$string);
return $string;
}


Außerdem solltest Du über die inhaltliche Prüfung nachdenken, ansonsten hat Du sehr bald Mist in Deiner DB zu stehen.

btw: ich wollte nicht meckern, sondern helfen ;)

#cousin#
18-11-2009, 13:34
danke für die Antwort. Inzwischen habe ich den Fehler gefunden, er lag in der DB-Tabelle. Ich habe vergessen die id automatisch erhöhen zu lassen.