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
- list_records.php
- update.php
- 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: -
- l'id venga raccolto tramite $ _GET, ho messo in un eco e stampato fuori sulla pagina update.php.
- 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> </td>
<td colspan="6"><strong>Update Porting Details</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </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> </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"> </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";
}
?>
stai sempre tenuti a $ file [ 'clienti']; ? –
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