Max Bogatec Max Bogatec - 3 months ago 10
MySQL Question

PHP Update Table Error Solved

I'm working on an website and made an form where you can add vehicles. Now I created a form where you should be able to edit them, but when I press on edit vehicle it gives out my default error message


Beim Speichern ist leider ein Fehler aufgetreten


Now I need your help. I have no clue where the problem could be, but I think it has to do something with the update query

$showFormular = true; //Variable ob das Registrierungsformular anezeigt werden soll
$pdo = new PDO('mysql:host=localhost;dbname=', '', '');

if(isset($_GET['bearbeiten'])) {
$error = false;
$EKNR = $_POST['EKNR'];
$EKDatum = $_POST['EK-Datum'];
$RGNummer = $_POST['RG-Nummer'];
$Marke = $_POST['Marke'];
$Modell = $_POST['Modell'];
$EZ = $_POST['EZ'];
$MotorNummer = $_POST['Motornummer'];
$Fin = $_POST['FIN'];
$KM = $_POST['KM'];
$VK = $_POST['VK'];
$EKBrutto = $_POST['EK-Brutto'];
$EKNetto = $_POST['EK-Netto'];
$VKDatum = $_POST['Verkaufsdatum'];
$Lack = $_POST['Farbe'];

//Keine Fehler, wir können den Nutzer registrieren
if(!$error) {
$statement = $pdo->prepare("UPDATE `Fahrzeugverkauf` SET `EKNR`=[$EKNR],`EKDatum`=[$EKDatum],`RGNummer`=[$RGNummer],`Marke`=[$Marke],`Modell`=[$Modell],`EZ`=[$EZ],`MotorNr`=[$MotorNummer],`FIN`=[$Fin],`KM`=[$KM],`VK`=[$VK],`EKBrutto`=[$EKBrutto],`EKNetto`=[$EKNetto],`Farbe`=[$Lack],`VKDatum`=[$VKDatum] WHERE EKNR = $id");
$result = $statement->execute(array('EKNR' => '$EKNR', 'EKDatum' => '$EKDatum', 'RGNummer' => '$RGNummer', 'Marke' => '$Marke', 'Modell' => '$Modell', 'EZ' => '$EZ', 'MotorNr' => '$MotorNummer', 'FIN' => '$Fin', 'KM' => '$KM', 'VK' => '$VK', 'EKBrutto' => '$EKBrutto', 'EKNetto' => '$EKNetto', 'Farbe' => '$Lack', 'VKDatum' => '$VKDatum'));

if($result) {
echo 'Das Fahrzeug wurde erfolgreich hinzugefügt.'; `
`
$showFormular = false;
} else {
echo 'Beim Abspeichern ist leider ein Fehler aufgetreten<br>'
;


Update

Solved code:

<?php
function updater($value,$value2,$value3,$value4,$value5,$value6,$value7,$value8,$value9,$value10,$value11,$value12,$value13,$value14,$value15,$value16,$value17,$value18){
// Create connection
$conn = new mysqli( 'localhost' , 'user' , 'pass' ,'db_name' );
$value =mysqli_real_escape_string($conn,$value);
$value2 =mysqli_real_escape_string($conn,$value2);
$value3 =mysqli_real_escape_string($conn,$value3);
$value4 =mysqli_real_escape_string($conn,$value4);
$value5 =mysqli_real_escape_string($conn,$value5);
$value6 =mysqli_real_escape_string($conn,$value6);
$value7 =mysqli_real_escape_string($conn,$value7);
$value8 =mysqli_real_escape_string($conn,$value8);
$value9 =mysqli_real_escape_string($conn,$value9);
$value10 =mysqli_real_escape_string($conn,$value10);
$value11 =mysqli_real_escape_string($conn,$value11);
$value12 =mysqli_real_escape_string($conn,$value12);
$value13 =mysqli_real_escape_string($conn,$value13);
$value14 =mysqli_real_escape_string($conn,$value14);
$value15 =mysqli_real_escape_string($conn,$value15);
$value16 =mysqli_real_escape_string($conn,$value16);
$value17 =mysqli_real_escape_string($conn,$value17);
$value18 =mysqli_real_escape_string($conn,$value18);
// Check connection

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE Fahrzeugverkauf SET EKNR='{$value}', EKDatum='{$value3}' , RGNummer='{$value4}' , Marke='{$value5}' , Modell='{$value6}' , Farbe='{$value7}', VKNummer='{$value8}', EZ='{$value9}', FIN='{$value10}', KM='{$value11}', VK='{$value12}', EKBrutto='{$value13}', EKNetto='{$value14}', VKDatum='{$value15}', MotorNr='{$value16}', Status='{$value17}', Anmerkung='{$value18}' WHERE EKNR='{$value2}'";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
}

if($_SERVER['REQUEST_METHOD'] === 'POST') {
$value = $_POST['EKNR'];
$value2 = $_POST['EKNR2'];
$value3 = $_POST['EK-Datum'];
$value4 = $_POST['RG-Nummer'];
$value5 = $_POST['Marke'];
$value6 = $_POST['Modell'];
$value7 = $_POST['Farbe'];
$value8 = $_POST['VKnummer'];
$value9 = $_POST['EZ'];
$value10 = $_POST['FIN'];
$value11 = $_POST['KM'];
$value12 = $_POST['VK'];
$value13 = $_POST['EK-Brutto'];
$value14 = $_POST['EK-Netto'];
$value15 = $_POST['Verkaufsdatum'];
$value16 = $_POST['Motornummer'];
$value17 = $_POST['selektion'];
$value18 = $_POST['anmerkung'];
updater($value,$value2,$value3,$value4,$value5,$value6,$value7,$value8,$value9,$value10,$value11,$value12,$value13,$value14,$value15,$value16,$value17,$value18);
}
?>

Answer

Solved code:

<?php
function updater($value,$value2,$value3,$value4,$value5,$value6,$value7,$value8,$value9,$value10,$value11,$value12,$value13,$value14,$value15,$value16,$value17,$value18){
    // Create connection
    $conn = new mysqli( 'localhost' , 'user' , 'pass' ,'db_name' );
    $value =mysqli_real_escape_string($conn,$value);
    $value2 =mysqli_real_escape_string($conn,$value2);
    $value3 =mysqli_real_escape_string($conn,$value3);
    $value4 =mysqli_real_escape_string($conn,$value4);
    $value5 =mysqli_real_escape_string($conn,$value5);
    $value6 =mysqli_real_escape_string($conn,$value6);
    $value7 =mysqli_real_escape_string($conn,$value7);
    $value8 =mysqli_real_escape_string($conn,$value8);
    $value9 =mysqli_real_escape_string($conn,$value9);
    $value10 =mysqli_real_escape_string($conn,$value10);
    $value11 =mysqli_real_escape_string($conn,$value11);
    $value12 =mysqli_real_escape_string($conn,$value12);
    $value13 =mysqli_real_escape_string($conn,$value13);
    $value14 =mysqli_real_escape_string($conn,$value14);
    $value15 =mysqli_real_escape_string($conn,$value15);
    $value16 =mysqli_real_escape_string($conn,$value16);
    $value17 =mysqli_real_escape_string($conn,$value17);
    $value18 =mysqli_real_escape_string($conn,$value18);
    // Check connection

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }   
    $sql = "UPDATE Fahrzeugverkauf SET EKNR='{$value}', EKDatum='{$value3}' , RGNummer='{$value4}' , Marke='{$value5}' , Modell='{$value6}' , Farbe='{$value7}', VKNummer='{$value8}', EZ='{$value9}', FIN='{$value10}', KM='{$value11}', VK='{$value12}', EKBrutto='{$value13}', EKNetto='{$value14}', VKDatum='{$value15}', MotorNr='{$value16}', Status='{$value17}', Anmerkung='{$value18}' WHERE EKNR='{$value2}'";
    if ($conn->query($sql) === TRUE) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . $conn->error;
    }
    $conn->close();
}   

if($_SERVER['REQUEST_METHOD'] === 'POST') {
    $value = $_POST['EKNR'];
    $value2 = $_POST['EKNR2'];
    $value3 = $_POST['EK-Datum'];
    $value4 = $_POST['RG-Nummer'];
    $value5 = $_POST['Marke'];
    $value6 = $_POST['Modell'];
    $value7 = $_POST['Farbe'];
    $value8 = $_POST['VKnummer'];
    $value9 = $_POST['EZ'];
    $value10 = $_POST['FIN'];
    $value11 = $_POST['KM'];
    $value12 = $_POST['VK'];
    $value13 = $_POST['EK-Brutto'];
    $value14 = $_POST['EK-Netto'];
    $value15 = $_POST['Verkaufsdatum'];
    $value16 = $_POST['Motornummer'];
    $value17 = $_POST['selektion'];
    $value18 = $_POST['anmerkung'];
    updater($value,$value2,$value3,$value4,$value5,$value6,$value7,$value8,$value9,$value10,$value11,$value12,$value13,$value14,$value15,$value16,$value17,$value18);
}
?>