2013-09-27 33 views
0

我真的希望你能幫我解決我的問題。我的表格已經完成,所有的按鈕都可以工作,而且所有的按鈕都很豪華,但是今天我決定在桌子上增加一個額外的列。列被稱爲「國家」。現在,我的所有按鈕都還在工作,數據被添加或更新沒有問題,除了那一列。什麼都沒有去,但我可以從該列檢索數據沒有麻煩。不知道我在哪裏搞砸了。這是我第一次接觸到php和mysql,所以如果我犯了一些愚蠢的錯誤,請原諒我。你能幫我嗎?無法通過html格式更新Mysql數據庫中的一列

<?php 

    //adding functions 
    function initVars() { 
     //declaring variables 
    global $ID, $First_name, $Last_name, $Address,$Country, 
        $Ph_number,$add,$find,$reset, $delete,  
        $update, $next, $previous; 

    $ID=""; 
    $First_name = ""; 
    $Last_name=""; 
    $Address = ""; 
    $Country=""; 
    $Ph_number=""; 
    $add = ""; 
    $find = ""; 
    $reset=""; 
    $delete=""; 
    $update=""; 
    $next=""; 
    $previous=""; 
    } 

    initVars(); 

    function getRecord($query,$resultOffset){ 

    global $ID, $First_name, $Last_name, $Address,$Country, $Ph_number; 
    $result = mysql_query($query); 
    $resultCount = mysql_num_rows($result); 
     echo "FOUND ", $resultCount, " RESULTS";//3 
     $r=0; 
     while ($r <= $resultOffset) { 
       $rec = mysql_fetch_assoc($result); 
      $r = $r + 1; 
      } 
     $ID=$rec['ID']; 
     $First_name = $rec['First_name']; 
     $Last_name = $rec['Last_name']; 
     $Address = $rec['Address']; 
     $Country= $rec['Country']; 
     $Ph_number = $rec['Ph_number']; 
     return($resultCount); 
    } 
    // collecting form input 
    if (isset($_POST['First_name'])) $First_name = $_POST['First_name']; 
    if (isset($_POST['Last_name'])) $Last_name = $_POST['Last_name']; 
    if (isset($_POST['Address'])) $Address = $_POST['Address']; 
    if (isset($_POST['Country'])) $$Country = $_POST['Country']; 
    if (isset($_POST['Ph_number'])) $Ph_number = $_POST['Ph_number']; 
    if (isset($_POST['add'])) $add = $_POST['add']; 
    if (isset($_POST['find'])) $find = $_POST['find']; 
    if (isset($_POST['reset'])) $reset = $_POST['reset']; 
    if (isset($_POST['delete'])) $delete = $_POST['delete']; 
    if (isset($_POST['update'])) $update = $_POST['update']; 
    if (isset($_POST['ID'])) $ID = $_POST['ID']; 
    if (isset($_POST['next'])) $next = $_POST['next']; 
    if (isset($_POST['previous'])) $previous = $_POST['previous']; 

    if (isset($_POST['resultOffset'])) $resultOffset = $_POST['resultOffset']; 
    if (isset($_POST['resultCount'])) $resultCount = $_POST['resultCount']; 
    if (isset($_POST['query'])) $query = $_POST['query']; 


    // connecting to database 
      $connection = mysql_connect('localhost', 'root', '*******') 
      or die('Connection to MySql server failed'); 
      $db = mysql_select_db('myaddressbook', $connection) 
     or die('Cannot connect'); 


    ////////////////////////////////////////////////////////////////////////////// 

    //BUTTONS 

    if ($add) { 
     if($First_name && $Last_name) { 
     echo 'Adding to database'; 

     // adding records to database 
     $ins = mysql_query("INSERT INTO people SET First_name='$First_name',  
       Last_name='$Last_name', 
     Address='$Address', Country='$Country',Ph_number='$Ph_number'"); 

     if($ins) echo "Your address book was updated succesfully"; 
     else echo "Adding data failed"; 
     initVars(); 

    } 
    else echo 'First and last name needed'; 

    } 
    //////////////////////////////////////////////////////////////////////// 
    if ($find) { 
     if($First_name || $Last_name) { 

    $query = " SELECT ID, First_name, Last_name, Address , Ph_number FROM people WHERE 
    First_name LIKE '%{$First_name}%'"; 
    $result = mysql_query($query);//1 
    $resultCount = mysql_num_rows($result);//2 
    $resultOffset= 0; 
    echo "FOUND ", $resultCount, " RESULTS";//3 

    $rows = mysql_fetch_array($result, MYSQL_ASSOC); 

    $ID=$rows['ID']; 
    $First_name = $rows['First_name']; 
    $Last_name = $rows['Last_name']; 
    $Address = $rows['Address']; 
    $Country= $rows['Country']; 
    $Ph_number = $rows['Ph_number']; 

    } 
    else echo "Enter required field"; 
    } 

    ///////////////////////////////////////////////////////////////// 

    if ($delete){ 

    mysql_query(" DELETE FROM people WHERE ID = '{$ID}' "); 

    echo('Record deleted'); 

    } 

    ////////////////////////////////////////////////////////////////////// 
    if ($update){ 

     mysql_query ("UPDATE people 
     SET First_name='$First_name', 
     Last_name='$Last_name',Address='$Address',Country='$Country', Ph_number='$Ph_number' 
     WHERE ID = '{$ID}'"); 
       echo ('Update succesfull'); 
    } 


    ///////////////////////////////////////////////////// 
    if ($reset){ 
    initVars(); 
    } 
    ///////////////////////////////////////////////////////////// 
    if($next){ 
    if ($resultOffset < $resultCount - 1){ 
     $resultOffset= $resultOffset + 1; 

     $resultCount=getRecord($query,$resultOffset); 

    } 
    echo "next ", $resultOffset, " of ", $resultCount; 
    } 

    if ($previous){ 
    if ($resultOffset > 0){ 
     $resultOffset= $resultOffset - 1; 

     $resultCount=getRecord($query,$resultOffset); 

    } 
    echo "prev ", $resultOffset, " of ", $resultCount; 

    } 


    mysql_close($connection); 
    ?> 

And my piece of HTML: 

    <html> 
    <head><h1 align="center"> 
    My Address Book </h1> 
    <hr></br></br></br> 
    </head> 

    <body style="color: #000; background-color:#e0e2e6; background-image:url(C:/Users/Marta 
    /Desktop/old.jpg);"> 

    <form action="people.php" method="post"> 
    <table "width="500"; border="0"; align="center"> 
    <tr> 
    <th>ID:</th> 
    <td><label for="textfield"></label> 
    <input type="text"name="ID" size="50" value="<?php echo $ID ?>"/></td> 
    <tr><th>First Name:</th> 
    <td><input type="text" name="First_name" size="50" value="<?php echo $First_name ?>" 
    /></td></tr> 
    <tr><th>Last Name:</th> 
    <td><input type="text" name="Last_name" size="50" value="<?php echo $Last_name ?>" 
    /></td></tr> 
    <tr><th>Address:</th> 
    <td><input type="text" name="Address" size="50" value="<?php echo $Address ?>" /></td> 
    </tr> 
    <tr><th>Country:</th> 
    <td><input type="text" name="Country" size="50" value="<?php echo $Country ?>" /></td> 
    </tr> 
    <tr><th>Ph number:</th> 
    <td><input type="text" name="Ph_number" size="50" value="<?php echo $Ph_number ?>" 
    /></td></tr></table> 
    <hr> 
    <table align="center"> 
    <tr><td> 

    <td><input type="submit" name="find" value="Find" /></td> 
    <td><input type="submit" name="add" value="Add" /></td> 
    <td><input type="submit" name="delete" value="Delete"/></td> 
    <td><input type="submit" name="update" value="Update"/></td> 
    <td><input type="submit" name="previous" value="<< Previous" /></td> 
    <td><input type="submit" name="next" value="Next >>" /></td> 

    <td align="center" colspan="3"><input type="submit" name="reset" value="Reset"/> 
    </td></tr></table> 
    <input type="hidden" id="resultOffset" name="resultOffset" value="<?php echo 
    $resultOffset ?>"> 
    <input type="hidden" id="resultCount" name="resultCount" value="<?php echo 
    $resultCount ?>"> 
    <input type="hidden" id="query" name="query" value="<?php echo $query ?>"> 
    </form> 
    </body> 
    </html> 
+0

$$國家= $ _ POST [ '國家'];' - 你有太多了,在開始'$'。 – andrewsi

+0

哦,我的哦,我的。我認爲這將是一件愚蠢的事情。現在所有人都在工作,感謝安德魯工廠。你讓我的一天;) – user2815059

+0

我認爲我們都做了類似的事情。 – andrewsi

回答

2

改變你的代碼:的$$Country = $_POST['Country'];$Country = $_POST['Country'];代替

+0

我的眼睛太疼了我會說。謝謝你Vipul – user2815059