PHP - 将空白插入MySQL

时间:2014-05-02 04:05:25

标签: php mysql sql

我点击了这里的每个链接,我还没有发现任何与我的问题相符的内容。请记住,这是针对学校项目的,它必须与我为"count"编写的方式类似。

我希望这是一个由编码太长引起的简单语法错误。如果我格式错误,请原谅我。

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(AUTO-INCREMENT PRIMARY)
  2. 电子邮件
  3. 尺寸
  4. 我已经从我的另一个.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.-_]+@[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> 
    

3 个答案:

答案 0 :(得分: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.-_]+@[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

答案 1 :(得分: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.-_]+@[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注入也是一种很好的做法。

答案 2 :(得分:0)

在你的html代码中,你没有将电子邮件和大小变量放在表单元素中,这就是为什么,$ _POST [&#39; email&#39;]和$ _POST [&# 39;尺寸&#39;]是空白的。

你应该将这些元素移到里面:

<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>