2011-06-01 34 views
0

我有一個問題,我是PHP新手,我一直在做一些練習。我目前的工作是創建一個簡單的表單來搜索數據庫(名字,姓氏)。返回的結果應該填充到另一個表單中。這樣,如果我想更新記錄,我所要做的就是更改填充表單的值並點擊更新。我創建數據庫沒有問題。PHP,關於搜索和更新記錄的問題

以下是代碼。 (大家不要笑,我很新...我相信有這樣做的更有效的方式,但我只是玩弄現在)

這裏是形式:

<form action="" method="post"> 
<strong>Search for name</strong><br> 
<label for="fname">First Name</label> 
<input type="text" name="fname"> 

<label for="lname">Last Name</label> 
<input type="text" name="lname"> 

<input type="submit" name="submit" value="Search"> 

</form> 

這裏是PHP:

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

    $first_name = $_POST['fname']; 
    $last_name = $_POST['lname']; 

    if ($first_name == NULL || $last_name == NULL) { 
     echo "please enter search record"; 
    } 
    else { 
     $query = "SELECT first_name, last_name FROM formdata WHERE first_name LIKE '%$first_name%' OR last_name LIKE '%$last_name%'"; 

     $result = mysqli_query($conn, $query); 

     $result_array = mysqli_fetch_row($result); 

     $fname_value = $result_array[0]; 
     $lname_value = $result_array[1]; 

     echo " 
     <form action='' method='post'>\n 
      <label for='fname_u'>First Name</label>\n 
      <input type='text' name='fname_u' value='$fname_value'>\n 

      <label for='lname_u'>Last Name</label>\n 
      <input type='text' name='lname_u' value='$lname_value'>\n 

      <input type='submit' name='update' value='Update'>\n 
     </form>"; 

    } 

} 


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

    $first_name_u = ($_POST['fname_u']); 
    $last_name_u = ($_POST['lname_u']); 

    $query_update = "UPDATE formdata SET first_name = '$first_name_u', last_name = '$last_name_u' WHERE first_name = '$fname_value';"; 

    echo $query_update; // this is just for testing 

} 

此代碼似乎工作,做我想做的,一路攀升時,我提交更新的信息。我無法弄清楚如何將$ fname_value變量的值傳遞給if(isset($ _POST ['update']))條件。我想我不能,因爲他們是兩個不同的POSTS?我真的不知道......我只需要找到一種方法來獲取檢索到的表單數據的值並將其用於WHERE子句。

同樣,我很新的,剛開始我的腳溼用這種東西...任何幫助將是巨大的

感謝

+0

我們都是新的一次。任何看着代碼和笑的人都會傲慢到危險的地步。歡迎來到網站設計的酷炫世界,祝您學習愉快! – 2011-06-01 03:43:58

回答

1

我認爲你必須在你的代碼一個錯字。您的POST數據保存到變量$first_name,但是當您查詢SQL時,您正在使用$first_name_r而不是$first_name

+0

嘿,感謝您的快速回復......對不起,因爲我正在將代碼複製到此線程中,這是一個錯字。我在編輯器中命名了一些不同的變量,並且在我將其粘貼到這裏時忘了更改它。我更新了上面的代碼,並且我確信所有名稱在我的末尾都拼寫正確...對於混淆 – kdub 2011-06-01 03:30:03

0

我在想錯字是答案,但我必須指出你犯的一個致命錯誤,那就是你將用戶提供的輸入直接輸入到SQL查詢中。這會將您的代碼打開到一系列稱爲SQL injection攻擊的惡意攻擊。我並不想說服自己,但它很重要,因爲你閱讀並理解那篇文章,尤其是關於緩解的部分。

我會建議你使用這樣的事情,而不是:

$query = 'SELECT first_name, last_name '. 
    'FROM formdata WHERE first_name LIKE ? OR last_name LIKE ?;'; 
$sth = mysqli_prepare($dbh, $query); 
mysqli_stmt_bind_param($sth, "s", '%'.$first_name.'%'); 
mysqli_stmt_bind_param($sth, "s", '%'.$last_name.'%'); 
$result = mysqli_execute($sth); 

我知道這是一個有點更長,更復雜,但相信我,它會爲你節省世界頭痛的。你越早了解這一點,並深深根植於你的心理,你永遠不會有,有史以來寫一個查詢,通過unsanitized輸入直接到數據庫,越快樂,我們都會(越久,你會得到最終保持你的工作;)。

