PHP如何将select box值插入数据库

时间:2014-02-14 06:26:36

标签: php mysql

我有一个问题 我已经创建了一个表单来查看带有数据库和一个文本字段值的两个选择框值 我想知道如何将这些值插回到具有不同表的数据库

我的脚本视图选择框值是这样的

<?php
$query = "SELECT gejala FROM gejala where idatribut =110000";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>
<select name="gejala1">
<?php 
while ($row = mysql_fetch_array($result)) {
    echo "<option value='".$row['gejala']."'>".$row['gejala']."</option>";
}
?>  
</select>


<p>Subatribut2
<?php
$query = "SELECT gejala FROM gejala where idatribut =110000";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>
<select name="gejala2">
<?php 
while ($row = mysql_fetch_array($result)) {
    echo "<option value='".$row['gejala']."'>".$row['gejala']."</option>";
}
?>
</select>          


<p>
    <label>value
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;          
      <input type="text" name="textfield">
    </label>  
<p>
<input name="submit" type="button" value="submit">    
<p>

如何知道如何将此值插入到具有相同字段的另一个表(pucuk)中 三江源

我已经尝试插入脚本

<?php
if(isset($_POST['gejala1'])) {
    $gejala1 = $_POST['gejala1'];
        $sql = "INSERT INTO pucuk (gejala1) VALUES ({$gejala2})";
    $dbLink = mysql_connect('localhost', 'root', 'root') or die(mysql_error());
              mysql_select_db('cbrteh', $dbLink) or die(mysql_errno());

    $result = mysql_query($sql);
    if($result) {
        echo "Record successfully inserted!";
    }
    else {
        echo "Record not inserted! (". mysql_error() .")";
    }
}
if(isset($_POST['gejala2'])) {
    $gejala2 = $_POST['gejala2'];
        $sql = "INSERT INTO pucuk (gejala2) VALUES ({$gejala2})";
    $dbLink = mysql_connect('localhost', 'root', 'root') or die(mysql_error());
              mysql_select_db('cbrteh', $dbLink) or die(mysql_errno());

    $result = mysql_query($sql);
    if($result) {
        echo "Record successfully inserted!";
    }
    else {
        echo "Record not inserted! (". mysql_error() .")";
    }
}
}   
?>
  </form>

但是当我点击提交按钮时它仍然没有效果

4 个答案:

答案 0 :(得分:0)

在while循环中你可以编写插入查询并将选项框值发送到你想要的字段中,试试吧!

答案 1 :(得分:0)

提交后,您的选择选项值将位于您选择名称下的$ _POST []变量中。即,这里是“gejala2”

答案 2 :(得分:0)

将您的代码放在一个带有method = POST的表单中,在提交表单后,您将获得_POST数组中的所有新值 例如

if(isset($_POST['submit']))
{
$var1=$_POST['select1'];
$var2=$_POST['select2'];
$var3=$_POST['select3'];
//then write update/insert query as your requirement
}

答案 3 :(得分:0)

试试这个

<?php
if(isset($_POST['gejala1'])) {
    $gejala1 = $_POST['gejala1'];
        $sql = "INSERT INTO pucuk (gejala1) VALUES ({$gejala2})";
    $dbLink = mysql_connect('localhost', 'root', 'root') or die(mysql_error());
              mysql_select_db('cbrteh', $dbLink) or die(mysql_errno());

    $result = mysql_query($sql);
    if($result) {
        echo "Record successfully inserted!";
    }
    else {
        echo "Record not inserted! (". mysql_error() .")";
    }
}
if(isset($_POST['gejala2'])) {
    $gejala2 = $_POST['gejala2'];
        $sql = "INSERT INTO pucuk (gejala2) VALUES ({$gejala2})";
    $dbLink = mysql_connect('localhost', 'root', 'root') or die(mysql_error());
              mysql_select_db('cbrteh', $dbLink) or die(mysql_errno());

    $result = mysql_query($sql);
    if($result) {
        echo "Record successfully inserted!";
    }
    else {
        echo "Record not inserted! (". mysql_error() .")";
    }
}

?>
<form action="" method="POST"><!-- add this -->

<?php
$query = "SELECT gejala FROM gejala where idatribut =110000";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>
<select name="gejala1">
<?php 
while ($row = mysql_fetch_array($result)) {
    echo "<option value='".$row['gejala']."'>".$row['gejala']."</option>";
}
?>  
</select>


<p>Subatribut2
<?php
$query = "SELECT gejala FROM gejala where idatribut =110000";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>
<select name="gejala2">
<?php 
while ($row = mysql_fetch_array($result)) {
    echo "<option value='".$row['gejala']."'>".$row['gejala']."</option>";
}
?>
</select>          


<p>
    <label>value
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;          
      <input type="text" name="textfield">
    </label>  
<p>
<input name="submit" type="submit" value="submit">    <!-- changed from type="button" to type="submit"> -->
<p>
</form>