0

I am trying to creat a crud application using PHP, I have succeeded to display my data table from postgres but when i try i got some errors. this is my code :

<?php require 'database.php'; $id = null; if ( !empty($_GET['Code_Espece'])) { $id = $_REQUEST['Code_Espece']; } if ( null==$id ) { header("Location: index.php"); } if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST)) {
$firstnameError = null; 

if (isset($_POST['Nom_Scien'])) {
                 $firstname = $_POST['Nom_Scien'];
            } 



 $valid = true;if (empty($firstname)) { $firstnameError = 'Please enter firstname'; $valid = false; }  
  if ($valid) { $pdo = Database::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            $sql = 'UPDATE espece SET Nom_Scien = ? WHERE Code_Espece = ?';

            $q = $pdo->prepare($sql);
            $q->execute(array($firstname));
            Database::disconnect();
            header("Location: index.php");
        } 
       }else {

             $pdo = Database::connect();
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = 'SELECT * FROM espece where "Code_Espece" = ?';
            $q = $pdo->prepare($sql);
            $q->execute(array($id));
            $data = $q->fetch(PDO::FETCH_ASSOC);
            $firstname = $data['Nom_Scien'];

            Database::disconnect();
        }

    ?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Crud-Update</title>
            <link href="css/bootstrap.min.css" rel="stylesheet">

    </head>
    <body>

<div class="container">

<div class="row">
</div>

<form method="post" action="update.php?Code_Espece=<?php echo $id ;?>">


</div>

<div class="control-group<?php echo!empty($firstnameError) ? 'error' : ''; ?>">
                    <label class="control-label">Nom Scientifique</label>

<br />
<div class="controls">
                        <input type="text" name="nomscientifique" value="<?php echo!empty($firstname) ? $firstname : ''; ?>">
                        <?php if (!empty($firstnameError)): ?>
                            <span class="help-inline"><?php echo $firstnameError; ?></span>
                        <?php endif; ?>
</div>
</div>

<div class="form-actions">
                    <input type="submit" class="btn btn-success" name="update" value="update">
                    <a class="btn" href="index.php">Retour</a>
</div>
            </form>
</div>
    </body>
</html>

The problem is when i put what i want to change and click in the button 'Update' it return the following message : 'Please enter firstname' inspite of teh input isnot empty.

Thank you ;

1
  • Not related: put exit; after header("Location: index.php"); Commented Jun 27, 2017 at 10:25

1 Answer 1

1

Change

<input type="text" name="nomscientifique" value="<?php echo!empty($firstname) ? $firstname : ''; ?>">
to
<input type="text" name="Nom_Scien" value="<?php echo!empty($firstname) ? $firstname : ''; ?>">

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you; Your answer was helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.