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 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题