Alright so I have a form and i'm trying to get it to submit data into my database but it's not working... i'm not sure if it's my form that is causing the issue or if it's a problem with connecting to the database?
好吧,所以我有一个表格,我试图让它将数据提交到我的数据库,但它不起作用...我不确定是否是我的表格导致问题或如果它是一个问题连接到数据库?
My table names are correct, value fields, log-in info... but whenever I hit submit it is not inserting any data.
我的表名是正确的,值字段,登录信息...但每当我点击提交它没有插入任何数据。
Could it be my form that is having the issue?? Would you guys mind taking a look?
可能是我的表格有问题吗?你们介意看看吗?
<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>
Here's my connecting to the database part:
这是我连接到数据库部分:
<?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);
}
?>
Also, if I want to enter data from the checkboxes and drop down how would I go about doing that? Will it be the same as textfields?
此外,如果我想从复选框中输入数据并下拉,我将如何进行此操作?它会和textfields一样吗?
1 个解决方案
#1
5
This answer for your originally posted code before your edit and without marking it as an edit.
您在编辑之前对原始发布的代码的答案,而不将其标记为编辑。
This is the reason:
这就是原因:
You have no equal sign in name"submit"
for your submit button.
您的提交按钮没有名称“提交”的等号。
Change it to name="submit"
which your code's execution is based on your conditional statement:
将其更改为name =“submit”,您的代码的执行基于您的条件语句:
if (isset($_POST['submit']))
It took me a while to spot that one after staring at your code, wondering "why" it wasn't working.
在盯着你的代码后,我花了一段时间才发现那个,想知道“为什么”它不起作用。
The rest of your code does check out, however I must also state that your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements, they're much safer.
其余代码确实签出,但是我还必须声明您的当前代码对SQL注入是开放的。使用准备好的语句,或PDO和准备好的语句,它们更安全。
As for your checkboxes, you can base yourself on the following:
至于您的复选框,您可以基于以下内容:
$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());
}
For some added protection:
为了一些额外的保护:
$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."')";
or mysqli_
method:
或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);
#1
5
This answer for your originally posted code before your edit and without marking it as an edit.
您在编辑之前对原始发布的代码的答案,而不将其标记为编辑。
This is the reason:
这就是原因:
You have no equal sign in name"submit"
for your submit button.
您的提交按钮没有名称“提交”的等号。
Change it to name="submit"
which your code's execution is based on your conditional statement:
将其更改为name =“submit”,您的代码的执行基于您的条件语句:
if (isset($_POST['submit']))
It took me a while to spot that one after staring at your code, wondering "why" it wasn't working.
在盯着你的代码后,我花了一段时间才发现那个,想知道“为什么”它不起作用。
The rest of your code does check out, however I must also state that your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements, they're much safer.
其余代码确实签出,但是我还必须声明您的当前代码对SQL注入是开放的。使用准备好的语句,或PDO和准备好的语句,它们更安全。
As for your checkboxes, you can base yourself on the following:
至于您的复选框,您可以基于以下内容:
$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());
}
For some added protection:
为了一些额外的保护:
$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."')";
or mysqli_
method:
或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);