Php+MySql: Estrarre dati da tabella e consentire all'utente di modificare un valore 'in linea'

  • Creatore Discussione Creatore Discussione malela
  • Data di inizio Data di inizio

malela

Nuovo Utente
20 Mar 2014
5
0
0
Ciao a tutti!
Premetto che sono piuttosto nuova di PHP e al 100% autodidatta e avrei bisogno di un piccolo aiuto da questa bellissima community.

Sto creando un gestionale Ordini-Magazzino-Vendite PHP+MySql per il mio negozio e vorrei fare in modo che dalla pagina per ricevere la merce ordinata fosse possibile modificare la quantità ricevuta (in caso di discrepanze tra l'ordinato e cio' che effettivamente è stato ricevuto) 'in linea', ossia premendo il tasto "modifica" diventasse modificabile la casella quantità corrispondente alla riga selezionata. Così una volta premuto invio vengono aggiornati i dati in magazzino.

Spero di essere stata comprensibile, posto il mio codice (scusate la rozzezza!) fino ad ora.
Al momento diventa modificabile il campo 'quantita' di tutte le righe, mentre vorrei fosse modificabile solo quella corrispondente al tasto premuto.
Devo inoltre ancora creare l'azione che viene eseguita una volta premuta la conferma, (e anche tutta la parte dell'UPDATE ma ci penserò a tempo debito).

PHP:
<?php

include('connect.php');
//Visualizza merce ordinata nella data selezionata//
if(isset($_POST['show']))
{
$levata = ($_POST['levata']);
 session_start();
$_SESSION[levata]=$levata;
echo "<table border='1' cellpadding='10'><tr style='font-weight: bold;'>";
echo "<td width='100'>Cod.AAMS</td><td width='100' align='center'>Ricevuto</td><td width='300'>Prodotto</td>";
$result = mysql_query("SELECT * FROM ordini WHERE data ='$levata'");
while($row=mysql_fetch_array($result))
{
$AAMS=$row['AAMS'];
echo "<tr><td><input type='hidden' name='AAMS' value='".$AAMS."'>".$AAMS."</td>";
echo "<td><input name='quantita'  type='hidden' value='".$row['quantita']."'>".$row['quantita']."</td>";
echo "<td>";
 $query_nome=mysql_query("SELECT * FROM prodotti WHERE AAMS= {$AAMS}")or die(mysql_error());
 while($nome= mysql_fetch_array ($query_nome)) {
 echo $nome['descriz']; }
 echo "</td>";
 echo "<td><form name='edit_order' action='".$_PHP_SELF."' method='post'>
        <input type='hidden' name='ordinato_id' value='". $row['id'] ."'>
		<input type='submit' name='edit_order' value='MODIFICA'></form></td>";
}
}
//ho premuto il tasto modifica//
else if (isset($_POST['edit_order'])) {
session_start();
$levata = $_SESSION[levata];
$mod_id= ($_POST['ordinato_id']);
echo "<table border='1' cellpadding='10'><tr style='font-weight: bold;'>";
echo "<td width='100'>Cod.AAMS</td><td width='100' align='center'>Ricevuto</td><td width='300'>Prodotto</td>";
$result = mysql_query("SELECT * FROM ordini WHERE data ='$levata'");
while($row=mysql_fetch_array($result))
{
$AAMS=$row['AAMS'];
echo "<tr><td><input type='hidden' name='AAMS' value='".$AAMS."'>".$AAMS."</td>";
if ($row['id'] =$mod_id) {
echo "<td><input name='quantita'  value='".$row['quantita']."'></td>";
}
else {
echo "<td><input name='quantita'  type='hidden' value='".$row['quantita']."'>".$row['quantita']."</td>";
}
echo "<td>";
 $query_nome=mysql_query("SELECT * FROM prodotti WHERE AAMS= {$AAMS}")or die(mysql_error());
 while($nome= mysql_fetch_array ($query_nome)) {
 echo $nome['descriz']; }
 echo "</td>";
 if ($row['id'] =$mod_id) {
echo "<td><form name='conferma_mod' action='".$_PHP_SELF."' method='post'>
			<input type='submit' name='conferma_mod' value='CONFERMA'></form></td>";
}
else {
 echo "<td></td>";  }
}
}
else {?>
<form action="<?php $_PHP_SELF ?>" method='post'>
Ricevi Merce Ordinata il 
<input type="date" value="" name="levata">
<input name='show' type='submit'>
</form>
<?php } ?>

