Autor Zpráva
Jan Žák
Profil
Dobrý den všem,

chtěl bych vás požádat o pomoc, nevím si rady. Mám script na update dokumentů v db. Bohužel při editaci položek mi napíše hlášku, o vyplnění povinných polí, které vyplněny jsou. Obdobně mám script zhotoven pro INSERT a tam vše funguje dobře. Koukám do toho již den a nevím si rady, poradil by mě někdo z vás? Děkuji moc.

if (isset($_POST['submit'])) {

    // confirm that the 'id' value is a valid integer before getting the form data
    if (is_numeric($_POST['id'])) {
        // get form data, making sure it is valid
    $default = array(
    'id' => '',
    'doc_number' => '',
    'name' => '',
    'description' => '',    
    'date' => '',
    'doc' => '',
    'contract_id' => ''
    );
    $_POST = array_merge($default, $_POST);

    $id = $_POST['id'];
    $doc_number = isset($_POST['doc_number'])? intval($_POST['doc_number']) : 0;
    $name = sanitizeString($_POST['name']);
    $description = sanitizeString($_POST['description']);
    if (empty($_POST["date"])) { $date = '0000-00-00'; } else { $date = strtotime($_POST["date"]); $date = date("Y-m-d", $date); };
    $doc= $_FILES['doc']['name'];
    $contract_id = isset($_POST['contract_id'])? intval($_POST['contract_id']) : 0;

    // get the original file name from $_FILES
    $file_name= $_FILES['doc']['name'];
    
    // remove any characters you dont want
    $some_special_chars = array("ě", "š", "č", "ř", "ž", "ý", "á", "í", "é", "ú", "ů", "ñ", "ň", "Ě", "Š", "Č", "Ř", "Ž", "Ý", "Á", "Í", "É", "Ú", "Ů");
    $replacement_chars  = array("e", "s", "c", "r", "z", "y", "a", "i", "e", "u", "u", "n", "n", "E", "S", "C", "R", "Z", "Y", "A", "I", "E", "U", "U");
    $file_name = str_replace($some_special_chars, $replacement_chars, $file_name);
    
    $file_name = preg_replace('/\s+/', '_', $file_name);
    
    // get the location of the folder to upload into
    $location = '../../documents/';

    // check if required fields are filled in
    if ($name && $description) {
    
      // move the files from the temporary directory into your new home and save the data to DB :)
      if (move_uploaded_file($_FILES["doc"]["tmp_name"], $location.$file_name)) {
  
      // insert the report to the database, then
      report('upravil/a dokument <strong>'.$_POST['name'].'</strong>');
      
      mysqli_query($con,"UPDATE documents SET doc_number='$doc_number', name='$name', description='$description', date='$date', doc='$file_name', contract_id='$contract_id', `updated`= NOW() WHERE id='$id'") or die(mysqli_error($con));
  
      // once inserted, generate success message
      $errmsg[] = 'Dokument úspěšně upraven.';
      $_SESSION['ERRMSG'] = $errmsg;
      Header("Location: documents-edit.php?id=$id");
  
      } else {
      // or generate error message
      $errmsg[] = 'Vyplňte prosím všechna povinná pole!';
      $_SESSION['ERRMSG'] = $errmsg;
      Header("Location: documents-edit.php?id=$id");
      }  
    }
  } else {
  // else if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
  require_once (ROOT . '/core/errors/error.php');
  exit;
    }
}
Slark
Profil
Zkusil bych prověřit proměnou $file_name. Potom ještě jestli se náhodou nenachází script pro update v jiné složce než pro insert? Pak by bylo třeba upravit hodnotu proměnné $location.
Z toho bloku kódu:

else {
    // or generate error message
    $errmsg[] = 'Vyplňte prosím všechna povinná pole!';
    $_SESSION['ERRMSG'] = $errmsg;
    Header("Location: documents-edit.php?id=$id");
    }  

zakomentujte ten Header ať se dovíte chybovou hlášku
Jan Žák
Profil
Díky za odpověď, ten script fungoval normálně a je ve stejné složce, všechny údaje uložil do db, ale před dvěma dny jsem ho upravil, abych i z editace položky mohl nahrávat soubory a vložil tam tedy řádky pro nahrání souboru. A od té doby to píše již výše popsané. Díky za tip, zkusím zakomentovat header a uvidím.

