我已经点击了这里的每个链接,我还没有找到任何匹配我的问题。请记住,这是学校的一个项目,为了"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个属性:
- id (auto-increment primary)
- 大小
我复制/粘贴了相同的代码格式从我的另一个.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>
©2014, XXXX
</p>
</div>
</body>
</html>
我猜你没有把你的输入放在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语句。这是一个很好的实践,甚至可以防止SQL注入。
在你的html代码中,你没有把email和size变量放在form元素中,这就是为什么$_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>
这意味着输入没有张贴输入的值。原因是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