从PHP文件读取和写入时遇到问题

时间:2013-11-29 11:18:37

标签: php html mysql

我正在做的是首先生成一个动态下拉框,其中显示了页面名称。

我想要做的是取决于下拉列表中选择的文件,将该文件加载到文本框中,然后当我提交更改时,新文件将包含文本框中的新文本。

这就是我尝试过的:

   <select name="page_name">

                        <?php
                            include_once('lib/sql.php');
                            $sql = new sql();
                            $mysqli = new mysqli(blah);
                            /*$arr = array(
                               'cust_id' => "{$_SESSION['cust_id']}"
                            );
                            $query = $sql->select_query("code",$arr);
                            */
                            $query = "SELECT * FROM code";
                           $result = $mysqli->query($query);


                            while ($row = $result->fetch_assoc()) {
                              //var_dump($row['pagename']);
                            ?>
                                 <option name="<?php echo $row['pagename']; ?>" > <?php echo $row['pagename']; ?> </option>
                            <?php
                            } 
                            ?>

                       </select>
                   </td>
                <tr/>
                <tr>
                    <td>
                        <input type="submit" name="edit_page" Value="Select Page">
                    </td>
                </tr>
                 <tr>
                   <td>PHP/HTML CODE:</td>
                   <td>       
                   <?php
                   if(isset($_POST['page_name']) && $_POST['page_name'] != '' && isset($_POST['edit_page'])){
                      $_POST['page_name'] = $_SESSION['pgname'];
                      var_dump($_POST['page_name']);
                      //die();

                      if(isset($_POST['editcode']) && isset($_POST['commit_changes']))
                      {
                          file_put_contents("{$_SESSION['pgname']}","{$_POST['editcode']}");
                          header("Location: ".$_SERVER['PHP_SELF']);
                          //exit;
                      }
                      $text = file_get_contents("{$_SESSION['pgname']}");
                     echo " <textarea id='textarea_2' name='editcode' cols='90' rows='20'>";echo trim($text);echo "</textarea>";

                  ?>
                   </td>

有人可以帮忙吗? 感谢

2 个答案:

答案 0 :(得分:1)

编辑:

<form method="post" action="subForm.php">    
<table>
    <tr>    
    <select name="page_name">
        <?php
        include_once('lib/sql.php');
        $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
        $query = "SELECT * FROM code";
        $result = $mysqli->query($query);
        while ($row = $result->fetch_assoc()) { ?>
            <option 
                name="page_name" 
                value="<?php echo $row['pagename']; ?>" 
            > 
                <?php echo $row['pagename']; ?> 
            </option>
        <?php } ?>
    </select>
    </td>
    <tr/>
    <tr>
        <td>
            <input type="submit" name="edit_page" Value="Select Page">

        </td>
    </tr>
</table>
</form>
<?php //here your other PHP code or to external file ?>

您没有表格,也没有选择价值。 action可以导致您的其他PHP代码检查数据并将其传递到$_SESSION

EDIT2: 这是您的外部文件:

<table>
    <tr>
         <td>PHP CODE:</td>
    </tr>
    <tr>
         <td>
    <?php
    if(isset($_POST['page_name']) && $_POST['page_name'] != '' && isset($_POST['edit_page'])) {
        $_POST['page_name'] = $_SESSION['pgname'];
        var_dump($_POST['page_name']);    
        if(isset($_POST['editcode']) && isset($_POST['commit_changes'])) {
            file_put_contents("{$_SESSION['pgname']}","{$_POST['editcode']}"); // I dont know if this right, but ok.
            header("Location: ".$_SERVER['PHP_SELF']);
            //exit;
        }
        $text = file_get_contents("{$_SESSION['pgname']}");
        echo " <textarea id='textarea_2' name='editcode' cols='90' rows='20'>"; 
        echo trim($text); 
        echo "</textarea>";  ?>
        </td>
    </tr>
</table>

答案 1 :(得分:0)

检查服务器上目标文件夹的权限。

您必须具有服务器上用户的写入权限才能写入文件。