无法将查询插入数据库

时间:2019-07-17 12:16:10

标签: php mysql

我已将以下代码编写为将查询插入到数据库中。我已将此PHP代码放在同一页面以及port.php上。它也不起作用。任何想法或建议都非常感谢。

这是我的PHP查询,用于将数据插入数据库

Php代码:

<?php
//connect to DB
 print_r($_POST); 
ini_set('display_errors', 1); //<- here you can switch on and off the error reporting 0 / 1 - makes life easy ;) 
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$debug=1;
            $host = "localhost"; $username = "root"; $password = "mysqlr00tpa55";
     try {
          $myconnection = new PDO("mysql:host=$host;dbname=myDB", $username, $password);
          // set the PDO error mode to exception    
          $myconnection ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    if(isset($_POST['insert'])){
     $vorname = $_POST['vorname'];
     $nachname = $_POST['nacname'];
     $email = $_POST['email'];
     $telefon = $_POST['telefon'];
     $created = $_POST['created']; //geburtstag
     $plz = $_POST['plz'];
     $ort = $_POST['ort'];
     $strasse = $_POST['strasse'];
     $hnr = $_POST['hnr'];
     $adrZus = $_POST['adrZus'];

     $pdoQuery = "INSERT INTO `Port_Owner`(`vorname`, `nachname`, `email`, `telefon`, `created`, `plz`, `ort`, `strasse`, `hnr`, `adrZus`)
     VALUES (:vorname,:nachname,:email,:telefon,:created,:plz,:ort,:strasse,:hnr,:adrZus)";

     $pdoResult = $myconnection ->prepare($pdoQuery);

     $pdoExec =  $pdoResult->execute(array(
          ":vorname"=>$vorname,
                                           ":nachname"=>$nachname,
                                           ":email"=>$email,
                                           ":telefon"=>$telefon,
                                           ":created"=>$created,
                                           ":plz"=>$plz,
                                           ":ort"=>$ort,
                                           ":strasse"=>$strasse,
                                           ":hnr"=>$hnr, 
                                           ":adrZus"=>$adrZus));
     if($pdoExec)
     {
        echo 'Attempt to port a new user is successful!';
     }else{
        echo 'Attempt to port a new user is unsuccessful!';
     }
     }
?>

HTML代码:

<form action="port.php" method="post">

<br><br>
    <!--Beginn des Anrede-->
<div class="form1"><br><br>
<div class="names">
    <div class="anrede">
    <label for="Anrede">Anrede
        <select id=" select" name="anrede" >
        <option value="">--Please choose an option--</option>
        <option value="herr">Herr</option>
        <option value="frau">Frau</option>
        <option value="andere">Andere</option>
        </select>
    </label></div>
    <br><br>
<div><br>
    <label class="desc" id="title1" for="Field1">Vorname</label>
    <div><input id="vorname" name="vorname" type="text" class="field text fn" value="<?php echo $value['vorname']; ?>" size="8" tabindex="1" placeholder="Ihr Vorname"></div>
</div>
<div><br><br>
    <label class="desc" id="title1" for="Field1">Nachname</label>
    <div><input id="nachname" name="nachname" type="text" class="field text fn" value="<?php echo $value['nachname']; ?>" size="8" tabindex="1" placeholder="Ihr Nachname"></div>
</div>
<div><br>
    <label class="desc" id="title1" for="Field1">E-mail-Addresse</label>
    <div><input id="email" name="email" type="text" class="field text fn" value="<?php echo $value['email']; ?>" size="8" tabindex="1" placeholder="Deine Emailadresse"></div>
</div>
<div><br>
    <label class="desc" id="title1" for="Field1">Telefonnummer</label>
    <div><input id="telefon" name="telefon" type="number" class="field text fn" value="<?php echo $value['telefon']; ?>" size="8" tabindex="1" placeholder="Deine Telefonnummer"></div>
</div>
<div><br><br>
    <label class="desc" id="title3" for="Field3">Geburtstag</label>
    <div><input id="geburtstag" name="geburtstag" type="date" spellcheck="false" value="<?php echo $value['created']; ?>" maxlength="255" tabindex="3"></div>
</div>
<div><br><br>
    <label class="desc" id="title3" for="Field3">Plz</label>
    <div><input id="plz" name="plz" type="number" spellcheck="false" value="<?php echo $value['plz']; ?>" maxlength="255" tabindex="3" placeholder="Ihre Postleitzahl"></div><br><br>
   <label class="desc" id="title3" for="Field3">Ort</label>
    <div><input id="ort" name="ort" type="text" spellcheck="false" value="<?php echo $value['ort']; ?>" maxlength="255" tabindex="3" placeholder="Dein Platz"></div><br><br>
  </div>

<div>
    <label class="desc" id="title1" for="Field1">Stra&beta;e</label>
    <div><input id="strasse" name="strasse" type="text" class="field text fn" value="<?php echo $value['strasse']; ?>" size="8" tabindex="1" placeholder="Deine Stra&beta;e"></div>
</div>

 <div><br><br>
    <label class="desc" id="title3" for="Field3">Hausnummer</label>
    <div><input id="hnr" name="hnr" type="number" spellcheck="false" value="<?php echo $value['hnr']; ?>" maxlength="255" tabindex="3" placeholder="Ihre Hausnummer"></div></div><br><br>
     <div>
      <label class="desc" id="title3" for="Field3">Hausnummerzusatz</label>
      <div><input id="adrZus" name="adrZus" type="number" spellcheck="false" value="<?php echo $value['adrZus']; ?>" maxlength="255" tabindex="3" placeholder="Zus&#228;tzliche Adresse"></div></div>
<div class="sub"><br>
    <br>
     <input type="submit" name="submit" value="Suche">
     <input type="submit" name="insert" value="New port">
      <input type="reset" name="reset" value="Reset">
</div>
  </div>
</form>

当我插入vorname,nachname和其他字段时,它显示“此页面无效”。有人帮助我。预先感谢

1 个答案:

答案 0 :(得分:0)

表单中的name=属性与您尝试从$_POST数组中获取的属性不匹配。例如:

<input id="vorname" name="vorname" type="text" class="field text fn" value="<?php echo $value['vorname']; ?>" size="8" tabindex="1" placeholder="Ihr Vorname">

name="vorname"不匹配:

$vorname = $_POST['portO_firstN'];

其中标识符应与输入名称相同。相反,它应该是:

$vorname = $_POST['vorname'];

与输入名称匹配。 $_POST数组中的所有变量均未定义。要查看此错误,请在打开<?php标签error_reporting(E_ALL); ini_set('display_errors', 1);

后立即在文件顶部添加错误报告。