2013-04-08 21 views
6

Ho uno script php che sto cercando di ottenere, che ho praticamente estratto da un tutorial e modificato per adattarlo alle mie esigenze. Questo è il mio primo tentativo di php quindi per favore andate piano con me.php update sql dal modulo

Ho 3 file

  1. list_records.php
  2. update.php
  3. update_ac.php

List_records legge i dati da una tabella in mysql. la tabella in list_records ha una funzione di modifica che ti porta ad update.php dove visualizza i dati nella tabella db.

Update.php ha un pulsante di invio che ha lo scopo di aggiornare mysql utilizzando update_ac.php con tutte le informazioni che hai modificato usando il campo id nell'url usando $ _GET ['id].

So che questo script è molto aperto alle iniezioni di slq ma ho intenzione di utilizzarlo solo in un ambiente locale, non sarà esposto a Internet e solo io e un'altra persona userò questa pagina in modo che non sia Davvero un problema.

In ogni caso, ho confermato un paio di cose: -

  1. l'id venga raccolto tramite $ _GET, ho messo in un eco e stampato fuori sulla pagina update.php.
  2. posso eseguire il comando update entro i valori PHP e di cambiamento, ma non funzionerà quando si utilizza $ _GET [id]

Qualcuno mi può punto nella giusta direzione?

qui sono i 3 file con i dettagli della connessione db alterati

list_records.php

<title>Ports</title> 
</head> 

<?php 

// Connect to server and select database. 
mysql_connect("localhost", "username", "passsword")or die("cannot connect"); 
mysql_select_db("porting")or die("cannot select DB"); 


$sql="SELECT * FROM ports"; 
$result=mysql_query($sql); 

?> 
<body> 


<table width="1200" border="1" cellspacing="1" cellpadding="0"> 
<tr> 
<td> 
<table width="1200" border="1" cellspacing="1" cellpadding="3"> 
<tr> 
<td colspan="50"><strong>Pending Port Requests 2</strong> </td> 
</tr> 

<tr> 
<td align="center"><strong>Customer</strong></td> 
<td align="center"><strong>Number</strong></td> 
<td align="center"><strong>Type</strong></td> 
<td align="center"><strong>Completed</strong></td> 
<td align="center"><strong>Update</strong></td> 
</tr> 

<?php 
while($rows=mysql_fetch_array($result)){ 
?> 
<tr> 
<td><?php echo $rows['Customer']; ?></td> 
<td><?php echo $rows['Number']; ?></td> 
<td><?php echo $rows['Type']; ?></td> 
<td><?php echo $rows['Completed']; ?></td> 
<td align="center"><a href="update.php?id=<?php echo $rows['id']; ?>">update</a></td> 
</tr> 

<?php 
} 
?> 

</table> 
</td> 
</tr> 
</table> 
</body> 
</html> 

update.php

<title>update</title> 
</head> 

<?php 
// Connect to server and select database. 
mysql_connect("localhost", "username", "password")or die("cannot connect"); 
mysql_select_db("porting") or die("cannot select DB"); 

// get value of id that sent from address bar 
$id=$_GET['id']; 



// Retrieve data from database 
$sql="SELECT * FROM porting.ports WHERE id = '$id'"; 
$result=mysql_query($sql); 
$rows=mysql_fetch_array($result); 
?> 
<body> 


<table width="1200" border="0" cellspacing="1" cellpadding="0"> 
<tr> 
<form name="form1" method="post" action="update_ac.php"> 
<td> 
<table width="100%" border="0" cellspacing="1" cellpadding="0"> 
<tr> 
<td>&nbsp;</td> 
<td colspan="6"><strong>Update Porting Details</strong> </td> 
</tr> 
<tr> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
</tr> 
<tr> 
<td align="center">&nbsp;</td> 
<td align="center"><strong>Customer</strong></td> 
<td align="center"><strong>Number</strong></td> 
<td align="center"><strong>Type</strong></td> 
<td align="center"><strong>Completed</strong></td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td align="center"> 
<input name="Customer" type="text" id="Customer" value="<?php echo $rows['Customer']; ?>"size= "15"/> 
</td> 
<td align="center"> 
<input name="Number" type="text" id="Number" value="<?php echo $rows['Number']; ?>" size="15"/> 
</td> 
<td align="center"> 
<input name="Type" type="text" id="Type" value="<?php echo $rows['Type']; ?>" size="15"/> 
</td> 
<td align="center"> 
<input name="Comments" type="text" id="Completed" value="<?php echo $rows['Comments']; ?>" size="15"/> 
</td> 
<tr> 
</table> 
<input name="id" type="hidden" id="id" value="<?php echo $rows['id']; ?>"/> 
<input type="submit" name="Submit" value="Submit" /></td> 
<td align="center">&nbsp;</td> 
</td> 
</form> 
</tr> 
</table> 
</body> 
</html> 

update_ac.php

<?php 
// Connect to server and select database. 
mysql_connect("localhost", "username", "password")or die("cannot connect"); 
mysql_select_db("porting")or die("cannot select DB"); 

// update data in mysql database 
$sql="UPDATE ports SET Customer='Customer', Number='Number' WHERE id='id'" or die ("this stuffed up"); 
$result=mysql_query($sql) or die ("this stuffedup"); 


// if successfully updated. 
if($result){ 
echo "Successful"; 
echo "<BR>"; 
echo "<a href='list_records.php'>View result</a>"; 
} 

else { 
echo "ERROR"; 
} 

?> 
+0

stai sempre tenuti a $ file [ 'clienti']; ? –

+1

Il tuo codice ti lascia aperto agli attacchi SQL injection. Guarda il caso di [bobby tables] (http://bobby-tables.com/) per gli esempi. Inoltre, poiché le funzioni di 'mysql_ *' sono deprecate, per favore guarda a usare MySQLi o [PDO] (http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers) – jcbwlkr

risposta

8

tuo aggiornamento regolare interrogazione te dovrebbe essere

// update data in mysql database 
$sql="UPDATE ports SET Customer='".$_POST['Customer']."', Number='".$_POST['Number']."' WHERE id='".$_POST['id']."'"; 

$result=mysql_query($sql)or 
die ("this stuffedup"); 
+1

Questo codice è vulnerabile all'iniezione SQL. – epicdev

+8

@flov hai persino guardato la domanda? ha spiegato che lo userà solo locale e che questo non è un problema .. – Daanvn

0
$sql="UPDATE ports SET Customer='Customer', Number='Number' WHERE id='id'" ; 

questa linea è sbagliato lo si aggiorna con la stringa invece di numero intero. Si dovrebbe mettere valore

$sql="UPDATE ports SET Customer='Customer', Number='Number' WHERE id='".intval($_REQUEST['id'])."'" 
1
1.You have to pass a id when clicking a submit in update.php by 
<a href="update_ac.php?id=<?php echo $rows['id']; ?>"><input type="submit" name="submit" value="Submit"></a>. 

2.The line $id=$_GET['id'] is used in update_ac.php before insert query. 
+1

Grazie Mahendra, che ha funzionato a meraviglia.Grazie a tutti anche per il vostro consiglio, molto apprezzato :) – themoose