如果条件满足,则插入datestamp

时间:2013-02-13 10:12:39

标签: php mysql datetime

我想在满足if条件后将日期时间戳插入变量中。但是我收到以下错误:

  

注意:未定义的索引:第55行的C:\ wamp \ www \ business \ edit_log_widget.php中的状态

以下是php代码:

<?php
    include 'scripts/init.php';
    include 'html/header.php';
    $page = 'servers';

    $id =$_SESSION['logid'];
    $query = "SELECT *FROM log WHERE logid = $id";
    $query_submit = mysql_query($query) or die(mysql_error);
    $row = mysql_fetch_assoc($query_submit);
?>

<div class="article">
    <h2><span>Edit Logs</span></h2>
    <div class="clr"></div>
    <form action="" method="POST" >
    <p>
        <table border="0">
            <tr>
                <td><label for="Task Name">Task Name:*</label></td>
                <td><input type="text" name="task_name" size="45" value="<?php echo $row['task_name'] ?>"/></td>
            </tr>
            <tr>
                <td><label for="description">Problem Description:*</label></td>
                <td><textarea name="description" cols="33" rows="10" ><?php echo $row['description'] ?></textarea></td>
           </tr>
           <tr>
                <td><label for="solution">Solution Description:*</label></td>
                <td><textarea name="solution" cols="33" rows="10" ><?php echo $row['solution'] ?></textarea></td>
           </tr>
           <tr>
                <td><label for="status">Status:*</label></td>
                <td>
                    <select id="Select2" name="status">
                        <option>-Select-</option>
                        <option>Resolved</option>
                        <option>Un-resolved</option>
                        <option>In-Progress</option>
                    </select>
                </td>
           </tr>
        </table>
    </p>
    <p>
        <td><input id="Submit" type="submit" value="Submit" /></td>
        <td><input id ="Clear and Restart" type ="reset" value= "Clear and Restart" /></td>
    </p>
    <?php 
        if($_POST['status']== 'Resolved')
        {
            $today = DateTime::createFromFormat('!Y-m-d',date('Y-m-d')); // This is Line 55
        }

        if(isset($_GET['success']) && empty($_GET['sucess']))
        {
            echo 'the log has been captured';
        }
        else
        {
            if(empty($_POST) === false && empty($errors)=== true)
            {
                //Update Log details
                $update_log = array(
                                'task_name'=>$_POST['task_name'],
                                'description' => $_POST['description'],
                                'solution' =>$_POST['solution'],
                                'status'=>$_POST['status'],
                                'closed_date'=>$today,
                                'userid' =>$_SESSION['userid']);
                update_log($update_log);
                //redirect
                header('Location: edit_log_widget.php?success');
                exit();
            }
            else if(empty($errors) === false)
            {
                //output errors if the errors array is not empty
                echo output($errors);
            }
        }
    ?>
    </form>  
    <?php
        include 'html/side_menu.php';
        include 'html/footer.php';
    ?>

更新:edit_log.php。

<?php
    include 'scripts/init.php';
    include 'html/header.php';
    $page = 'servers';

    $id = $_GET['logid'];
    $_SESSION['logid'] = $id;
    $query = "SELECT *FROM log WHERE logid = $id";
    $query_submit = mysql_query($query) or die(mysql_error);
    $row = mysql_fetch_assoc($query_submit);
?>

<div class="article">
    <h2><span>Edit Logs</span></h2>
    <div class="clr"></div>
    <form action="edit_log_widget.php" method="POST" >
    <p>
        <table border="0">
            <tr>
                <td><label for="Task Name">Task Name:*</label></td>
                <td><input type="text" name="task_name" size="45" value="<?php echo $row['task_name'] ?>"/></td>
            </tr>
            <tr>
                <td><label for="description">Problem Description:*</label></td>
                <td><textarea name="description" cols="33" rows="10" ><?php echo $row['description'] ?></textarea></td>
            </tr>
            <tr>
                <td><label for="solution">Solution Description:*</label></td>
                <td><textarea name="solution" cols="33" rows="10" ><?php echo $row['solution'] ?></textarea></td>
            </tr>
            <tr>
                <td><label for="status">Status:*</label></td>
                <td>
                    <select id="Select2" name="status">
                        <option>-Select-</option>
                        <option value="Resolved">Resolved</option>
                        <option value="Un-resolved">Un-resolved</option>
                        <option value="In-Progress">In-Progress</option>
                    </select>
                </td>
            </tr>
        </table>
    </p>
    <p>
        <td><input id="Submit" type="submit" value="Submit" /></td>
        <td><input id ="Clear and Restart" type ="reset" value= "Clear and Restart" /></td>
    </p>
</form>  
<?php
    include 'html/side_menu.php';
    include 'html/footer.php';
?>

5 个答案:

答案 0 :(得分:0)

您尚未为选项指定任何值;)

<option value="Resolved">Resolved</option>

答案 1 :(得分:0)

$_POST['status']

未定义数组$ _POST的条目状态

您正在尝试访问尚未设置的变量。

为了避免您可以先检查,如果表单是在提交之前提交的,例如

<?php 
        if(!empty($_POST['Submit'])){

                    if($_POST['status']== 'Resolved')
                        {
                            $today = DateTime::createFromFormat('!Y-m-d',date('Y-m-d')); // This is Line 55

                        }


                    if(isset($_GET['success']) && empty($_GET['sucess']))
                        {
                            echo 'the log has been captured';
                        }
                    else
                        {
                            if(empty($_POST) === false && empty($errors)=== true)
                                {
                                                                            //Update Log details
                                    $update_log = array(
                                    'task_name'=>$_POST['task_name'],
                                    'description' => $_POST['description'],
                                    'solution' =>$_POST['solution'],
                                    'status'=>$_POST['status'],
                                    'closed_date'=>$today,
                                    'userid' =>$_SESSION['userid']);


                                    update_log($update_log);
                                    //redirect
                                    header('Location: edit_log_widget.php?success');
                                    exit();

                                }
                            else if(empty($errors) === false)
                                {
                                    //output errors if the errors array is not empty
                                    echo output($errors);
                                }
                        }
                    }
            ?>

答案 2 :(得分:0)

PHP代码在表单提交之前执行,因此尚未定义$ _POST ['status']。

答案 3 :(得分:0)

你错过了一些html道具,你的<option>必须有value道具,如下所示:

<td>
   <select id="Select2" name="status">
      <option value="0">-Select-</option>
      <option value="1">Resolved</option>
      <option value="2">Un-resolved</option>
      <option value="3">In-Progress</option>
    </select>
</td>

你收到了这个错误,因为你在第一个html片段中遗漏了它,而你在第二个html片段中错过了它,所以没有什么可以让PHP获得

答案 4 :(得分:0)

我将所有的PHP移动到html表单的顶部,现在它工作正常。谢谢你们试图帮助我