首页 文章

在html中为<option value =“...”>提供多个值,并使用PHP将值排序到MySQL中的相应列中

提问于
浏览
0

我对php和mysql的了解有限,我正在尝试学习它,同时从头开始创建一个非常简单的CMS,以结合我的PHPBB论坛 .

CMS功能齐全,但我只能创建1个级别的类别 . 我没有多级别的类别,任何帮助将不胜感激 .

我在类别中有以下列 - cID, Headers ,描述,级别,依赖 . 根据需要包括父类别的cID值,Level应该是父类别的级别值的1 .

这是我到目前为止没有运气 - 功能:

function  addCat($cName, $cDesc, $property) {
$property = "$cLevel, $cDepend";
$query = mysql_query("INSERT INTO categories VALUES(null,'$cName','$cDesc', '$cLevel', '$cDepend')") or die(mysql_error());
}

类别选择器的html:

<form action="doAddC.php" method="post">
<td><label for="">Category</label></td>
    <td><select name="'CatLevel''CatDepend'">
        <?php
            $result = mysql_query("SELECT * FROM categories");
            while($cat = mysql_fetch_assoc($result)){
        ?>
            <option value="'CatLevel':'<?php echo $cat['Level']; ?>','CatDepend':'<?php echo $cat['cID']; ?>'"><?php echo $cat['Title']; ?></option>
        <?php
            }
        ?>
    </select></td>

doAddC.php

if(isset($_POST['submit'])) {
if(isset($_POST['CatName'])) {
    if(isset($_POST['CatDesc'])) {
        addCat($_POST['CatName'], $_POST['CatDesc'], $_POST['CatLevel']['CatDepend']);
        header("Location: cats.php");
    } else {
        echo "Please Add the Category Description!";
        include('addCat.php');
    }
} else {
    echo "Please Set the Category Name!";
    include('addCat.php');
}
} else {
header("Location: addCat.php");
}

在最佳方案中,Level和Reduction在数据库中等于0 . 我甚至不知道在手册中找到这个 .

1 回答

  • 1

    问题已经解决了 . 那些可能正在努力解决类似问题的人的代码如下 .

    HTML快照:

    <form action="doAddC.php" method="post">
    <td><select name="CatDepend">
            <option value="<?php
            $depending = "0"; /* Tells that there is no master category */
            $level = "1"; /* Tells that this is the master category (Level 1) */
            $str = "$depending $level";
            echo $str ?>">None</option>
        <?php
            $result = mysql_query("SELECT * FROM categories");
            while($cat = mysql_fetch_assoc($result)){
        ?>
            <option value="<?php
            $depending = $cat['ID']; /* Tells that the ID of the master category is a variable for Depending */         
            $level = ($cat['Level'] + 1); /* Tells that the category level of a new category is +1 */
            $str = "$depending $level";
            echo $str ?>"><?php echo $cat['Title']; ?></option>
        <?php
            }
        ?>
    </select></td>
    

    doAddC.php的快照:

    if(isset($_POST['submit'])) {
        if(isset($_POST['CatName'])) {
            if(isset($_POST['CatDesc'])) {
                addCat($_POST['CatName'], $_POST['CatDesc'], $_POST['CatDepend']);
                header("Location: cats.php");
            } else {
                echo "Please Add the Category Description!";
                include('addCat.php');
            }
        } else {
            echo "Please Set the Category Name!";
            include('addCat.php');
        }
    } else {
        header("Location: addCat.php");
    }
    

    功能快照:

    function  addCat($cName, $cDesc, $cOrder) {
        $part = explode(" ",$cOrder); /* Using explode() break down the value of $str (html snapshot), where a space works as a separator */
        $query = mysql_query("INSERT INTO categories VALUES(null,'$cName','$cDesc', '$part[0]', '$part[1]')") or die(mysql_error());    /* $part[0] and $part[1] put the values into the database as needed /*
    }
    

    任务完成 :)

相关问题