好吧所以我有一个表单,我试图让它将数据提交到我的数据库但是它不起作用...我不确定这是我的表单导致问题还是它是一个问题连接到数据库?
我的表名是正确的,值字段,登录信息......但每当我点击提交时,它都没有插入任何数据。
这可能是我的表格有问题吗?你们介意看看吗?
<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一样吗?
答案 0 :(得分:5)
编辑前originally posted code的答案,而不是将其标记为编辑。
这就是原因:
您的提交按钮name"submit"
没有等号。
将其更改为name="submit"
,您的代码的执行基于您的条件声明:
if (isset($_POST['submit']))
在盯着你的代码后,我花了一段时间发现那个,想知道&#34;为什么&#34;它没有工作。
您的其余代码会检出,但我还必须声明您的当前代码对SQL injection开放。使用prepared statements或PDO with prepared statements,他们更安全。
至于您的复选框,您可以基于以下内容:
$checkbox = $_POST['products'];
for($i=0;$i <sizeof($checkbox);$i++) {
$query="INSERT INTO your_table (col) VALUES('".$checkbox[$i]."')";
mysql_query($query,$con) or die(mysql_error());
}
为了一些额外的保护:
$firstname= stripslashes($_POST['firstname']);
$lastname= stripslashes($_POST['lastname']);
$email = stripslashes($_POST['email']);
$firstname= mysql_real_escape_string($_POST['firstname']);
$lastname= mysql_real_escape_string($_POST['lastname']);
$email = mysql_real_escape_string($_POST['email']);
$sql = "INSERT INTO Signup (Firstname,Lastname,Email)
VALUES('".$firstname."','".$lastname."','".$email."')";
或mysqli_
方法:
<?php
$con = mysqli_connect("myhost","myuser","mypassw","mybd")
or die("Error " . mysqli_error($con));
$firstname = stripslashes($_POST['firstname']);
$lastname = stripslashes($_POST['lastname']);
$email = stripslashes($_POST['email']);
$firstname = mysqli_real_escape_string($con,$_POST['firstname']);
$lastname = mysqli_real_escape_string($con,$_POST['lastname']);
$email = mysqli_real_escape_string($con,$_POST['email']);
$sql = "INSERT INTO Signup (Firstname,Lastname,Email)
VALUES('".$firstname."','".$lastname."','".$email."')";
mysqli_query($con,$sql);
mysqli_close($con);