PDO MySQL连接失败

时间:2017-09-15 04:10:09

标签: php html mysqli

A link to my previous thread

这是我的PHP代码

<?php
$dbh = new PDO("mysql:host=$host;dbame=$dbname",$user,$pass);
$UserN = mysqli_real_escape_string($con, $_POST['UserN']);
$FullN = mysqli_real_escape_string($con, $_POST['FullN']);
$Adrs = mysqli_real_escape_string($con, $_POST['Adrs']);
$Email = mysqli_real_escape_string($con, $_POST['Email']);
$PhoneN = mysqli_real_escape_string($con, $_POST['PhoneN']);
$stmt = $dbh->prepare("INSERT INTO UserIn (UserN, FullN, Adrs, Email, 
PhoneN) VALUES ('$UserN','$FullN','$Adrs','$Email','$PhoneN')"); //Insert 
query $stmt->execute($UserN, $FullN, $Adrs, $Email, $PhoneN);



?>

这是我的表格代码

 <div class="form-con">
         <form action="insert.php" method="POST">
             <label>Username</label><br>
             <input type="text" name="UserN" placeholder="Your Username" ><br>
             <label>Full Name</label><br>
             <input type="text" name="FullN" placeholder="Full Name"><br>
             <label>Full Address</label><br>
             <textarea type="text" rows="4" cols="50" name="Adrs" placeholder="Address"></textarea><br> 
                  <label>Email Address</label><br>
                  <input type="text" name="Email" placeholder="Email Address"><br>
                  <label>Phone Number</label><br>
                  <input type="text" name="PhoneN" placeholder="Phone Number"><br>
                   <div class="btn">
                     <a href="#"><button type="submit">Submit</button></a>
                   </div>
         </form>
      </div>

点击提交按钮后,我提供了一个,它为我提供了此页面无效

xxxxxxxx.com目前无法处理此请求。 HTTP ERROR 500

1 个答案:

答案 0 :(得分:0)

首先尝试重命名你的&#34; .htaccess&#34;到&#34; htaccess的测试&#34;然后尝试提交,或者对文件使用CHMOD 644,对文件夹(目录)使用CHMOD 755,如果继续,请尝试:

mysqli_real_escape_string与Pdo完美配合,当您使用时 &#34; pdo准备执行ou查询准备&#34; ,mysqli_real_escape_string并不重要!!!

使用此代码:

<?php
$dbh = new PDO("mysql:host=$host;dbame=$dbname",$user,$pass);
extract($_POST);
$UserN =strip_tags($_POST['UserN']);
$FullN = strip_tags($_POST['FullN']);
$Adrs = strip_tags($_POST['Adrs']);
$Email = strip_tags($_POST['Email']);
$PhoneN = strip_tags($_POST['PhoneN']);
$stmt = $dbh->prepare('INSERT INTO UserIn (UserN, FullN, Adrs, Email, PhoneN) VALUES(:UserN, :FullN, :Adrs, :Email, :PhoneN)');
$stmt->execute(array(
             'UserN' => $UserN,
             'FullN' => $FullN,
             'Adrs' => $Adrs,
             'Email' => $Email,
             'PhoneN' => $PhoneN,  
));

?>