2014-05-02 76 views
0

我点击了这里的每一个链接,我还没有找到任何符合我的问题的东西。请记住,这是一个学校项目,它必须保持类似于我已经写入"count"PHP - 在MySQL中插入空白

我希望这是一个简单的语法错误造成的编码太长。如果我把这个错误格式化,请原谅我。

PHP代码:

<?php 
// Connect to LOCAL or SERVER MySQL Database. Just change between local and server 
    include('connect/local-connect.php'); 

// initialize and populate PHP variables from user-entered data 

    $email = $_POST['email']; 
    $size = $_POST['size']; 

//Build a MySQL statement to populate the database table 

$query = "INSERT INTO shopping (email, size) 
VALUES ('$email', '$size')"; 

// Run the query we just built 
$result = mysqli_query($dbc,$query) or die('Unable to write to database'); 

// Close the database connection 
mysqli_close($dbc); 

?> 

我的表名为"shopping",并有3个属性:

  1. ID(自动递增PRIMARY)
  2. 电子邮件
  3. 大小

我有复制/过去编辑这个相同的代码格式从我的另一个.php页面写入​​另一个数据库表就好了。我不明白为什么这不起作用。

我的HTML代码:

<!DOCTYPE html> 

<!-- 
project 
sneaker1.htm --> 

<html lang="en"> 

    <head> 
    <!-- Meta tag --> 
    <meta name="robots" content="noindex.nofollow" /> 
    <meta charset="utf-8" /> 

    <!-- Link tag for CSS --> 
    <link type="text/css" rel="stylesheet" href="../stylesheet/project.css" /> 

    <!-- Javascript tags --> 
    <script type="text/javascript" src="../js/messages.js"></script> 


    <!-- Web Page Title --> 
    <title>Shoe Source Unlimited - Sneaker Sale</title> 

    </head> 

    <body> 
    <div id="header"> 

       <img src="../images/logo.png" alt="Logo" /> 

     <p class="sh1">Shoe Source Unlimited</p> 
     <p class="sh2">Your source for lightning sales of this season's hot shoes!</p> 

    <p class="sh3">XXXXXX</p> 
    </div> 

    <div id="navbar"> 
     <ul id="nav"> 
      <li> 
       <a href="../homepage.htm">Home</a>       
      </li> 

      <li> 
       <a href="#">Men's</a> 
        <ul> 
         <li><a href="../sneakers.htm">Sneakers</a></li> 
         <li><a href="../loafers.htm">Loafers</a></li> 
         <li><a href="../athletic.htm">Athletic</a></li> 
        </ul>      
      </li> 

      <li> 
       <a href="#">Women's</a> 
        <ul> 
         <li><a href="../boots.htm">Boots</a></li> 
         <li><a href="../heels.htm">Heels</a></li> 
         <li><a href="../sandals.htm">Sandals</a> </li>  
        </ul>    
      </li> 

      <li> 
       <a href="../about.htm">About Us</a>   

      </li> 

      <li> 
       <a href="../signup.htm">Sign Up</a> 

      </li> 

      <li> 
       <a href="../login.php">Log In</a> 

      </li> 

      </ul> 
     </div> 



    <div id="external"> 

<p> 

     <a href="https://twitter.com/XXXXXX" onclick="window.open(this.href); return false;"> 
      <img src="../images/twitter.jpg" alt="twitter" /> 
     </a> 
</p> 
     <p>Follow us on Twitter!</p> 
    <br/> 


<p> 

     <a href="http://www.facebook.com/XXXXXXXX" onclick="window.open(this.href); return false;"> 
      <img src="../images/facebook.png" alt="facebook" /> 
     </a> 
</p> 
     <p>Like us on Facebook!</p> 
    <br/> 


    <a href="../em/projectem.htm"> 
    <img src="../images/email.jpg" alt="pinkemail" /> 
    </a> 

     <p> Send us an email!</p> 
</div> 

<div id="about"> 

    <p class="abouttitle">Red Sneaker with Black Laces</p> 
    <p class="abouttitle"><img src="../images/shoes/sneaker1.jpg" alt="sneaker1" /></p> 
    <p class="description">This low-top sneaker provides you with a comfortable wear with a stylish flare. 
    That red will surely catch some eyes!</p> 

