2015-11-03 46 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