2013-08-25 31 views
0

您好,我在查詢中遇到問題。我想在一個特定的特定ID的SQL表中插入一個值。 詳細地說,有一個表,每列都有一個文本字段,並且每個文本字段的值必須插入到SQL表的特定行中。 uid是表'候選'的外鍵,它是包含每個用戶詳細信息的表的主鍵。 代碼如下:無法在特定ID上插入來自texfield的值

if(isset($_REQUEST['uid']) && $_REQUEST['uid'] && isset($_POST['add'])) 
    {  
    $comments_manager =($_POST['comments_manager']); 
    $uid = $_REQUEST['uid']; 
    //$comments_manager = mysql_real_escape_string($_POST['comments_manager']); 



$sql = "INSERT INTO candidate (comments_manager) 
     VALUES ('$comments_manager') 
     WHERE uid = ".intval($uid) 
     ; 
     $query = mysql_query($sql); 
     if(!$query){ 

      die('Invalid query:' . mysql_error()); 
     } 
      else{ 
      header('Location: ../manager/test3.php'); 

這是一張表格

   print "<td>"; 
       print '<form action="" method="POST">'; 
       print '<input type="hidden" name="uid" value='.$row['uid'].' />'; 
       print '<input type="text" name="comments_manager" value='.$comments_manager.' />'; 
       print '<br><input type="submit" name="add" value="add">'; 
            print '</form>'; 
            print "</td>"; 

` 的列當我提交的文本框的值取此消息查詢無效:您有一個錯誤你的SQL語法;檢查對應於你的MySQL服務器版本的手冊。在第2行'WHERE uid = 63'附近使用正確的語法我的代碼有什麼問題?

回答

0

INSERT用於創建新行,不更新現有行,所以WHERE子句沒有意義。要修改現有的行,請使用UPDATE

$sql = "UPDATE candidate 
     SET comments_manager = '$comments_manager' 
     WHERE uid = " . intval($uid); 
+0

好吧,我沒有意識到差異!謝謝!!你幫我解決了一個我無法忍受的問題!謝謝 – user2703341