<p class="price">Price: $25.00 - tax included </p><br/><br/>  

    <!--Email --> 
      <p class="size"> 
      <label for="email">Email:</label> 
      <input type="email" id="email" name="email" required 
      title="Email: 6-59 characters, lowercase, valid email only!" 
      pattern="[a-z0-9.-_][email protected][a-z0-9-]+\.[a-z]{2,6}" 
      maxlength="60" 
      onfocus="emailmsg()" /> 
      </p> 

    <!-- Pick a size --> 

      <select name="size" id="size" required title="Select a size" > 
      <option value="">Select a size...</option> 
      <option value="sneaker1size1">Red and Black Mens 11</option> 
      <option value="sneaker1size2">Red and Black Mens 12</option> 
      </select> 




     <!-- change "get" to "post" and change to .php for database writing --> 
<form id="joinform" action="../purchaseconfirm.php" method="post"> 
     <p class="submit"> 
       <input type="submit" 
       value=" Buy Now! "/> 
      <br/><br/> 
     </p>   
    </div> 




<div id="footer"> 

    <p> 

     &copy;2014, XXXX 

    </p> 
</div> 

    </body> 

</html> 
+0

你的HTML被打破。您的输入字段必须包含在开始和结束'

'标签之间。您的''标签位于您的''字段之后。 –

+0

@MikeW,没错,你为什么不把它当作答案? :) –

回答

0

我想你没有把你的情况下输入HTML表单。

例如参见下面的代码:

<!-- change "get" to "post" and change to .php for database writing --> 
    <form id="joinform" action="../purchaseconfirm.php" method="post"> 
    <!--Email --> 
     <p class="size"> 
     <label for="email">Email:</label> 
     <input type="email" id="email" name="email" required 
     title="Email: 6-59 characters, lowercase, valid email only!" 
     pattern="[a-z0-9.-_][email protected][a-z0-9-]+\.[a-z]{2,6}" 
     maxlength="60" 
     onfocus="emailmsg()" /> 
     </p> 

<!-- Pick a size --> 

     <select name="size" id="size" required title="Select a size" > 
     <option value="">Select a size...</option> 
     <option value="sneaker1size1">Red and Black Mens 11</option> 
     <option value="sneaker1size2">Red and Black Mens 12</option> 
     </select> 




    <!-- change "get" to "post" and change to .php for database writing --> 
    <p class="submit"> 
      <input type="submit" 
      value=" Buy Now! "/> 
     <br/><br/> 
    </p>  
</form> 

而且,最好是使用参数化查询,把SQL查询。在PHP中,您可以使用prepare statement。甚至防止SQL注入也是一种很好的做法。

0

这意味着犯规发布输入值的输入。原因是joinform形式错位。换句话说,输入需要在表单标签内。目前形式标签已经开始接近submit按钮。

你能避免这些使用isset($_POST['email']) ...

应该是这样,

 <!-- change "get" to "post" and change to .php for database writing --> 
    <form id="joinform" action="../purchaseconfirm.php" method="post"> <--Form tag placment 
    <div id="about"> 

     <p class="abouttitle">Red Sneaker with Black Laces</p> 
     <p class="abouttitle"><img src="../images/shoes/sneaker1.jpg" alt="sneaker1" /></p> 
     <p class="description">This low-top sneaker provides you with a comfortable wear with a stylish flare. 
     That red will surely catch some eyes!</p> 

    <p class="price">Price: $25.00 - tax included </p><br/><br/>  

     <!--Email --> 
      <p class="size"> 
      <label for="email">Email:</label> 
      <input type="email" id="email" name="email" required 
      title="Email: 6-59 characters, lowercase, valid email only!" 
      pattern="[a-z0-9.-_][email protected][a-z0-9-]+\.[a-z]{2,6}" 
      maxlength="60" 
      onfocus="emailmsg()" /> 
      </p> 

     <!-- Pick a size --> 

      <select name="size" id="size" required title="Select a size" > 
      <option value="">Select a size...</option> 
      <option value="sneaker1size1">Red and Black Mens 11</option> 
      <option value="sneaker1size2">Red and Black Mens 12</option> 
      </select> 
     <p class="submit"> 
      <input type="submit" 
      value=" Buy Now! "/> 
      <br/><br/> 
     </p>   
     </div> 
    </form> <--End tag missed 
+0

非常感谢你!我甚至没有意识到表单标签必须包含所有内容。谢谢!! – user3594938

0

在你的html代码中,你没有在表单元素中放置email和size变量,这就是为什么$ _POST ['email']和$ _POST ['size']是空白的。

你应该中移动这些元素:

<form id="joinform" action="../purchaseconfirm.php" method="post"> 
// add email and size element here 
    <p class="submit"> 
       <input type="submit" 
       value=" Buy Now! "/> 
      <br/><br/> 
     </p>   
</form>