将下拉数据添加到我的数据库中

时间:2014-12-08 04:42:21

标签: php mysql database forms

好吧,所以一个接一个地解决一些问题,现在我想让我的下拉菜单数据提交。我不知道从哪一个开始。

这是我的表格..这里有什么不对吗? :

<form action="form.php" method="POST"> 

<div class="row"> 
<div class="large-4 columns"> 


<span id="spryfirstname">
<input name="firstname" type="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" 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>

         <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<br>
           <input name="products[]" type="checkbox" value="laptops"> Laptops
           <input name="products[]" type="checkbox" value="monitors"> Monitors
           <input name="products[]" type="checkbox" value="phones"> Phones
           <input name="products[]" type="checkbox" value="cameras"> Cameras
           <input name="products[]" type="checkbox" value="acoustic"> Acoustic Guitars
           <input name="products[]" type="checkbox" value="electric"> Electric Guitars
           <input name="products[]" type="checkbox" value="drums"> Drums
           <input name="products[]" type="checkbox" value="wind"> Wind Instruments <br>
           <input name="products[]" type="checkbox" value="pianos"> Pianos
           <input name="products[]" type="checkbox" value="violins"> Violins
           <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
           <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的连接:

<?php
if(isset($_POST['submit'])){
$con = mysqli_connect("localhost","dxh6110","tcqfoz7","dxh6110") 
   or die("Error " . mysqli_error($con)); 

$first = stripslashes($_POST['firstname']);
$last = stripslashes($_POST['lastname']);
$email = stripslashes($_POST['email']);
$checkbox = stripslashes($_POST['products']);

$first = mysqli_real_escape_string($con,$_POST['firstname']);
$last = mysqli_real_escape_string($con,$_POST['lastname']);
$email = mysqli_real_escape_string($con,$_POST['email']);
$checkbox = mysqli_real_escape_string($con,$_POST['products']);

$checkbox = implode(',', $_POST['products']);


$sql = "INSERT INTO Register (Firstname,Lastname,Email,Product)  
    VALUES('".$firstname."','".$lastname."','".$email."','".$checkbox."')";
}

mysqli_query($con,$sql);
mysqli_close($con);

?>

2 个答案:

答案 0 :(得分:2)

好吧,部分问题是,每次访问该页面时,都会添加另一行。无论是否提交,请添加以下内容:

if(isset($_POST['Submit'])){
}

围绕你去那里的所有php包围^。^ 所以继续删除它得到的所有空白行,然后再试一次。这样你可以更好地调试它。

因此,这解释了您遇到的空行问题。 ;)3没有经历过什么?

答案 1 :(得分:0)

Welp我已经解决了我的问题。谢谢大家的帮助。

基本上,当我第一次发布我的问题时,我的数据库中有3个字段中有1个被提交 造成这种情况的原因是因为我的变量和我这样的变量不一样而且我得到了双重条目...... 1个空白条目和另外3个条目中的1个:

$first = stripslashes($_POST['firstname']);
$last = stripslashes($_POST['lastname']);
$email = stripslashes($_POST['email']);


$first = mysqli_real_escape_string($con,$_POST['firstname']);
$last = mysqli_real_escape_string($con,$_POST['lastname']);
$email = mysqli_real_escape_string($con,$_POST['email']);








$sql = "INSERT INTO Register (First,Last,Email,Product,Updates)  
    VALUES('".$firstname."','".$lastname."','".$email."');
 }

$ first不等于$ firstname,$ last不等于$ lastname $ email确实等于$ email ....所以只有3个被提交给数据库。

在这个问题之后,我不知道如何添加复选框和下拉数据......这就是我所做的改变它以及双重条目(1个空白),感谢Nate Nevins。

if(isset($_POST['submit'])){
$con = mysqli_connect("localhost","dxh6110","tcqfoz7","dxh6110") 
   or die("Error " . mysqli_error($con)); 


$first = stripslashes($_POST['firstname']);
$last = stripslashes($_POST['lastname']);
$email = stripslashes($_POST['email']);
$checkbox = stripslashes($_POST['products']);
$update = stripslashes($_POST['updates']);

$first = mysqli_real_escape_string($con,$_POST['firstname']);
$last = mysqli_real_escape_string($con,$_POST['lastname']);
$email = mysqli_real_escape_string($con,$_POST['email']);
$checkbox = mysqli_real_escape_string($con,$_POST['products']);
$checkbox = mysqli_real_escape_string($con,$_POST['updates']);

$checkbox = implode(',', $_POST['products']);






$sql = "INSERT INTO Register (First,Last,Email,Product,Updates)  
    VALUES('".$first."','".$last."','".$email."','".$checkbox."','".$update."')";
  }

mysqli_query($con,$sql);
mysqli_close($con);

?>