dongzhidian3538 2014-06-01 19:11
浏览 67
已采纳

试图解决这个问题:未定义的变量

Im trying to update my table news dynamically.

And its working fine, but I want to show in my form the current values ​​that my news has, so Im using inside my value = <?php echo $read['title'] ; ?>

But with date, Im getting two errors:

Im getting this error inside my input: Notice: Undefined variable: spanish and Undefined variable: english

I understand erros, but I wanted to ask you, if you know how I can solve this, I know if I repeat definition of my variables "spanish and english" inside my date value, it works, but it not seems correct to repeat this variables.

Do you know how I con solve this using only one definition for $english and $spanish variables??

<?php
if(isset($_POST['sendForm']))
{
    $f['title'] = $_POST['title'];
    $f['date'] = $_POST['date'];

    if(in_array('',$f))
    {
    echo 'Fill all fields';

    }


else
{
    $english = array(); //here I have some words
    $spanish = array();
    $result_date = str_ireplace ($english , $spanish, $f['date']);
    $date = DateTime::createFromFormat('l, j F, Y', $result_date);
    $date = $date->format('Y-m-d H:i:s'); 


    $update = $pdo->prepare("UPDATE news set title=:title, date=:date WHERE id = :id");
    $update->bindValue(':title', $f['title']);
    $update->bindValue(':date', $date); 
    $update->bindValue(':id', '20');
    $update->execute();         
}

}

?>
<form  action="" method="post" enctype="multipart/form-data">
    <label class="line">
    <span>Title:</span>
    <input type="text" name="title" value="<?php echo $read['title'] ; ?>" />
    </label>


    <label class="line">
    <span>Date:</span>
    <input type="text" name="date" value="
    <?php 
        $date = new DateTime($read['date']);
        $result_date = str_ireplace($spanish , $english, $date->format('l, j F, Y'));
        echo $result_date;
    ?>" />
    </label>

    <input type="submit" value="Submit" name="sendForm"/>

</form>
  • 写回答

2条回答 默认 最新

  • dthyxna3894 2014-06-01 19:13
    关注
    <?php
        $english = array(); //define at first because if you post the variables are unset
        $spanish = array();
        if(isset($_POST['sendForm']))
        {
            $f['title'] = $_POST['title'];
            $f['date'] = $_POST['date'];
            if(in_array('',$f))
            {
                echo 'Fill all fields';
            }
        else
        {
            $result_date = str_ireplace ($english , $spanish, $f['date']);
            $date = DateTime::createFromFormat('l, j F, Y', $result_date);
            $date = $date->format('Y-m-d H:i:s'); 
            $update = $pdo->prepare("UPDATE news set title=:title, date=:date WHERE id = :id");
            $update->bindValue(':title', $f['title']);
            $update->bindValue(':date', $date); 
            $update->bindValue(':id', '20');
            $update->execute();         
        }
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算