2015-11-03 134 views
-1

我的数据库结构看起来像这样; enter image description here从PHP保存到MySQL表格

我的代码看起来像这样;

<?php 
    $email_sql = mysqli_connect('localhost', 'root', 'root', 'myremovalsquote'); 

    // Check connection 
    if ($email_sql->connect_error) { 
    die("Connection failed: " . $email_sql->connect_error); 
} else { echo 'connected'; } 
?> 
<?php get_header(); ?> 
<section> 
    <div class="container"> 
     <div class="row"> 
      <div class="col-md-12"> 
       <article> 
        <h1>Get Your FREE Quotes Now!</h1> 
        <div class="post"> 
         <form method="post" action="<?php bloginfo('url') ?>/quote"> 
          <ul id="quoteform"> 
           <li class="row"> 
            <div class="col-md-12"> 
             <h2>Where are you moving from?</h2> 
             <select name="frompc"> 
              <option value="ab">Aberdeen (AB)</option> 
              <option value="al">St Albans (AL)</option> 
             </select> 

             <p>What type of property are you moving from?</p> 
             <select name="fromtype"> 
              <option value="storage">Storage</option> 
              <option value="bungalow">Bungalow</option> 
              <option selected value="house">House</option> 
              <option value="multistory">Multi-Story</option> 
             </select> 
            </div> 
           </li> 

           <li class="row"> 
            <div class="col-md-12"> 
             <h2>Move details</h2> 
             <input type="text" name="movedate" placeholder="DD/MM/YYYY"> 

             <p>Is this date flexible? 
             <select name="flexible"> 
              <option value="yes">Yes</option> 
              <option value="no">No</option> 
             </select></p> 

            </div> 
           </li> 

           <li class="row"> 
            <div class="col-md-12"> 
             <h2>Where are you moving to?</h2> 
             <select name="topc"> 
              <option value="ab">Aberdeen (AB)</option> 
              <option value="al">St Albans (AL)</option> 
             </select> 

             <select name="totype"> 
              <option value="storage">Storage</option> 
              <option value="bungalow">Bungalow</option> 
              <option selected value="house">House</option> 
              <option value="multistory">Multi-Story</option> 
             </select> 
            </div> 
           </li> 

           <li class="row"> 
            <div class="col-md-12"> 
             <h2>Extra Services</h2> 
             <input type="checkbox" name="packaging" value="Packaging"> 
             <input type="checkbox" name="packing" value="Packing"> 
             <input type="checkbox" name="furniture" value="Dismantle &amp; Reassemble Furniture"> 
            </div> 
           </li> 

           <li class="row"> 
            <div class="col-md-12"> 
             <h2>Notes/Additions</h2> 
             <textarea name="notes">Enter text here...</textarea> 
            </div> 
           </li> 

           <li class="row"> 
            <div class="col-md-12"> 
             <h2>Your Details</h2> 
             Full Name 
             <input type="text" name="fullname" value="Joe Bloggs"> 
             Contact Number 
             <input type="text" name="contactnumber"> 
             Email Address 
             <input type="email" name="email"> 
            </div> 
           </li> 

           <li class="row"> 
            <div class="col-md-12"> 
             <input type="submit" name="submit" required="required" value="Get Quotes!" id="emailbutton"> 

             <?php 
             //CREATE VARIBLES 
             if(isset($_POST["submit"])) { 

              $submitdate = date("d/m/Y", strtotime($oldDate)); 

              $frompostcode = $_POST["frompc"]; 
              $fromtype = $_POST["fromtype"]; 

              $movedate = $_POST["movedate"]; 
              $flexible = $_POST["flexible"]; 

              $topostcode = $_POST["topc"]; 
              $totype = $_POST["totype"]; 

              $packing = $_POST["packing"]; 
              $packaging = $_POST["packaging"]; 
              $furniture = $_POST["furniture"]; 

              $notes = $_POST["notes"]; 

              $name = $_POST["fullname"]; 
              $contactnumber = $_POST["contactnumber"]; 
              $email = $_POST["email"]; 

             } 
             ?> 

             <?php 
             //IMPORT TO DATABASE 
             if (!empty($_POST['email'])) { 

              //CLEANSE VARIBLES 
              date_default_timezone_set("Europe/London"); 
              $submitdate = date('d/m/y h:i a', time()); 



              //IMPORT 
              $query = " INSERT INTO `quotes` 
               ('id', 'date', 'fullname', 'email', 'contactnumber', 'movedate', 'flexible', 'fromtype', 'frompostcode', 'totype', 'topostcode', 'notes') 

               VALUES 

               (NULL, '".$submitdate."', '".$name."', '".$email."', '".$contactnumber."', '".$movedate."', '".$flexible."', '".$fromtype."', '".$frompostcode."', '".$totype."', '".$topostcode."', '".$notes."'); "; 

              mysqli_query($query); 

              echo 'Thankyou <br />'; 

              echo mysqli_error(); 

              echo $query; 

              mysqli_close($email_sql); 
             } 
             ?> 
            </div> 
           </li> 
          </ul> 
         </form> 
        </div> 
       </article> 
      </div> 
     </div> 
    </div> 
