【发布时间】:2015-02-05 16:20:38
【问题描述】:
好的,所以我有一个表单,我正在尝试让它将数据提交到我的数据库中,但它无法正常工作...我不确定是我的表单导致问题还是它的问题连接数据库?
我的表名是正确的,值字段,登录信息...但是每当我点击提交时,它并没有插入任何数据。
可能是我的表单有问题吗?你们介意看看吗?
<form action="form.php" method="POST">
<div class="row">
<div class="large-4 columns">
<span id="spryfirstname">
<input name="firstname" type="text" class="text" placeholder="First Name"/>
<span class="textfieldRequiredMsg">A value is required.</span></span></div>
<div class="large-4 columns">
<span id="sprylastname">
<input name="lastname" type="text" class="text" placeholder="Last Name"/>
<span class="textfieldRequiredMsg">A value is required.</span></span></div>
<div class="large-4 columns">
<div class="row collapse">
<div class="small-9 columns"><span id="spryemail">
<input name="email" type="text" placeholder="email@example.com"/>
<span class="textfieldRequiredMsg">A value is required.</span></span></div>
</div>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>Check all Products that you're interested in</label>
<div>
<input name="products[]" type="checkbox" value="all">
ALL PRODUCTS/SERVICES
<input name="products[]" type="checkbox" vallue="trade">Trade-in
<input name="products[]" type="checkbox" value="layaway">Layaway products
<input name="products[]" type="checkbox" value="theatre">Home Theatre Systems
<input name="products[]" type="checkbox" value="TV">HD TVs
<input name="products[]" type="checkbox" value="Games">Video Game Consoles</label> <br>
<input name="products[]" type="checkbox" value="laptops"> Laptops</label>
<input name="products[]" type="checkbox" value="monitors"> Monitors</label>
<input name="products[]" type="checkbox" value="phones"> Phones</label>
<input name="products[]" type="checkbox" value="cameras"> Cameras</label>
<input name="products[]" type="checkbox" value="acoustic"> Acoustic Guitars</label>
<input name="products[]" type="checkbox" value="electric"> Electric Guitars</label>
<input name="products[]" type="checkbox" value="drums"> Drums</label>
<input name="products[]" type="checkbox" value="wind"> Wind Instruments</label> <br>
<input name="products[]" type="checkbox" value="pianos"> Pianos</label>
<input name="products[]" type="checkbox" value="violins"> Violins</label>
<input name="products[]" type="checkbox" value="diamonds"> Diamonds
<input name="products[]" type="checkbox" value="neck"> Necklaces
<input name="products[]" type="checkbox" value="rings"> Rings
<input name="products[]" type="checkbox" value="ear"> Ear Rings</label>
<input name="products[]" type="checkbox" value="gold"> Gold Jewelry
<input name="products[]" type="checkbox" value="silver"> Silver Jewelry
<hr>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>How often would you like to have product updates? <select>
<option value="daily" name"Updates">Daily</option>
<option value="weekly" name"Updates">Weekly</option>
<option value="monthly" name"Updates">Monthly</option>
</select>
</label>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>Tell us a little about yourself <textarea placeholder="Type here">
</textarea>
</label>
</div>
</div>
<div class="row">
<input class="button small large-3" type="submit" name"submit" />
</div>
</form>
这是我连接到数据库的部分:
<?php
if (isset($_POST['submit'])){
$con = mysql_connect("localhost","dxh6110","******");
if(!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("dxh6110",$con);
$sql = "INSERT INTO Signup (Firstname,Lastname,Email) VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[email]')";
mysql_query($sql,$con);
mysql_close($con);
}
?>
另外,如果我想从复选框中输入数据并下拉,我该怎么做呢?会和textfields一样吗?
【问题讨论】:
-
在你打开
<?php标签error_reporting(E_ALL); ini_set('display_errors', 1);之后将错误报告添加到文件顶部,看看它是否产生任何东西。还要将or die(mysql_error())添加到mysql_query()。 -
Please, don't use
mysql_*functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解prepared statements,并使用PDO 或MySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial. -
好的,所以基本上我有 mysql_ 我需要将它们更改为 mysqli_?
-
@DLH 之类的。
mysqli_需要所有功能的数据库连接,并且是第一个参数。请访问php.net/manual/en/book.mysqli.php 获取完整手册。