The button does not send data to the database

A

Anonymous

Guest
Hello, would someone be so nice and tell me why after pressing the button it does not send data?


Code:
<?php

include "connect.php";

$id = (isset($_POST['ID']) ? $_POST['ID'] : '');

$qry = mysqli_query($conn,"select * from podstawowa where id='$id'");

$data = mysqli_fetch_array($qry);

if(isset($_POST["update"]))
{
    $id = $_POST['ID'];
    $tytul = $_POST['Tytuł'];
    $gatunek = $_POST['Gatunek'];
    $dataw = $_POST['Data_wykonania'];
    $platforma = $_POST['Platforma'];
    $nosnik = $_POST['Nośnik'];
    $srednia = $_POST['Średnia_ocena'];
	
    $edit = mysqli_query($conn,"UPDATE `podstawowa` SET `Tytuł`='$tytul',`Gatunek`='$gatunek',`Data wykonania`='$dataw',`Platforma`='$platforma',`Nośnik`='$nosnik',`Średnia ocena`='$srednia' where id='$id'"); var_dump($edit);
	
    if($edit)
    {
        echo("Zmodyfikowano");
        mysqli_close($conn);
        header("location:update.php");
        exit;
    }
    else
    {
        echo mysqli_error();
    }    	
}
?>

<h3>Update Data</h3>

<form method="POST" action="edytuj.php">
  <input type="hidden" name="ID">
  <input type="text" name="Tytuł" value="<?php echo $data['Tytuł'] ?>" placeholder="Wprowadź tytuł" Required>
  <input type="text" name="Gatunek" value="<?php echo $data['Gatunek'] ?>" placeholder="Wprowadź gatunek" Required>
  <input type="date" name="Data_wykonania" value="<?php echo $data['Data_wykonania'] ?>" placeholder="Wprowadź datę wydania" Required>
  <input type="text" name="Platforma" value="<?php echo $data['Platforma'] ?>" placeholder="Wprowadź platformę" Required>
  <input type="text" name="Nośnik" value="<?php echo $data['Nośnik'] ?>" placeholder="Wprowadź nośniki" Required>
  <input type="number" name="Średnia_ocena" step="any" value="<?php echo $data['Średnia_ocena'] ?>" placeholder="Wprowadź średnią ocenę" Required>
  <input type="submit" name="update" value="Edytuj">


</form>
 
The $_POST['ID'] seems to be empty (the form input contains only the name).
Additionally you should NEVER trust the data from a user. Read more about binding parameters to the query and sql injection vulnerability

Binding params:
https://www.php.net/manual/en/mysqli-stmt.bind-param.php
SQL Injection:
https://owasp.org/www-community/attacks/SQL_Injection
https://pl.wikipedia.org/wiki/SQL_injection
 
Here are a few things to check:

Check if the form method and action are correct
Verify if the connect.php file is included correctly
check if the form inputs have a name attribute
Ensure the ID field is correctly populated
Verify if the SQL query is returning results
Check for any error messages

By following these steps you should be able to solve you problems.
 
  1. Input Field Names: Add a value to the hidden input field for the ID: value="<?php echo $id; ?>".
  2. Form Action: Set the form action to the correct file where the PHP code resides, or remove it to default to the current file: action="<?php echo $_SERVER['PHP_SELF']; ?>".
  3. Database Connection: Verify that the connect.php file establishes a successful database connection and that the $conn variable is valid.
  4. SQL Query: Check the SQL query for updating the data, ensuring that table and column names match the database structure and that the id column is the primary key.
  5. Error Reporting: Update the error reporting line to include the database connection as an argument: echo mysqli_error($conn);.
By addressing these potential issues, you should be able to resolve the problem by sending the data after pressing the button.
 
1. Missing the ID value in form :
<input type="hidden" name="ID" value="<?php echo $data['ID'] ?>">
Kindly provide the ID filed some value so that the database may be updated.

2.The action may be empty if you are submitting data on the same page.
 
Back
Top