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 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算