Ogni suggerimento è prezioso!
Grazie in anticipo!
 
ciao,
quando ho cominciato ad usare php, mi infastidiva la frammentazione degli script,
ho realizzato un'applicazione dove tutti gli script sono "unici"
ovvero ciascuno è autosufficiente, richiamano solo pochi script comuni quali
il "form", ovviamente inteso come base, perchè i contenuti sono poi gestiti nello script

il seguente sito mi ha dato l'idea della tecnica da utilizzare per lo sviluppo anche se,
l'argomento è completamente diverso, mentre si avvicina al tuo caso

http://coredogs.com/lesson/form-and-php-validation-one-page

ti posto anche la traccia di un mio script con lo schema delle funzionalità
giusto per l'idea ...., spero che abbia valore
ciao
Marino

PHP:
if (isset($_GET['par'])) {               // entra con parametro non con submit
  $db->MyLog("par : ".$_GET['par']);

  $func = substr($_GET['par'], 0, 1);

  if (!strspn($func, 'BUDM')) $db->MyErr("SOLID ERROR a");
  if (!isset($_SESSION[$swN.'_data'])) $db->MyErr("SOLID ERROR b");

  if ($func == 'B') {
    $_POST['Action'] = "Cancella";
  }
  else {
    $_SESSION[$swN.'_ind'] = abs(filter_var(substr($_GET['par'], 1), FILTER_SANITIZE_NUMBER_INT));
    find_record(0);

    if ($func == 'U') {
      $_POST['Action'] = "Modifica";
    }
    else {
      if ($func == 'D') {
        $_POST['Action'] = "Elimina";
      }
      else {
        if ($func == 'M') {
          $_POST['Action'] = "M.O.";
} } } } }

if (!isset($_POST['Submitted'])) {
  $_POST['Submitted'] = "0";
  $_POST['Action'] = "";
}
$Buttons  = 1;  // gestione dei bottoni
$nextform = 1;  // gestione del form

$OPSubmitted = $_POST['Submitted'];
$OPACTION = $_POST['Action'];
$db->MyLog("OPSubmitted (1) : $OPSubmitted");
$db->MyLog("OPAction    (1) : $OPACTION");

switch ($OPACTION) {          // entra dopo submit o dal parametro

  case "M.O.":
    ....
    break;

  case "Continua":
    ....
    break;

  case "Cancella":
    ....
    break;

  case "Abbandona":
    ....

    header(NextStream($OPfather));
    exit();
    break;
}

$db->MyLog("OPSubmitted (2) : $OPSubmitted");
$db->MyLog("OPAction    (2) : $OPACTION");

if($OPSubmitted == "0") EmptyFields();
else {
  if ($OPSubmitted == "1") {
    check_SOLID();
    check_fields();
  }

  switch ($OPACTION) {

    case "Cerca":
      ....
      break;

    case "Inserisci":
      ....
      break;

    case "Aggiorna":
      ....
      break;

    case "Elimina":
      ....
      break;

    case "Conferma":
      ....
      break;

    case "Avanti":
      ....
      break;

    case "Indietro":
      ....
      break;

    case "Modifica":
      ....
      break;

    case "Annulla":
      ....
      break;
} }
($nextform == 1 ? DisplayForm1() : DisplayForm2());

funzioni .......
 
Marino51 grazie per la tua risposta.
A dire il vero non so se non mi sono spiegata per bene io oppure non sono riuscita a capire la tua risposta ma mi sembra che non si applichi al mio problema.
Ho letto l'articolo che mi hai linkato, ma parla di gestione degli errori e validazione dei dati, che è un passaggio successivo a quello in cui mi trovo ora (da profana affronto un passaggio per volta).