</section> 
<?php get_footer(); ?> 

我不能为我的生命得到的形式保存到数据库中,我学习PHP/SQL,我不能看到我错了,我知道,我需要使用的mysqli在mysql的地方。

+0

你有什么错误吗?但是,如果您正在学习,您可以关注网络上的很多教程。 –

+0

我没有得到任何的错误,这就是问题所在。当我检查数据库时没有输入任何内容。 –

+0

@BradHouston使用try catch块获取错误。 –

回答

2

就我个人而言,我会使用PDO。如果您选择使用PDO,则需要确保在您的php.ini文件中启用了该选项。 PHP的大多数默认安装已经安装了PDO,但可能未启用。找到该行:

;extension=php_pdo_mysql.dll 

并取消注释。所以,你现在应该有:

extension=php_pdo_mysql.dll 

然后你就可以开始与PDO解决这个问题:

//Place this section at the beginning of your `.php` file. 
//Connect to your DB 
$config = array(
    'host' => 'localhost' 
    'username' => 'root' 
    'password' => 'root' 
    'dbname' => 'myremovalsquote' 
); 
$db = new PDO('mysql:host='.$config['host'].';dbname='.$config['dbname'],$config['username'],$config['password']); 
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

//Place the following after your line: 
//if (!empty($_POST['email'])) { 

date_default_timezone_set("Europe/London"); 
$submitdate = date('d/m/y h:i a', time()); 
$frompostcode = htmlentities($frompostcode); 
$fromtype = htmlentities($fromtype); 
$movedate = htmlentities($movedate); 
$flexible = htmlentities($flexible); 
$topostcode = htmlentities($topostcode); 
$totype = htmlentities($totype); 
$packing = htmlentities($packing); 
$packaging = htmlentities($packaging); 
$furniture = htmlentities($furniture); 
$notes = htmlentities($notes); 
$name = htmlentities($name); 
$contactnumber = htmlentities($contactnumber); 
$email = htmlentities($email); 

//Create your query 
//You don't need to have the 'id' in your query because it's an auto increment field in your DB. You prepare the statement to avoid SQL injection 
$query = $db->prepare("INSERT INTO quotes 
(date, fullname, email, contactnumber, movedate, flexible, fromtype, frompostcode, totype, topostcode, notes) 
VALUES 
(:submitdate, :name, :email, :contactnumber, :movedate, :flexible, :fromtype, :frompostcode, :totype, :topostcode, :notes)"); 

//Bind the variables to the query and run 
$query_params = array(
    ':submitdate' => $submitdate, 
    ':name' => $name, 
    ':email' => $email, 
    ':contactnumber' => $contactnumber, 
    ':movedate' => $movedate, 
    ':flexible' => $flexible, 
    ':fromtype' => $fromtype, 
    ':frompostcode' => $frompostcode, 
    ':totype' => $totype, 
    ':topostcode' => $topostcode, 
    ':notes' => $notes 
); 
$query->execute($query_params); 

//Close connection 
$db = null; 
+1

直到我破解PHP的基础知识之前,我并不是真的想惹PDO。 –

+0

我可以理解,虽然PDO可能需要一些额外的时间来学习,但对我来说,语法看起来更清晰。我从来没有学过'mysql_'或'mysqli_'方法。我直接开始使用PDO。所以,显然我有偏见。 – Michael

0

经过大量的玩弄和研究,我发现的代码是正确的,对于最终因故部分执行查询失败的数据库使用;

mysqli_query($query); 

mysql_query($query); 

,但工作得很好,当我用下面的;

$connect->query($query); 
+0

您可以使用更新的代码打开SQL注入。你真正的逃跑发生了什么?应该首先使用预先准备好的陈述。如果因某种原因无法使用这些内容,请使用转义。你不应该只是将用户输入传递给你的查询。 – chris85