简体   繁体   English

如果不为空,则将值插入数据库;如果为空,则将值设置为“ null”

[英]Insert value to database if not empty and set the value to 'null' if empty

I'm new to mysql. 我是mysql新手。 so my problem is I want to set the $status in database to 'IN PROCESS' if the subject field is not empty. 所以我的问题是,如果主题字段不为空,我想将数据库中的$ status设置为'IN PROCESS'。 and if the subject is empty, the status will set to empty. 如果主题为空,则状态将设置为空。 here is my code, when i run it, nothing happen to the status. 这是我的代码,当我运行它时,状态什么都没有发生。 Thanks! 谢谢!

<?php
    if(isset($_POST['submit']))
     {
         $dbhost = 'localhost';
         $dbuser = 'root';
         $dbpass = '';
         $conn = mysql_connect($dbhost, $dbuser, $dbpass);

         mysql_select_db('course');

         if(! $conn )
         {
         die('Could not connect: ' . mysql_error());
         }

         $code= $_POST['code'];
         $subject= $_POST['subject'];
         $status= $_POST['status'];

         $sql = "INSERT INTO form(code,subject,status)
         VALUES('$code','$subject','$status')";

         $retval = mysql_query( $sql, $conn );

         if(!empty($_POST['subject'])) {
                $status = 'IN PROCESS';
            }      
            else $status = 'null';

         if(! $retval )
         {
         die('Could not enter data: ' . mysql_error());
         }

         else
         {
             ?>
             <div class="alert alert-success">
               <strong>Success!</strong> .
             </div>
             <?php
         }


         mysql_close($conn);
      }
 ?>

Try to put Insert statement after your if condition . 尝试在if条件之后放置Insert语句。 because the value of the $status has already insert before the condition. 因为$status的值已经在条件之前插入。

<?php
        if(isset($_POST['submit']))
         {
             $dbhost = 'localhost';
             $dbuser = 'root';
             $dbpass = '';
             $conn = mysql_connect($dbhost, $dbuser, $dbpass);

             mysql_select_db('course');

             if(! $conn )
             {
             die('Could not connect: ' . mysql_error());
             }

             $code= $_POST['code'];
             $subject= $_POST['subject'];
             $status= $_POST['status'];

             if(!empty($_POST['subject'])) {
                    $status = 'IN PROCESS';
                }      
                else $status = 'null'; 
             //put it here
             $sql = "INSERT INTO form(code,subject,status)
             VALUES('$code','$subject','$status')";

             $retval = mysql_query( $sql, $conn );

             if(! $retval )
             {
             die('Could not enter data: ' . mysql_error());
             }

             else
             {
                 ?>
                 <div class="alert alert-success">
                   <strong>Success!</strong> .
                 </div>
                 <?php
             }


             mysql_close($conn);
          }
     ?>

set

if(!empty($_POST['subject'])) {
    $status = 'IN PROCESS';
}      
else $status = 'null';

before your query 在查询之前

$sql = "INSERT INTO form(code,subject,status)
     VALUES('$code','$subject','$status')";

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

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