In sostanza a me servirebbe che dopo aver premuto il tasto "Modifica" di una riga, in quella riga (e solo in quella) il campo quantità passi da avere un type='hidden' ad un type='text', mentre tutte le altre righe dovrebbero rimanere come prima (hidden). Cio' che ho provato ora
PHP:
if ($row['id'] =$mod_id) {
echo "<td><input name='ordine[" . $row['id'] . "][ins]'  type='text' value='".$row['quantita']."'></td>";
}
else {
echo "<td><input name='ordine[" . $row['id'] . "][ins]'  type='hidden' value='".$row['quantita']."'>".$row['quantita']."</td>";
}
dove
PHP:
$mod_id= ($_POST['ordinato_id']);
(passato dal bottone modifica) mi rende modificabili tutte le righe.

Comunque credo che potrei tenere anche questa soluzione, mettendo un unico tasto "Modifica" alla fine del form.
Volevo solo cercare una soluzione piu' pratica, per fare in modo che mentre uno controlla l'ordine (che puo' avere anche molte righe) con la fattura della merce ricevuta, puo' variare la quantità nonappena trova una discrepanza.
 
ciao,
non avendo mysql ho adattato il tuo script, te lo posto in fondo per la curiosità ....
ma adattandolo e provandolo, ho trovato il motivo per cui non lavora bene,

sostituisci le due righe if dove c'è un solo uguale con due ...

PHP:
if ($row['id'] =$mod_id) { 


if ($row['id'] == $mod_id) {

ciao
Marino

PHP:
<?php 
// session_start(); 

# http://localhost/test_site/php/test/malela.php

$_POST['levata'] = '22/03/2014';

$result[] = array('AAMS'=>'1432','quantita'=>'1','id'=>'15','descriz'=>'prova1');
$result[] = array('AAMS'=>'1432','quantita'=>'2','id'=>'14','descriz'=>'prova2');
$result[] = array('AAMS'=>'1432','quantita'=>'3','id'=>'13','descriz'=>'prova3');
$result[] = array('AAMS'=>'1432','quantita'=>'4','id'=>'12','descriz'=>'prova4');
$result[] = array('AAMS'=>'1432','quantita'=>'5','id'=>'11','descriz'=>'prova5');

// include('connect.php'); 

//Visualizza merce ordinata nella data selezionata// 
if(isset($_POST['show'])) { 
  $levata = ($_POST['levata']); 
  $_SESSION[levata]=$levata; 

  $form = "<table border='1' cellpadding='10'><tr style='font-weight: bold;'>"; 
  $form.= "<td width='100'>Cod.AAMS</td><td width='100' align='center'>Ricevuto</td><td width='300'>Prodotto</td>"; 
//  $result = mysql_query("SELECT * FROM ordini WHERE data ='$levata'"); 
//  while($row=mysql_fetch_array($result)) { 
  foreach($result as $key => $row) {
    $AAMS=$row['AAMS']; 
    $form.= "<tr><td><input type='hidden' name='AAMS' value='".$AAMS."'>".$AAMS."</td>"; 
    $form.= "<td><input name='quantita' type='hidden' value='".$row['quantita']."'>".$row['quantita']."</td>"; 
    $form.= "<td>"; 
//    $query_nome=mysql_query("SELECT * FROM prodotti WHERE AAMS={$AAMS}")or die(mysql_error()); 
//    while($nome= mysql_fetch_array ($query_nome))
//      $form.= $nome['descriz'];
    $form.= $row['descriz'];
    $form.= "</td>"; 
    $form.= "<td><form name='edit_order' action='".$_PHP_SELF."' method='post'>";
    $form.= "<input type='hidden' name='ordinato_id' value='". $row['id'] ."'>";
    $form.= "<input type='submit' name='edit_order' value='MODIFICA'></form></td>"; 
  } 
} 
else if (isset($_POST['edit_order'])) {		//ho premuto il tasto modifica//
  $levata = $_SESSION[levata]; 
  $mod_id= ($_POST['ordinato_id']); 
  $form = "<table border='1' cellpadding='10'><tr style='font-weight: bold;'>"; 
  $form.= "<td width='100'>Cod.AAMS</td><td width='100' align='center'>Ricevuto</td><td width='300'>Prodotto</td>"; 
//  $result = mysql_query("SELECT * FROM ordini WHERE data ='$levata'"); 
//  while($row=mysql_fetch_array($result)) { 
  foreach($result as $key => $row) {
    $AAMS=$row['AAMS']; 
    $form.= "<tr><td><input type='hidden' name='AAMS' value='".$AAMS."'>".$AAMS."</td>"; 
    if ($row['id'] == $mod_id) { 
      $form.= "<td><input name='quantita'  value='".$row['quantita']."'></td>"; 
    } 
    else { 
      $form.= "<td><input name='quantita'  type='hidden' value='".$row['quantita']."'>".$row['quantita']."</td>"; 
    } 
    $form.= "<td>";
//    $query_nome=mysql_query("SELECT * FROM prodotti WHERE AAMS= {$AAMS}")or die(mysql_error()); 
//    while($nome = mysql_fetch_array ($query_nome))
//      $form.= $nome['descriz'];
    $form.= $row['descriz'];
    $form.= "</td>"; 
    if ($row['id'] == $mod_id) { 
      $form.= "<td><form name='conferma_mod' action='".$_PHP_SELF."' method='post'>";
      $form.= "<input type='submit' name='conferma_mod' value='CONFERMA'></form></td>"; 
    } 
    else { 
      $form.= "<td></td>";
    } 
  } 
} 
else {
  $form = "<form action='malela.php' method='post'>";
  $form.= "Ricevi Merce Ordinata il "; 
  $form.= "<input type='date' value='' name='levata'>";
  $form.= "<input type='submit' name='show'>";
  $form.= "</form>";
}
print $form;
?>
 
GRAZIE!!!
Che stupida che sono! In realtà finora non avevo capito bene la differenza tra '=' e '==' ora mi sono informata (eh si sarebbe stato meglio pensarci prima!).

Ora avrei un altro problema: e se volessi aggiungere una riga? Cioè aggiungere un prodotto che non era stato ordinato ma che invece è arrivato.

Mi sento un po' persa, non so da dove cominciare :hammer:

ciao,
non avendo mysql ho adattato il tuo script, te lo posto in fondo per la curiosità ....
ma adattandolo e provandolo, ho trovato il motivo per cui non lavora bene,

sostituisci le due righe if dove c'è un solo uguale con due ...

PHP:
if ($row['id'] =$mod_id) { 


if ($row['id'] == $mod_id) {

ciao
Marino

PHP:
<?php 
// session_start(); 

# http://localhost/test_site/php/test/malela.php

$_POST['levata'] = '22/03/2014';

$result[] = array('AAMS'=>'1432','quantita'=>'1','id'=>'15','descriz'=>'prova1');
$result[] = array('AAMS'=>'1432','quantita'=>'2','id'=>'14','descriz'=>'prova2');
$result[] = array('AAMS'=>'1432','quantita'=>'3','id'=>'13','descriz'=>'prova3');
$result[] = array('AAMS'=>'1432','quantita'=>'4','id'=>'12','descriz'=>'prova4');
$result[] = array('AAMS'=>'1432','quantita'=>'5','id'=>'11','descriz'=>'prova5');

// include('connect.php'); 

//Visualizza merce ordinata nella data selezionata// 
if(isset($_POST['show'])) { 
  $levata = ($_POST['levata']); 
  $_SESSION[levata]=$levata; 

  $form = "<table border='1' cellpadding='10'><tr style='font-weight: bold;'>"; 
  $form.= "<td width='100'>Cod.AAMS</td><td width='100' align='center'>Ricevuto</td><td width='300'>Prodotto</td>"; 
//  $result = mysql_query("SELECT * FROM ordini WHERE data ='$levata'"); 
//  while($row=mysql_fetch_array($result)) { 
  foreach($result as $key => $row) {
    $AAMS=$row['AAMS']; 
    $form.= "<tr><td><input type='hidden' name='AAMS' value='".$AAMS."'>".$AAMS."</td>"; 
    $form.= "<td><input name='quantita' type='hidden' value='".$row['quantita']."'>".$row['quantita']."</td>"; 
    $form.= "<td>"; 
//    $query_nome=mysql_query("SELECT * FROM prodotti WHERE AAMS={$AAMS}")or die(mysql_error()); 
//    while($nome= mysql_fetch_array ($query_nome))
//      $form.= $nome['descriz'];
    $form.= $row['descriz'];
    $form.= "</td>"; 
    $form.= "<td><form name='edit_order' action='".$_PHP_SELF."' method='post'>";
    $form.= "<input type='hidden' name='ordinato_id' value='". $row['id'] ."'>";
    $form.= "<input type='submit' name='edit_order' value='MODIFICA'></form></td>"; 
  } 
} 
else if (isset($_POST['edit_order'])) {		//ho premuto il tasto modifica//
  $levata = $_SESSION[levata]; 
  $mod_id= ($_POST['ordinato_id']); 
  $form = "<table border='1' cellpadding='10'><tr style='font-weight: bold;'>"; 
  $form.= "<td width='100'>Cod.AAMS</td><td width='100' align='center'>Ricevuto</td><td width='300'>Prodotto</td>"; 
//  $result = mysql_query("SELECT * FROM ordini WHERE data ='$levata'"); 
//  while($row=mysql_fetch_array($result)) { 
  foreach($result as $key => $row) {
    $AAMS=$row['AAMS']; 
    $form.= "<tr><td><input type='hidden' name='AAMS' value='".$AAMS."'>".$AAMS."</td>"; 
    if ($row['id'] == $mod_id) { 
      $form.= "<td><input name='quantita'  value='".$row['quantita']."'></td>"; 
    } 
    else { 
      $form.= "<td><input name='quantita'  type='hidden' value='".$row['quantita']."'>".$row['quantita']."</td>"; 
    } 
    $form.= "<td>";
//    $query_nome=mysql_query("SELECT * FROM prodotti WHERE AAMS= {$AAMS}")or die(mysql_error()); 
//    while($nome = mysql_fetch_array ($query_nome))
//      $form.= $nome['descriz'];
    $form.= $row['descriz'];
    $form.= "</td>"; 
    if ($row['id'] == $mod_id) { 
      $form.= "<td><form name='conferma_mod' action='".$_PHP_SELF."' method='post'>";
      $form.= "<input type='submit' name='conferma_mod' value='CONFERMA'></form></td>"; 
    } 
    else { 
      $form.= "<td></td>";
    } 
  } 
} 
else {
  $form = "<form action='malela.php' method='post'>";
  $form.= "Ricevi Merce Ordinata il "; 
  $form.= "<input type='date' value='' name='levata'>";
  $form.= "<input type='submit' name='show'>";
  $form.= "</form>";
}
print $form;
?>
 
ciao,

dopo il pulsante modifica, puoi inserire il codice simile alla mia prova
(attenzione alle parentesi, deve essere esterno al ciclo while)

PHP:
    $form.= "<input type='submit' name='edit_order' value='MODIFICA'></form></td>";
  } 
  $form.= "<tr><td></td><td></td><td></td>"; 
  $form.= "<td><form name='add_order' action='".$_PHP_SELF."' method='post'>";
  $form.= "<input type='submit' name='add_order' value='AGGIUNGI'></form></td>";
}

poi dove c'é l'istruzione else, intrometti le righe di codice

PHP:
      $form.= "<td></td>";
    } 
  } 
} 
else if (isset($_POST['add_order'])) {		// ho premuto il tasto aggiungi
  echo "qui devo gestire la riga aggiunta";
} 
else {
  $form = "<form action='".$_PHP_SELF."' method='post'>";
  $form.= "Ricevi Merce Ordinata il ";

è sufficiente ?

ciao
Marino
 
Grazie mille,
si si perfetto. Mi serviva giusto la spinta per partire. Inizialmente pensavo di tenere tutto 'sospeso' nelle variabili e aggiornare la tabella magazzino solo dopo il submit, ma credo sia molto meglio (e anche piu' facile da gestire) andare ad aggiornare i dati nella tabella ordini.

Grazie mille ancora!
 

Discussioni simili