2017-01-03 4 views
1

GETメソッドを使用してPHPでレコードを更新したいのですが、「更新ボタン」をクリックしても何も変更されず、ページが更新され、データは同じままです。親切にも、私が間違っていることを教えてください。おかげPHPでフォームを更新するには

mysql_query($sql_query) or die (mysql_error()) ; 

<?php 
include_once"dbconfig.php"; 
if(isset($_GET['edit'])) 
{ 
    $id=$_GET['edit']; 
    echo $id; 
    $sql_query="SELECT * FROM users WHERE user_id='$id' "; 

    $result_set=mysql_query($sql_query); 
    $fetched_row=mysql_fetch_array($result_set); 


} 
if(isset($_POST['btn-update'])) 
{ 
    // variables for input data 
    $first_name=$_POST['first_name']; 
    $last_name=$_POST['last_name']; 
    $city_name=$_POST['city_name']; 

    //sql query to update into database 
    $sql_query="UPDATE users SET first_name='$first_name',last_name='$last_name',user_city='$city_name' WHERE user_id='$id' "; 
    mysql_query($sql_query); 
    //if(!$sql_query) 
     //die('data can not update'.mysql_error()); 
    //else 
     //echo 'data updated successfully'; 


} 
?> 

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> 
<link rel="stylesheet" href="style.css" type="text/css" /> 
</head> 
<body> 
<center> 

<form method="post"> 
<table align="center"> 
<tr> 
<td><input type="text" name="first_name" placeholder="First Name" value="<?php echo $fetched_row['first_name']; ?>"required /> </td> 
</tr> 
<tr> 
<td><input type="text" name="last_name" placeholder="Last Name" value="<?php echo $fetched_row['last_name']; ?>" required /> </td> 
</tr> 
<tr> 
<td><input type="text" name="city_name" placeholder="City" value="<?php echo $fetched_row['user_city']; ?>" required /> </td> 
</tr> 
<tr> 
<td> 
<button type="submit" name="btn-update"><strong>UPDATE</strong></button> 
</td> 
</tr> 
</table> 
</form> 
</center> 
</body> 
</html> 

セカンドファイル

<?php 
include_once"dbconfig.php"; 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
<title>CRUD Operations With php and MySql </title> 
<link rel="stylesheet" href="style.css" type="text/css"/> 
</head> 
<body> 
<center> 

<div id="body"> 
<div id="content"> 
    <table align="center"> 

<table align="center"> 
<tr> 
<th colspan="9"><a href="add_data.php">Add data Here ! </a></th> 
</tr> 
<tr> 
<th colspan="2">FirstName </th> 
<th colspan="8">LastName</th> 
<th colspan="4">CityName </th> 
<th>Operations</th> 

<!--<th colspan="2">Operations</th> --> 
</tr> 

<?php 
$sql_query="SELECT * FROM users"; 
    $result_set=mysql_query($sql_query); 
    while($row=mysql_fetch_row($result_set)) 
    { 
     ?> 
    <tr> 
    <td><?php echo $row[0]; ?> </td> 
    <td colspan="4"><?php echo $row[1]; ?></td> 
    <td colspan="6"><?php echo $row[2];?></td> 
    <td colspan="4"><?php echo $row[3];?></td> 
    <td> <a href='edit_data.php?edit=$row[0]'>EDIT</a> </td> 
    <!--<td align="center"><a href="javascript edt_id('<//?php echo $row[0]; ?><img src="b_edit.png" align="EDIT" /></a></td> 
    <td align="center"><a href="javascript:delete_id('<//?php echo $row[0];?><img src="b_drop.png" align="DELETE" /></a></td> 
    </tr> --> 
    <?php 

    } 
    ?> 

    </table> 
    </div> 
    </div> 
    </center> 
    </body> 
    </html> 
+0

たびに、使用[ 'mysql_'](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) データベース拡張** [子猫は世界のどこかに絞め殺されている](http://bb.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)**これは廃止され、何年も続いているPHP7でこれまでになくなってしまっています。 PHPを学んでいるだけの方は、 'PDO'または' mysqli'データベースの拡張機能を学んでください。 [ここから] – RiggsFolly

+0

​​EDIT 'このようなPHP変数を埋め込むことはできません。 –

+0

あなたのスクリプトは[SQLインジェクション攻撃]の危険にさらされています(http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) [リトルボビーテーブル](http://bobby-tables.com/)でも [入力をエスケープしている場合、その安全ではありません!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets -around-mysql-real-escape-string) [用意されたパラメータ化された文](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly

答えて

0

変更

mysql_query($sql_query); 

あなたは(フォームアクションプロパティがMISSIあるIDを送信していません新しいコード でNG

+0

edit_data.php?edit = $ row [0] 'は正しい行です。私はIDが送信していないことを知っています –

関連する問題