0

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

下拉列表代码

<?
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> 
       '

我的数据库用空字符串更新,如果我直接传递下拉值 Name 它更新正常。但我想将相应的 memberid 更新到我的表中。请帮我。

4

1 回答 1

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.
于 2013-01-14T07:41:06.967 回答