1

这是我的表格

<form method="POST" action="includes/add.php" dir="rtl" enctype="multipart/form-data">
    <br>
           حدد القسم :  <select name="section">
    <?php
        $query = "SELECT * FROM `sections`";
            $result = mysql_query($query);
           while($row=mysql_fetch_array($result, MYSQL_ASSOC)){                                                 
               echo "<option value='".$row['id']."'>".$row['sectionName']."</option>";
           }
   ?>
    </select><br>
     عنوان الموضوع :<input type="text" name="title" class="mem-information"/><br>
    الموضوع : <br /><textarea name="subject" rows="10" cols="50" class="mem-information" style="width: 500px"></textarea><br /><br>
الصورة :<input type="file" name="image"><br>
    <input type="submit" value="إرسال" name="send" class="log" style="color: black">
</form>

我的 add.php (将表单的内容添加到数据库)是

<?php
    session_start();
    include('../../includes/connect.php');

    $sectionID = $POST["section"];

    $title = $_POST['title'];
    $subject = $_POST['subject'];
    $visiable = 1;
    $imageName = mysql_real_escape_string($_FILES["image"]["name"]);
    $imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
    $imageType = mysql_real_escape_string($_FILES["image"]["type"]);

    $query = "insert into news (title, subject, visiable, image, section_id) values ('$title','$subject', '$visiable', '$imageData', '$sectionID')"; 
    $result = mysql_query($query);
    $id = mysql_insert_id();

    $data = array(
            'id' => $id
            );
    $base = '../../show.php';
    $url = $base. '?' . http_build_query($data);
    header("Location: $url");
    exit();
?>

如何获取所选项目 ob dropbox(combobox) 的值并将值添加到数据库?,对不起我的英语不好

4

2 回答 2

3

您的代码在获取下拉值时出错。

你有代码:

$sectionID = $POST["section"];

这应该是:

$sectionID = $_POST["section"];

这将给出所选项目的值。

于 2013-07-30T08:35:24.287 回答
0

嘿,您在检索下拉列表框值时犯了一个错误。而不是$POST[...]使用$_POST[...]

这是修改后的PHP代码

PHP代码:

<?php
session_start();
include('../../includes/connect.php');

$sectionID = $_POST["section"];

$title = $_POST['title'];
$subject = $_POST['subject'];
$visiable = 1;
$imageName = mysql_real_escape_string($_FILES["image"]["name"]);
$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
$imageType = mysql_real_escape_string($_FILES["image"]["type"]);

$query = "insert into news (title, subject, visiable, image, section_id) values ('$title','$subject', '$visiable', '$imageData', '$sectionID')"; 
$result = mysql_query($query);
$id = mysql_insert_id();

$data = array(
        'id' => $id
        );
$base = '../../show.php';
$url = $base. '?' . http_build_query($data);
header("Location: $url");
exit();
?>

当您像这样调用时,可以访问选项标签内的Value 设置为value属性$_POST["section"]

快乐编码:)

于 2013-07-30T08:40:05.820 回答