简体   繁体   中英

Trying to solve this Notice: Undefined variable

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'] ; ?> <?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>
<?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();         
    }

    }

Notices you can off through php.ini file or just use the error_reporting(0); but make sure here this will not showing any other error. you can enable this after the site will goes to live. may this help you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM