-1

here is all my code

add.php

<?php 
    
    include_once('config.php');


    if(isset($_POST['submit']))
    {
        $emri = $_POST['emri'];
        $mbiemri = $_POST['mbiemri'];
        $email = $_POST['email'];

        
        $sql = "INSERT INTO users(emri,mbiemri,email) VALUES (:emri,:mbiemri,:email)";

        $arti = $con->prepare($sql);

        $arti->bindParam(':emri', $emri);
        $arti->bindParam(':mbiemri', $mbiemri);
        $arti->bindParam(':email', $email);

        $arti->execute();

        header("Location: index.php"); 

    }

    
 ?>

 <a href="index.php">Home</a>

config.php

<?php 

$host = 'localhost';
$db = 'maj6a';
$user = 'root';
$pass = '';

    try 
    {
        $con = new PDO("mysql:host=$host; dbname=$db", $user, $pass);
    } catch (Exception $e) {
        echo "Diqka shkoi gabim!";
    }


 ?>
<?php 
    
    include_once('config.php');

    $sql = "SELECT * from users";

    $prep = $con->prepare($sql);

    $prep->execute();

    $datas = $prep->fetchAll();

    // var_dump($data);

?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>

    <style>
        table
        {
            border: 1px solid black;
            border-collapse: collapse;
        }
        th,td
        {
            border: 1px solid black;
            padding: 5px 10px;
        }
    </style>
</head>
<body>
    
<table>
    <thead>
        <th>ID</th>
        <th>Emri</th>
        <th>Mbiemri</th>
        <th>Email</th>
    </thead>

    <tbody>

        <?php 
        foreach ($datas as $data) {
        ?>

            <tr>
                <td><?= $data['id'] ?></td>
                <td><?= $data['emri'] ?></td>
                <td><?= $data['mbiemri'] ?></td>
                <td><?= $data['email'] ?></td>
            </tr>

        <?php 
        }
        ?>

    </tbody>
</table>


<a href="index.php">Home</a>
</body>
</html>

index.php


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

    <form action="add.php" method="POST">
        
        <input type="text" name="emri" placeholder="Emri"><br>
        <input type="text" name="mbiemri" placeholder="Mbiemri"><br>
        <input type="email" name="email" placeholder="Email"><br>
        <br><br>
        <button type="submit" name="submit">Shto</button>
    </form>
    

    <a href="dashboard.php">Dashboard</a>
    

    
</body>
</html>

It's really irritating. I can't think of a reason the code might be wrong unless it's something very trivial, like a "" or a '' that's wrong. It's supposed to add data to SQL, then retrieve them from the other site. Ifa anyone could help me I would be very grateful.

Dielli
  • 1
  • 2
  • Now is the time to do some debugging. How specifically does this fail? Is the form posted to `add.php`? Is the user redirected to `index.php`? Are there any errors or warnings in the PHP logs? Does [`execute()` fail](https://stackoverflow.com/q/32648371/328193)? You need to narrow the problem. – David Sep 16 '20 at 17:42
  • @David hey. Thanks for replying. I've been trying to for the last couple of weeks, but I honest to God don't know how to fix this. That's why I posted this. – Dielli Sep 16 '20 at 17:44
  • What _actually_ happens when you submit the form? – droopsnoot Sep 16 '20 at 18:09
  • 1
    Enable error `PDO::ERRMODE_EXCEPTION` https://www.php.net/manual/en/pdo.error-handling.php – Barmar Sep 16 '20 at 18:37

1 Answers1

0

i think you have to set a value for the submit input

        <button type="submit" name="submit" value="sub">Shto</button>
shabandino
  • 224
  • 1
  • 3
  • I don't think this is necessary. If you leave out the value, it submits an empty string. `isset()` will still see it. – Barmar Sep 16 '20 at 18:33