對不起,如果我來強壯,但在我看來,你需要早日開發數據庫驅動的網站的最重要的一課,是你真的需要精通發現注入漏洞到指出它是自動的,當你看到它時,你會想:「哦,不!不要那樣做!!!」

+0

哦,不,請欣賞非常多的反饋....這只是一個簡單的練習練習 – kdub 2011-06-01 03:59:30

+0

哦,對不起,另外,錯字不是問題...我將代碼複製到此線程並開始更改變量名稱,我只是在提交 – kdub 2011-06-01 04:01:23

0

以上回答您發現isssue,但在旁註:

$first_name = $_POST['fname']; 
$last_name = $_POST['lname']; 

不要這樣。我的朋友是PHP應用程序最常見的安全漏洞。

最2周最重要的事情要記住,當腳本是

  1. 濾波器的輸入,並 2:逃離輸出。

See this write-up for more details ..

在你的情況下,輸入值不被過濾,或檢查惡意/不當值。

這裏是另一本關於this page的入門書,您將看到一些提示以及如何解決過濾問題。

保持它,這些練習是拾取排骨的好方法。

快樂編碼的朋友。

+0

之前沒有更改它們,非常感謝,我很感激反饋意見......就像我上面發佈的那樣,對於混淆,感到抱歉。我的代碼中不存在拼寫錯誤。只在這篇文章中。我將我的代碼複製到帖子中,並開始更改變量名稱,在提交我的問題之前,我只是沒有更改它們... – kdub 2011-06-01 04:02:50

0

好的,重新閱讀你的文章,我想我明白你想要做什麼以及你有什麼困難。通常情況下,您不會有像這樣的相同表單的兩個單獨的頁面。通常情況下,您會按照這些方式編寫更多代碼,請記住,我正在將其從頭頂轉過來,而不是實際測試它,因此可能需要進行較小的更正和/或調整:

<?php 
    $fname_value = ''; 
    $lname_value = ''; 
    if (isset($_POST['submit']) && $_POST['submit'] === 'Search') { 
    if (isset($_POST['fname']) && isset($_POST['lname'])) { 
     // We are processing a submitted form, not displaying a brand new one 
     // from scratch. Code any post-validation steps. 

     // Fetch the user information from the database. You'll need to define 
     // the $host, $user, $password, and $dbname variables above, or 
     // substitute literal strings with real information in here. 
     $dbh = new mysqli($host, $user, $password, $dbname); 
     $sql = 'SELECT first_name, last_name'. 
     'FROM formdata WHERE first_name LIKE ? OR last_name LIKE ?;'; 
     $sth = $dbh->prepare($sql); // Use parameters to avoid injection! 
     $sth->bind_param('s', $_POST['fname']); 
     $sth->bind_param('s', $_POST['lname']); 
     if ($sth->execute()) { 
     $result = $sth->get_result(); 
     if (($row = $result->fetch_assoc()) != NULL) { 
      // Set the default values displayed in the text edit fields. 
      $fname_value = $row['first_name']; 
      $lname_value = $row['last_name']; 
     } 
     } 

     // Whatever other processing you want to do if this is a submitted 
     // form instead of displaying the page from scratch. 
    } 
    } 
?> 
<html> 
    <body> 
    <form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST"> 
     <strong>Search for name</strong><br /> 
     <label for="fname">First Name</label> 
     <input type="text" name="fname" value="<?= htmlentities($fname_value) ?>"> 
     <label for="lname">Last Name</label> 
     <input type="text" name="lname" value="<?= htmlentities($lname_value) ?>"> 
     <input type="submit" name="submit" value="Search"> 
    </form> 
    </body> 
</html> 
+0

嘿,非常感謝代碼...我真的很感激它....讓我問你...使用mysqli_connect()函數和創建mysqli類的新實例有什麼區別。 – kdub 2011-06-02 00:52:54

+0

真的沒有一個。使用mysqli類是面向對象的編程,其中使用mysqli_函數更具有程序性。他們都做同樣的事情。它真的歸結爲你更喜歡哪個。就我個人而言,我更喜歡OO類,因爲它可以節省一些打字量。 :) – 2011-06-02 02:27:12