在更新时,PHP将所有字段都清空

时间:2011-04-30 23:16:06

标签: php database

我正在使用以下代码。每当表单用于更新时,PHP将清空db中的那些字段。我知道如果field_names不匹配会发生这种情况,所以我对它们进行了三次检查,它们都能正常工作。这是我第一次收到此错误。不知道该怎么办。

代码:

     //Getting Unique ID
     $sql = "select `Person_id` from `accounts` where `username` = '$username'";
     $query = mysql_query($sql) or die ("Error: ".mysql_error());

     while ($row = mysql_fetch_array($query)){

     $pid = $row['Person_ID'];
      }
         mysql_free_result($query);

      $newname = mysql_real_escape_string($_POST['Full_name']);
      $newgender = mysql_real_escape_string($_POST['Patient_gender']);
       $newmonth = mysql_real_escape_string($_POST['Month']);
       $newday = mysql_real_escape_string($_POST['Day']);
       $newyear = mysql_real_escape_string($_POST['Year']);
        $newacctss = mysql_real_escape_string($_POST['Acct_SS']);
        $newaddress = mysql_real_escape_string($_POST['Address']);
        $newaddress2 = mysql_real_escape_string($_POST['Address2']);
          $newcity = mysql_real_escape_string($_POST['City']);
         $newstate = mysql_real_escape_string($_POST['State']);
         $newzipcode = mysql_real_escape_string($_POST['Zip_code']);
          $newhomephone = mysql_real_escape_string($_POST['Home_phone']);
        $newcellphone = mysql_real_escape_string($_POST['Cell_phone']);
       $neworkphone = mysql_real_escape_string($_POST['Work_phone']);
       $newsure = mysql_real_escape_string($_POST['Sure']);
       $newfav = mysql_real_escape_string($_POST['Favorite']);
       $newcars = mysql_real_escape_string($_POST['Cars']);
       $newdrinks = mysql_real_escape_string($_POST['Drinks']);
         $newmoi = mysql_real_escape_string($_POST['About_moi']);


       //Update Name Only
      $sql = "UPDATE accounts SET `full_name` = '$newname' WHERE Username = '$username'"; 
       $query1 = mysql_query($sql) or die ("Error: ".mysql_error());


     //Everything else being udpated here 
    $sql2 = "UPDATE profile SET `Patient_gender` = '$newgender', 
    `Month` = '$newmonth', `Day` =  '$newday', `Year` = '$newyear', 
    `Acct_SS` = '$newacctssn', `Address` = '$newaddress', 
    `Address2` = '$newaddress2', `City` = '$newcity', `State` = '$newstate', 
    `Zip_code` = '$newzipcode', `Home_phone` = '$newhomephone', 
    `Cell_phone` = '$cellphone', `Work_phone` = '$neworkphone', 
    `Sure` = '$newsure', `Favorite` = '$newfav', `Cars` = '$newcars', 
     `Drinks` = '$newdrinks', `About_moi` = '$newmoi' WHERE Person_id = '$pid'"; 
      $query2 = mysql_query($sql2) or die ("Error: ".mysql_error());

1 个答案:

答案 0 :(得分:3)

数组键在PHP中区分大小写。您正在查询Person_id的数据库,但正在提取Person_ID。您想通过在提取调用后检查$pid是否为空来检查您是否实际从结果集中获取了某些内容,如果是,则检查是否为空。

同样,假设您的查询只提取一行,则不需要while()构造。

除此之外,检查$newname实际上是否正确创建。如果您的表单没有正确提交,或者名称字段实际上被称为其他内容,那么您将在数据库中插入一个空白,因为该变量从未正确填充。通过var_dump($_POST)来查看提交的内容。