0

私はphpの初心者です。このコーディングを試しました。ドロップダウンリストで値を選択します。対応する値を更新したいです。データベースにユーザー名のリストとそれらのIDがあります。ユーザー名を表示しています。更新したいとき、メンバーIDを見つけてデータベースに更新するSQLクエリを書きましたが、null値が挿入されています。これが私のコードです。

ドロップダウン リストのコード

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

私のデータベースは空の文字列で更新されます。ドロップダウン値の名前を直接渡すと、正常に更新されます。しかし、対応するメンバー ID をテーブルに更新したいと考えています。私を助けてください。

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 に答える