Mam skrypt php, który próbuję uzyskać, a który po prostu wyciągnąłem z samouczka i zmieniono tak, aby odpowiadał moim potrzebom. To jest moja pierwsza próba php, więc proszę, nie przejmuj się.Aktualizacja php sql z formularza
mam 3 pliki
- list_records.php
- update.php
- update_ac.php
List_records odczytuje dane z tabeli w mysql. tabela w list_records posiada funkcję edycji, która przenosi cię do update.php, gdzie wyświetla dane w tabeli db.
Update.php ma przycisk wysyłania, który ma na celu aktualizację mysql przy użyciu update_ac.php przy każdej zmianie informacji za pomocą pola id w adresie URL za pomocą $ _GET ['id].
Wiem, że ten skrypt jest bardzo otwarty na zastrzyki slq, ale mam zamiar używać tego tylko w środowisku lokalnym, nie będzie on narażony na działanie Internetu i tylko ja i jedna inna osoba będzie korzystała z tej strony, więc jej nie naprawdę problem.
W każdym razie, mam potwierdzone kilka rzeczy: -
- id nie odebrano przy użyciu $ _GET, i umieścić w echa i drukowane go na stronie update.php.
- mogę uruchomić polecenie aktualizacji wartości w PHP i zmienić, ale to nie będzie działać przy użyciu $ _GET [id]
Czy ktoś może wskazać mi w dobrym kierunku?
tutaj są 3 pliki o szczegółach połączenia db zmienionych
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";
}
?>
ty wymagane uzyskanie wartości w $ wiersze [ 'Klient']; ? –
Twój kod pozostawia cię otwartym na ataki SQL injection. Spójrzmy na przykład na [bobby tables] (http://bobby-tables.com/). Ponadto, ponieważ funkcje 'mysql_ *' są przestarzałe, spójrz na użycie MySQLi lub [PDO] (http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers) – jcbwlkr