私は、基本的にチュートリアルから引っ張ってきて、自分のニーズに合わせて変更した、機能させようとしているphpスクリプトを持っています。初めてのphpなのでお手柔らかにお願いします。
私は3つのファイルを持っています
- list_records.php
- update.php
- update_ac.php
List_records は、mysql のテーブルからデータを読み取ります。list_records のテーブルには、db テーブルにデータを表示する update.php に移動する編集機能があります。
Update.php には、$_GET['id] を使用して URL の id フィールドを使用して変更した情報で update_ac.php を使用して mysql を更新する送信ボタンがあります。
このスクリプトは slq インジェクションに対して非常にオープンであることは知っていますが、これをローカル環境でのみ使用することを計画しています。インターネットに公開されることはなく、自分と他の 1 人だけがこのページを使用するため、実際には問題ではありません。
とにかく、私はいくつかのことを確認しました:-
- ID は $_Get を使用して取得されます。エコーを入力すると、update.php ページに出力されます。
- PHP 内で更新コマンドを実行して値を変更することはできますが、$_GET[id] を使用すると機能しません。
誰かが私を正しい方向に向けることができますか?
データベース接続の詳細が変更された3つのファイルは次のとおりです
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";
}
?>