简体   繁体   English

使用PHP更新时,MySQL数据库使用空值更新

[英]MySQL database is updated with empty value when updating using PHP

I am new in php, i tried this coding i select a value in my drop down list i want a corresponding value to be updated, i have list of user name in my database and a ID for them, i am displaying the user name and when i want to update i written a sql query to find the member id and update to database but it's inserting a null value. 我是php的新手,我尝试了这种编码方式,在下拉列表中选择了一个值,希望更新相应的值,我在数据库中有用户名列表,并有一个ID,我正在显示用户名,当我想更新时,我写了一个SQL查询来查找成员ID并更新到数据库,但它会插入一个空值。 Here is my code. 这是我的代码。

Dropdown list code 下拉列表代码

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

My database is updated with empty string, if i directly pass the dropdown value Name it's updating fine. 我的数据库使用空字符串更新,如果我直接传递下拉值Name,则更新正常。 But i want to update the corresponding memberid to my table. 但是我想将对应的memberid更新为我的表。 Please help me. 请帮我。

Stage 1: 阶段1:

You don't do anything if the field is blank. 如果该字段为空白,则您什么也不做。 (Plus you have your logic wrong with $ok). (另外,您对$ ok的逻辑有误)。

Suggested code would be: 建议的代码为:

$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.

** Stage 2:** **第二阶段:**

You should check for isset($_POST['onamo']) before getting the variable. 您应该在获取变量之前检查isset($_POST['onamo']) Otherwise it would throw a warning. 否则会发出警告。 This will probably give you the error. 这可能会给您错误。 You have a discrepancy between 'heid' and 'memname1'! 您在'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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM