更新并插入类别和主持人

时间:2014-07-01 21:16:27

标签: php mysql

我有文件category.php,我有:

Category name / Moderator
Economy / uername1
Math / username2
Biology / username1

我可以更新每个类别,并使用可以调节某个类别的用户名添加新类别。

我对categorie.php有问题,因为当我点击标签上的更新时输入中的类别名称写我经济和用户名1当我点击更新时输入的其他类别如数学输入再次写入经济和用户名1。 我遇到的第二个问题是当我想要与主持人添加新类别时。点击提交后,不会在我的mysql数据库名称中插入主持人添加的类别。

 <?PHP
    session_start();
    if(!isset($_SESSION["type_id"])){
        header("Location:index.php");
        exit();
    }
    else if($_SESSION["type_id"]!=0)
    {
        header("Location:index.php");
        exit();
    }

include_once("meni.php"); ?>

<div class="mid-right"><?php
$dbc=mysql_connect("localhost","2013","013");

if (!$dbc)
{
echo 'Error!'.mysql_error();

exit();}

$db=mysql_select_db("2013_db",$dbc);

    $category = $category_id = $name = $user_id = "";
    $id = 0;

    if(isset($_POST['username'])) {
        if (isset($_POST['type_id'])) {
            $type_id = $_POST['type_id'];
        } else  {
            $type_id = 2;
        }

        $id = $_POST['new'];

        if ($id == 0) {

            $name = $_POST['name'];
            $user_id = $_POST['user_id'];
            $category_id = $_POST['category_id'];


            $sql = "INSERT INTO category (category_id, name, user_id) VALUES ($category_id, '$name', '$user_id');";
        } 

        $result=mysql_query($sql);
        mysql_close($db);

    header("Location: category.php");
    }

    if(isset($_POST['category_id']) && isset($_POST['name']) && isset($_POST['moderator']) && $_SESSION['type_id'] == 0) {          
            $name = $_POST['name'];
            $user_id = $_POST['moderator'];
            $category_id = $_POST['category_id'];                   

            $sql = "UPDATE category SET name = '$name', user_id = $user_id  WHERE category_id = $category_id";

        $result=mysql_query($sql);
            mysql_close($db);

        header("Location: category.php");       
    }   

    if(isset($_GET['categories'])) {
        $kategorija_id = $_GET['categories'];
        if ($id==2) {
            $id = $_SESSION["category_id"];
        }
$dbc=mysql_connect("localhost","2013","2013");

if (!$dbc)
{
echo 'Error!'.mysql_error();

exit();}

$db=mysql_select_db("2013_db",$dbc);

        $sql = "SELECT k.category_id, k.name, ko.username FROM category k, user ko WHERE k.user_id = ko.user_id AND ko.type_id = 1";
        $result=mysql_query($sql);
        list($category_id, $name, $user_id) = mysql_fetch_array($result);
    } else {
        $name = "";
    }
    ?>
    <form method="POST" action="categorie.php"> 
    <div>
            <input type="hidden" name="category_id" value="<?php echo $category_id ?>"/>
            <input type="hidden" name="new" value="<?php echo $id?>"/>
    <table>
                <tr>
                    <td><label for="name">Category name:</label></td>
                    <td><input type="text" name="name" id="name" value="<?php echo $name ?>"/></td>
                </tr>               
                    <tr>                    
                    <td><label for="moderator">Moderator:</label></td>
                        <td><select name="moderator">
                        <?php                       
                        $sql2 = "SELECT user_id, username FROM user WHERE type_id = 1 ";
                        $rs2 = mysql_query($sql2);
                    while(list($user_id, $username) = mysql_fetch_array($rs2)){

    ?>
                                    <option value="<?php echo $user_id ?>"><?php echo $username ?></option><?php } ?>
                        </select></td>
                <tr>
                <tr>
                    <td colspan="2"><input type="submit" value="Send" id="submit"/></td>
                </tr>
            </table>
            </div>
        </form>     
<?php
    mysql_close($dbc);
?>
</div>
</div><?php include("footer.php"); ?>
</body>
</html>

0 个答案:

没有答案