2013-01-13 158 views
0

我是新的PHP,我试过这个编码我在我的下拉列表中选择一个值我想要更新相应的值,我有我的用户名列表数据库和他们的ID,我显示用户名,当我想更新我写了一个SQL查询来查找成员ID和更新到数据库,但它插入一个空值。这是我的代码。当使用PHP更新时,MySQL数据库更新为空值

下拉列表代码

<? 
session_start(); 

if(!isset($_SESSION[''])){ 
header("location:"); 
} 
?> 
    <?php include('dbconnect.php'); ?> 
    <?php 
    $ed=$_GET['ed']; 



$query=mysql_query("select * from table1 where id='$ed'"); 

$query2= "select * from table2"; 

      $row=mysql_fetch_assoc($query); 



     if($_POST['Submit']) 
     { 
      $mem= $_POST['memid']; 

      $memname =mysql_query("select memid from table2 where name='$mem'"); 

$memname1= mysql_fetch_assoc($memname); 
$tot_count = mysql_fetch_assoc($ro_count); 



      $date=date("d-m-Y");  

      $status="Active"; 


      $onamo=mysql_real_escape_string($_POST['onamo']); 
      $heid = mysql_real_escape_string($_POST['memname1']); 
     if($_POST['heid']=='') 
      { 
       $namo1="*Required"; 
       $ok=1; 

      } 
      if($_POST['onamo']=='') 
      { 
       $onamo1="*Required"; 
       $ok=1; 

      } 

     $insert=mysql_query("update table1 set oname='$onamo', heid='$heid' where id='$ed'") or die('error'); 

       if($insert) 
       { 
        header("Location"); 

       } 

     } 



     ?> 

    <body> 
<div id="main_container"><br /> 
<div class="main_content"> 
    <div class="center_content"> 
    <div class="right_content">    
    <div class="form">  
    <form action="" method="post" name="fomo" enctype="multipart/form-data" onsubmit="return fall();" class="niceform"> 






     <h1 align="center">Edit Referal Partner </h1> 

      <? 



     if($_GET['val']==1) { echo "<h1 class='FeatureBlockHeader' >Member Added Successfully</h1>"; } ?> 


     <fieldset> 

      <dl><dt><label for="Owner Name">Referal Partner Name</label></dt><dd><input name="onamo" type="text" size="53" id="onamo" value="<?=$row['oname']?>"/><b style="color:#CA0000"><?=$onamo1?></b></dd></dl> 


     <dl><dt><label for="">Health Executives</label> 
     <?php $result1 = mysql_query($query2); 
echo'<select name="memid" >'; 
    while($row = mysql_fetch_assoc($result1)) { 
    echo '<option value="'.$row['name'].'">' . $row['name'] . '</option>'; 
    } 
    echo '</select>'; ?> 
    </b></dd></dt> 
     <dl><dt><label for="submit"></label></dt><dd> <input type="submit" name="Submit" value="Submit"></dd></dl></fieldset> 



     </table> 


     </form> 
     ' 

我的数据库与空字符串更新,如果我直接通过下拉值名称它的更新的罚款。但我想更新相应的memberid到我的表。请帮帮我。

+0

你能显示你的HTML代码吗? – Yasmeen

+1

另外,检查print_r($ _ POST)的输出; – SISYN

+1

'onamo'或'oname'? YOu同时使用 - 检查你不会感到困惑。 – Robbie

回答

0

第1阶段:

如果该字段为空你没有做任何事情。 (加上你的逻辑错误$ OK)。

建议代码将是:

$ok = 1; // assume ok unless we have an error 
if($_POST['heid']=='') 
{ 
    $namo1="*Required"; 
    $ok=0; // Set to "0" to say "Not Ok" 
} 
if($_POST['onamo']=='') 
{ 
    $onamo1="*Required"; 
    $ok=0; // Set to "0" to say "Not Ok" 
} 
if ($ok) 
{ 
    // Do your update 
    $insert = mysql_query("update table1 set oname='$onamo', heid='$heid' where id='$ed'") or die('error'); 

    if($insert) 
    { 
     header('location: ???'); 
     exit(); // ALWAYS exit after a header redirect, otherwise the rest of the code will continue to work, then the redirect happens! 
    } 
    $ok = 0; 
    $error = 'Failed to update database' 
} 

// If you get here, you have an error condition. 

**第2阶段:**

获取变量之前,您应该检查isset($_POST['onamo'])。否则它会发出警告。这可能会给你错误。你有'heid'和'memname1'之间的差异! :)

$ok = 1; // assume ok unless we have an error 
if(!isset($_POST['heid']) || $_POST['heid']=='') // Or is it $_POST['memname1']? 
{ 
    $namo1="*Required"; 
    $ok=0; // Set to "0" to say "Not Ok" 
} 
if(!isset($_POST['onamo']) || $_POST['onamo']=='') 
{ 
    $onamo1="*Required"; 
    $ok=0; // Set to "0" to say "Not Ok" 
} 
if ($ok) 
{ 
    $onamo=mysql_real_escape_string($_POST['onamo']); 
    $heid = mysql_real_escape_string($_POST['memname1']); // Or is it $_POST['heid'] ?? 

    // Do your update 
    $insert = mysql_query("update table1 set oname='$onamo', heid='$heid' where id='$ed'") or die('error'); 

    if($insert) 
    { 
     header('location: ???'); 
     exit(); // ALWAYS exit after a header redirect, otherwise the rest of the code will continue to work, then the redirect happens! 
    } 
    $ok = 0; 
    $error = 'Failed to update database' 
} 

// If you get here, you have an error condition.