2016-11-05 75 views
0

我試圖創建一個表單,允許您使用php更新數據庫表。 我對PHP很陌生,所以如果我在代碼中犯了一個愚蠢的錯誤,請原諒我。使用HTML表單更新MySQL

這是我的edit.php代碼:

<html> 
<head> 
</head> 
<body> 

<?php 

$con=mysqli_connect("localhost","root","root","test"); 
// Check connection 
if (mysqli_connect_errno()) 
{ 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

$result = mysqli_query($con,"SELECT * FROM cats"); 

?> 


<form method="post" action="<?php $_PHP_SELF ?>"> 
<table width="400" border="0" cellspacing="1" cellpadding="2"> 
<tr> 

<?php 

while($row = mysqli_fetch_array($result)) 
      { 

$name = $row['name']; 
$email = $row['email']; 
$rank = $row['rank']; 
$birth = $row['birth']; 
$joined = $row['joined']; 
$steamid = $row['steamid']; 
?> 


<td width="100"></td> 
<td><?=$name?></td> 
</tr> 
<tr> 
<td width="100">Email</td> 
<td><input name="emailid" type="text" value="<?=$email?>"></td> 
</tr> 
<tr> 
<td width="100">Rank</td> 
<td><input name="rankid" type="text" value="<?=$rank?>"></td> 
</tr> 
<tr> 
<td width="100">Birth</td> 
<td><input name="birthid" type="text" value="<?=$birth?>"></td> 
</tr> 
<tr> 
<td width="100">Joined</td> 
<td><input name="joinedid" type="text" value="<?=$joined?>"></td> 
</tr> 
<tr> 
<td width="100">Steamid</td> 
<td><input name="steamidid" type="text" value="<?=$steamid?>"></td> 
</tr> 
<?php } ?> 
<tr> 
<td width="100"> </td> 
<td> </td> 
</tr> 
<tr> 
<td width="100"> </td> 
<td> 
<input name="update" type="submit" id="update" value="Update"> 
</td> 
</tr> 
</table> 
</form> 



<?php 

if(isset($_POST['update'])) 
{ 

$name = $row['nameid']; 
$email = $row['emailid']; 
$rank = $row['rankid']; 
$birth = $row['birthid']; 
$joined = $row['joinedid']; 
$steamid = $row['steamidid']; 

$update = mysqli_query($con,"UPDATE cats SET email = '$email', rank = '$rank', birth = '$birth', joined = '$joined', steamid = '$steamid' WHERE name = '$name';"); 

$retval = mysqli_query($con,"UPDATE cats SET email = '$email', rank = '$rank', birth = '$birth', joined = '$joined', steamid = '$steamid' WHERE name = '$name';"); 
if (!$update) { 
    echo "Could not update data: " . mysqli_error($con); 
} 
echo "Updated data successfully\n"; 

} 
mysqli_close($con); 

?> 
</body> 
</html> 

它顯示錶和信息,但更新不工作。

Updated data successfully 

我檢查了數據庫,但沒有更新任何東西。

+0

「更新數據成功」消息不是有條件的,所以無論何時提交表單,它都會打印。用「其他」包裝它。 – Terry

+0

現在看,你錯了更新你的數據。 –

+0

我已經更新了我的答案。 –

回答

0

信息:

隨着mysqli_error()你需要寫你想要的連接出現錯誤,就像這樣:

mysqli_error($con); 

隨着mysqli_query()你需要給兩個參數,connectionquery像這個:

$update = mysqli_query($con,"UPDATE cats SET email = '$email', rank = '$rank', birth = '$birth', joined = '$joined', steamid = '$steamid' WHERE name = '$name';"); 

如何調試:

如果你想檢查UPDATE查詢返回,你可以做這樣的事情的錯誤:

if (!$update) { 
    echo "Could not update data: " . mysqli_error($con); 
} 

你可以嘗試這樣的事情來調試查詢:

$sql = "UPDATE cats SET email = '$email', rank = '$rank', birth = '$birth', joined = '$joined', steamid = '$steamid' WHERE name = '$name';"; 
echo $sql; // this output write in your phpMyadmin to check if there are any errors. 
$update = mysqli_query($con, $sql); 

其他問題,我們得到:

1.我想想也是,你應該在你的代碼有else,f.ex:

if (!$update) { 
    echo "Could not update data: " . mysqli_error($con); 
} else { 
    echo "Updated data successfully\n"; 
} 

2.你是不是從$_POST獲取數據就應該是這樣的:

$name = $_POST['nameid']; // not $row['nameid'] 
$email = $_POST['emailid']; 
$rank = $_POST['rankid']; 
$birth = $_POST['birthid']; 
$joined = $_POST['joinedid']; 
$steamid = $_POST['steamidid']; 

更多關於使用的功能:

PHP: mysqli::$error

PHP: mysqli::query

在你的情況下,它是程序風格

+0

謝謝,但它還沒有完全working.I已更新的代碼。 –

+0

它在數據庫中更新嗎? –

+0

不,它不更新數據庫中的任何東西 –

0

嘗試使用PHP PDO訪問數據庫的功能,因爲它代表你的代碼容易受到SQL注入! PDO還將使調試和數據庫的工作更容易。

我認爲您的$ _POST中的「更新」檢查不起作用,因爲更新不是表單中的字段,而是提交按鈕本身,請嘗試檢查其中一個字段。

1

親愛的我想你更改基於名稱的記錄,因爲你可以使用where子句中$名字,你也可以改變名稱比從未真正where子句讓你的查詢執行上的任何成功,但不影響該行。

你想得到可編輯的記錄,這是唯一的ID基地更新行,它將反抗工作。