2016-04-22 73 views
-1

值我有一个表项idnamecontentcategories id(外键表类别))和类别表idtitlePHP MySQL的获取的动态选择

  • name:文本类型
  • content:文本
  • categories_id:选择动态相关到t他分类表

插入项目表,工作得很好,但在修改。我对动态选择类别列表存在问题,而不是选择我选择添加文章的选项。

我如何获得select标签的值? <select> <option></option> </select>

<?php 
    include 'dbconnect.php'; 

    $id = $_GET['id']; 
    $sql = mysql_query("SELECT * FROM articles WHERE id ='".$id."'"); 
    $res = mysql_fetch_assoc($sql); 

    if (@$_REQUEST['do'] == "update") { 
     $m_id = $_POST['id']; 
     $nom = $_POST["nom"]; 
     $contenu = $_POST["contenu"]; 
     $categories_id = $_POST["categories_id"]; 

     $sql = mysql_query("UPDATE articles SET nom='$nom', contenu='$contenu', categories_id='$categories_id' WHERE id =' $m_id' "); 

     if($sql) 
      header("Location:listArticles.php"); 
     else 
      header("Location:updateArticle.php"); 
    } 
?> 
<html lang="en"> 
    <body class="nav-md"> 
     <?php if (isset($_GET['id']) && $_GET['id'] == $id) { ?> 
      <form action="" method="post" accept-charset="utf-8"> 
       <table> 
        <td>Nom: <input type ="text" name ="nom" value="<?php echo $res['nom'] ?>"></td> 
        </br> 
        <td>Contenu: <textarea name ="contenu"><?php echo $res['contenu'] ?></textarea></td> 
        </br> 
        <td> 
         Categories: 
         <select class="form-control" name="categories_id" value="<?php echo $res['categories_id'] ?>" > 
          <option></option> 
         </select> 
        </td> 
        <td> 
         <button type="submit" class="btn btn-success" name ="do" value="update">Modifier</button> 
        </td> 
        <input type="hidden" name="id" value="<?php echo $id; ?>"/> 
       </table> 
      </form> 
     <?php } ?> 
    </body> 
</html> 

这正是当前页面看起来像:

Page displaying in browser

+0

请重写第一段,您使用换行解释您的问题,并清楚解释预期行为和当前行为。就像现在这样,它是不可读的。 – Eloims

+0

在你的'?id ='里面你应该放上''或者(TRUNCATE TABLE文章) - ' – ash

回答

0

如果我没有理解你正确(并根据图片判断),你想显示类别并选择与文章相关的类别。

这是一个粗略的,未经测试的草图,你可以如何接近。请阅读我的评论。

<?php 
include 'dbconnect.php'; 

// assuming ID is integer, we'll use intval() 
$id = isset($_GET['id']) ? intval($_GET['id']) : null; 

// query article matching given ID 
$articleRes = mysql_query("SELECT * FROM articles WHERE id ='" . $id . "'"); 
$article = mysql_fetch_assoc($articleRes); 

// query categories 
$categoriesRes = mysql_query("SELECT * FROM categories"); 

// check if form has been submitted 
// if you are expecting POST, use $_POST not $_REQUEST 
// don't use @, it's sloppy 
if (!empty($_POST['do'])) { 
    $m_id = $_POST['id']; 
    $nom = $_POST["nom"]; 
    $contenu = $_POST["contenu"]; 
    $categories_id = $_POST["categories_id"]; 

    // update article with given ID 
    // is it nom or name? 
    $updateRes = mysql_query("UPDATE articles SET nom='$nom', contenu='$contenu', categories_id='$categories_id' WHERE id='$m_id'"); 
    if ($updateRes) { 
     header("Location: listArticles.php"); 
    } else { 
     header("Location: updateArticle.php"); 
    } 
    // good practice to die after you redirect 
    die(); 
} 
?> 


<html lang="en"> 
    <body class="nav-md"> 
    <?php if ($article) : ?> 
    <form action="" method="post" accept-charset="utf-8"> 
     <table> 
      <td>Nom: <input type="text" name="nom" value="<?php echo $article['nom'] ?>"></td> 
      <!-- you cannot have a BR tag in between TD tags --> 
      <!--/br--> 
      <td>Contenu: <textarea name="contenu"><?php echo $article['contenu'] ?></textarea></td> 
      <!-- you cannot have a BR tag in between TD tags --> 
      <!--/br--> 
      <td> 
       Categories: 
       <!-- SELECT tag does not have a VALUE attribute --> 
       <select class="form-control" name="categories_id"> 
        <!-- loop through the categories and build the OPTION tag --> 
        <!-- for each iteration, check if the category ID matches the article's category ID --> 
        <!-- if so, mark the option as selected --> 
        <?php while ($category = mysql_fetch_assoc($categoriesRes)) : ?> 
         <option <?php echo $category['id'] == $article['categories_id'] ? 'selected' : '' ?>><?php echo $category['title'] ?></option> 
        <?php endwhile ?> 
       </select> 
      </td> 
      <td> 
       <!-- unnecessary to have VALUE attribute as this element will always be submitted --> 
       <button type="submit" class="btn btn-success" name="do">Modifier</button> 
      </td> 
      <input type="hidden" name="id" value="<?php echo $article['id'] ?>"> 
     </table> 
    </form> 
    <?php endif ?> 
    </body> 
</html> 

其他景点:

  • 停止使用mysql_ *功能!他们由于很好的原因而被弃用。使用mysqli_*或更好的PDO函数。

  • 您的问题是容易发生SQL injection

  • 当混合使用HTML PHP控制结构(例如ifwhile等),我喜欢用自己alternative syntax(例如if (condition):endif; while (condition):endwhile;等)。它看起来更可读,imo。

  • 我正在使用ternary operator这是简单的if/else语句的较短语法。

  • 添加评论!

0

更新您的更新查询:

$sql = mysql_query("UPDATE articles SET nom='$nom', contenu='$contenu', categories_id='$categories_id' WHERE id ='$m_id' "); 

对于建议:

+0

如果你打算给出一个带有建议的答案,至少要给出一些好的信息而不是半焦的要点 – ash