2017-03-17 85 views
0

我有一個表單併成功將它們連接到數據庫。PHP ODBC:更新表格

現在我試圖更新數據。不幸的是,當我點擊提交按鈕時,什麼也沒發生。我確定我錯過了一些東西。請幫助我,謝謝。

的config.php:

<?php 

$conn=odbc_connect("dsn", "", ""); 

if (!$conn) 
{ 
exit("Connection Failed : " . $conn); 
} 

?> 

這是我的代碼:

<?php 

include "config.php"; 

ini_set('error_reporting', E_ALL); 

error_reporting(-1); 

$sql = odbc_exec($conn, "SELECT 

     UserId, 
     UserName, 
     UserEmail 

     FROM DBA.tblUser 
     WHERE UserId='".$_GET['UserId']."'"); 

if(isset($_POST['submit'])) 
{ 
    $UserId=$_POST["UserId"]; 
    $UserName=$_POST["UserName"]; 
    $UserEmail=$_POST["UserEmail"]; 

    //UPDATE 

    $stmt = odbc_exec( $conn, 
    "UPDATE DBA.tableUsers SET 
    UserName = '$UserName', 
    UserEmail ='$UserEmail' 

    WHERE UserId=$UserId"); 

    if ($stmt) { 

     echo "Update Success"; 
     echo $UserName; 
     echo $UserEmail; 

    } else { 

     "Error : " . odbc_errormsg(); 
    } 
} 

?> 

形式:

<form class="form" method="post"> 

    <tr> 
    <td class = "userid">User ID</td> 
    <td><?php echo $UserId = odbc_result($sql,'UserId'); ?></td> 
    </tr> 


    <tr> 
    <td class = "name">User Name<span class="required">&nbsp; * &nbsp;</span></td> 
    <td><input type="text" name="UserName" value="<?php echo $UserName = odbc_result($sql,'UserName'); ?>"></td> 
    </tr> 

    <tr> 
    <td class = "email">Email<span class="required">&nbsp; * &nbsp;</span></td> 
    <td><input type="text" name="UserEmail" value="<?php echo $UserEmail = odbc_result($sql,'UserEmail'); ?>"></td> 
    </tr> 

    <button name="submit" type="submit" value ="submit" >Update</button> 

</form> 
+0

你如何連接到數據庫'$ conn'?你發佈的表單是什麼?你如何定義你的$ UserId變量?並且你定義了'$ UserNm'變量,然後在你的查詢中使用它作爲'$ UserName' – hassan

+0

我已經更新了我的文章@hassan – Far

+0

關閉你的表單標籤'' – hassan

回答

0

我設法用相同的概念和可變答案。

的config.php:

<?php 

$conn=odbc_connect("dsn", "", ""); 

if ($conn) { 

echo "Connected"; 

} 

if (!$conn) { 

exit("Connection Failed : " . $conn); 

} 

?> 

代碼:

<?php 

include "config.php"; 

$sql = odbc_exec($conn, "SELECT 

     UserId, 
     UserName, 
     UserEmail 

     FROM DBA.tblUser 
     WHERE UserId='".$_GET['UserId']."'"); 

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

     $UserName=$_POST["UserName"]; 
     $UserEmail=$_POST["UserEmail"]; 

     $stmt = odbc_exec($conn, 
     "UPDATE DBA.tblUser SET 
     UserName = '$UserName', 
     UserEmail ='$UserEmail' 

     WHERE UserId='".$_GET['UserId']."'"); 

if ($stmt) { 

     echo "Update Successfull"; 
     echo $UserName; 
     echo $UserEmail; 
} 

else { 

     "Error : " . odbc_errormsg(); 
} 
} 

?> 

HTML:

<!DOCTYPE html> 
<html> 
<head> 
<title>Administration</title> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<link rel="stylesheet" type="text/css" href="css/userpage.css" media="screen"/> 

<body> 

    <content> 

    <div class="user-form"> 

    <div class="user-form-heading">Update User</div><br> 

    <form class="form" method="post"> 

    <h><table class ="user"> 


<!-- .................................. Updatable ....................................... --> 
<tr> 
<td class = "name">User Name<span class="required">&nbsp; * &nbsp;</span></td> 
<td><input type="text" name="UserName" value="<?php echo $UserName = odbc_result($sql,'UserName'); ?>"></td> 
</tr> 

<tr> 
<td class = "userid">User Id</td> 
<td><?php echo $UserId = odbc_result($sql,'UserId'); ?></td> 
</tr> 

<tr> 
<td class = "email">Email<span class="required">&nbsp; * &nbsp;</span></td> 
<td><input type="text" name="UserEmail" value="<?php echo $UserEmail = odbc_result($sql,'UserEmail'); ?>"></td> 
</tr> 
<tr> 
<td> 
<f><button name="submit" type="submit" value ="submit" >UPDATE</button></f> 
&nbsp;&nbsp;&nbsp; 

</table> 
</form> 
</div> 
</content> 
</body> 
</head> 
</html> 
1

更換

$UserName=$_POST["UserName"]; 

With

$UserName=$_POST["UserId"]; 

爲什麼?很簡單,因爲在你的表格,你已經命名爲UserId,而不是UserName

<input type="text" name="UserId" value="<?php echo $UserName = odbc_result($sql,'UserName'); ?>"></td> 
+0

@Far Upvote如果它幫助:) –

+0

它的確如此。但我仍然無法執行更新:( – Far

+0

@Far你怎麼知道你已經成功連接到數據庫?按照這個來設置ODBC http://w3schools.sinsixx.com/php/php_db_odbc.asp.htm –