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

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!
 

marino51

Utente Attivo
28 Feb 2013
3.204
207
63
Lombardia
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 .......
 

malela

Nuovo Utente
20 Mar 2014
5
0
0
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.
 

marino51

Utente Attivo
28 Feb 2013
3.204
207
63
Lombardia
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;
?>
 

malela

Nuovo Utente
20 Mar 2014
5
0
0
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;
?>
 

marino51

Utente Attivo
28 Feb 2013
3.204
207
63
Lombardia
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
 

malela

Nuovo Utente
20 Mar 2014
5
0
0
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
Autore Titolo Forum Risposte Data
D Miglior modo per estrarre le occorrenze di un elemento in un set di più file xml e quindi scrivere il risultato in una tabella Excel o magari in JSON XML 0
M Estrarre soltanto i prodotti con stessa descrizione PHP 10
peppe0703 Come Estrarre dati da db wordpress e richiamarli in html esterno HTML e CSS 0
M PHP/MySQL - Estrarre valori min e max di ogni gruppo PHP 5
E Estrarre dati da doppia tabella, banale ma non sempre PHP 1
M Estrarre valore con SELECT COUNT PHP 0
D Estrarre database con link esterno Database 10
S Raggruppare ed estrarre data più recente MySQL 6
F Estrarre ultima foto da cartelle PHP 2
R Estrarre valori duplicati più volte PHP 0
C Estrarre dati stringa Sql Database 1
L estrarre valori max tra più tabelle MySQL 2
L Estrarre da Cartella una determinata 'via' PHP 15
J estrarre url dei file video da youtube "get_video_info" PHP 6
M Estrarre dati da un database PHP 2
M Estrarre valori MAX da un db con una left join MySQL 8
M Estrarre dati da una select HTML PHP 3
C [PHP] Estrarre da una classe i valori che mi interessano PHP 5
S [PHP] Estrarre dati da tabella e fare la media ad intervalli di tempo PHP 11
R [PHP] Estrarre id utente loggato.? PHP 4
S [PHP] Estrarre elementi array su più variabili PHP 5
S [PHP] Estrarre dati tabella in diversi array PHP 2
P [MS ACCESS] Estrarre più somme da una query MS Access 4
trattorino estrarre titolo video facebook in php PHP 0
D [WordPress] come fare per estrarre dati da un db MySQL tramite una form e visualizzare il risultato WordPress 0
S [Javascript] come estrarre valori array e inserirli in diverse variabili Javascript 1
N [PHP] Estrarre singolo valore da array PHP 4
trattorino [PHP] sql estrarre in base all'ultima visita PHP 4
P [Visual Basic] access sql estrarre id vendite x cli e articolo con data maggiore del recordset Visual Basic 2
Jensen [PHP] Estrarre prossimi 4 lunedì PHP 4
trattorino [PHP] estrarre dati singoli PHP 1
G [PHP] estrarre data da timeline yyyy-dd-gg hh:mm:ss PHP 4
Z Estrarre dati da un DB Mysql PHP 4
I database mysql estrarre due tabelle Database 2
S [PHP] Estrarre dati colonna in una stringa PHP 6
S [PHP] estrarre dati sito web protetto con username e password PHP 13
Merlina3377 [PHP] estrarre solo un determinato id da tanti con REGEXP O LIKE PHP 1
Gigi87 estrarre i dati da un forum o social network Presentati al Forum 1
trattorino Estrarre Nome Utente jquery div php PHP 9
trattorino [Javascript] estrarre id link yt Javascript 1
S [PHP] estrarre le email da un elenco di url PHP 21
T4MAR4 [PHP] estrarre tag da un testo PHP 3
T [PHP] Estrarre stringa PHP 3
G [PHP] ESTRARRE DA DB VALORE MASSIMO E MINIMO IN UN INTERVALLO IMPOSTABILE A PIACERE PHP 56
bubino8 [PHP] Estrarre dominio No Sottodomini PHP 10
L [PHP] Estrarre dati da sito web Offerte e Richieste di Lavoro e/o Collaborazione 4
sandropochi [PHP] Query per estrarre record con data successiva a quella odierna PHP 2
gandalf1959 Estrarre da un db mysql le mail, eliminando le doppie PHP 6
giancadeejay [PHP] Estrarre solo record dell'utente connesso PHP 4
U [PHP] estrarre i risultati di una query e visualizzarli in una tabella. PHP 9

Discussioni simili