简体   繁体   中英

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. 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. But i want to update the corresponding memberid to my table. Please help me.

Stage 1:

You don't do anything if the field is blank. (Plus you have your logic wrong with $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. Otherwise it would throw a warning. This will probably give you the error. You have a discrepancy between 'heid' and '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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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