Edit: Header jsem zakomentoval, vložil tam ještě print_r($_POST) ; a je to bez chybové hlášky. Pokud vyberu nový soubor, celý skript proběhne, data se uloží, soubor se nahraje do příslušné složky. Pokud soubor nevyberu, pole nechám prázdné a chci editovat pouze text, napíše mi to již výše zmíněnou hlášku. Přitom to pole povinné není.
Slark
Profil
Zkuste to takto:

if (isset($_POST['submit'])) {
 
    // confirm that the 'id' value is a valid integer before getting the form data
    if (is_numeric($_POST['id'])) {
        // get form data, making sure it is valid
    $default = array(
    'id' => '',
    'doc_number' => '',
    'name' => '',
    'description' => '',    
    'date' => '',
    'doc' => '',
    'contract_id' => ''
    );
    $_POST = array_merge($default, $_POST);
 
    $id = $_POST['id'];
    $doc_number = isset($_POST['doc_number'])? intval($_POST['doc_number']) : 0;
    $name = sanitizeString($_POST['name']);
    $description = sanitizeString($_POST['description']);
    if (empty($_POST["date"])) { $date = '0000-00-00'; } else { $date = strtotime($_POST["date"]); $date = date("Y-m-d", $date); };
    
    $contract_id = isset($_POST['contract_id'])? intval($_POST['contract_id']) : 0;
 
    // get the original file name from $_FILES
    $file_name = "";
    $doc = "";

    if(!empty($_FILES['doc']['name']))
    {
      $doc= $_FILES['doc']['name'];

      $file_name= $_FILES['doc']['name'];
      
      // remove any characters you dont want
      $some_special_chars = array("ě", "š", "č", "ř", "ž", "ý", "á", "í", "é", "ú", "ů", "ñ", "ň", "Ě", "Š", "Č", "Ř", "Ž", "Ý", "Á", "Í", "É", "Ú", "Ů");
      $replacement_chars  = array("e", "s", "c", "r", "z", "y", "a", "i", "e", "u", "u", "n", "n", "E", "S", "C", "R", "Z", "Y", "A", "I", "E", "U", "U");
      $file_name = str_replace($some_special_chars, $replacement_chars, $file_name);
      
      $file_name = preg_replace('/\s+/', '_', $file_name);
    }

    
    // get the location of the folder to upload into
    $location = '../../documents/';
 
    // check if required fields are filled in
    if ($name && $description) {
    
      // move the files from the temporary directory into your new home and save the data to DB :)
      if (!empty($file_name)) {
        move_uploaded_file($_FILES["doc"]["tmp_name"], $location.$file_name);
      } 
  
      // insert the report to the database, then
      report('upravil/a dokument <strong>'.$_POST['name'].'</strong>');
      
     $q =  mysqli_query($con,"UPDATE documents SET doc_number='$doc_number', name='$name', description='$description', date='$date', doc='$file_name', contract_id='$contract_id', `updated`= NOW() WHERE id='$id'") or die(mysqli_error($con));
  
      // once inserted, generate success message
     if($q)
     {
        $errmsg[] = 'Dokument úspěšně upraven.';
        $_SESSION['ERRMSG'] = $errmsg;
        Header("Location: documents-edit.php?id=$id");

      } else {
      // or generate error message
      $errmsg[] = 'Vyplňte prosím všechna povinná pole!';
      $_SESSION['ERRMSG'] = $errmsg;
      Header("Location: documents-edit.php?id=$id");
      }  
    }
  } else {
  // else if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
  require_once (ROOT . '/core/errors/error.php');
  exit;
    }
}
Tomášeek
Profil
Slark:
Cely kód jsem neprochazel, ale konstrukce na řádcích 6-15 ne peklo.

Nikdy neprepisuj superglobální proměnné, na konstrukci $_POST = cokoliv zapomen a v zadnem případe ji nedoporučuj.
Jan Žák
Profil
Díky všem za pomoc, s menší úpravou to funguje a díky za info Tomášeek, ty řádky tam opravdu nemají co dělat.

Vaše odpověď

Mohlo by se hodit


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0