douji5746 2014-03-12 00:55
浏览 34
已采纳

php mysql中不同的输入项

I created a html form with html and had inputs of item, comment and visible. What i would like to do is to be able to input these variables to sql by creating a code that cycles through with these protocols: if all fields are blank, all are filled or only one or two are filled so it can only add said filled data to mysql table. Example of one of the codes i used was

            if($nitem != ' ' and $comment != ' ')
{$query = "UPDATE shop.titem  SET 
item = '$nitem', comment = '$comment', visible = $visible
WHERE titem.item ='$item'";
$sqlhandle = mysql_query($query, $connection);}
if (!$sqlhandle){echo 'create all failed' . mysql_error();
}else{break;}

I used the break but it keeps cycling through ALL the queries done and thats where i need help. As usual, i'm a total noob in this so i'd appreciate it if you have patience and understanding with me. Thank you.

UPDATE: i did an echo to generate sql and it returns a statement where all the codes wher run, i.e if only the item field had been edited with to eg: milk, it gives me item as milk, comment as blank and visible as 1 (i set default values as blank and visible as 1 if fields weren't filled with anything)

full code

    $item = $_POST['item'];
$nitem = $_POST['nitem'];
$comment = $_POST['comment'];
if(!isset($_POST['visible']))
{$visible = 1;}
else
{$visible = $_POST['visible'];}


     //for when all is true
if($nitem != ' ' and $comment != ' ')
{$query = "UPDATE shop.titem  SET 
item = '$nitem', comment = '$comment', visible = $visible
WHERE titem.item ='$item'";
$sqlhandle = mysql_query($query, $connection);}
if (!$sqlhandle){echo 'create all failed' . mysql_error();
}else{break;}



//for when all is false
if($nitem = ' ' and $comment = ' ' )
{echo 'please go back and edit a field.';
}else{break;}


//for when nitem and comment is true
if($nitem != ' ' and $comment != ' ')
{$query = "UPDATE shop.titem SET item = $nitem, comment = $comment
WHERE item = $item";
$sqlhandle = mysql_query($query, $connection);}
if (!$sqlhandle){echo 'item and comment write failed' . mysql_error();
}else{break;}


//for when nitem only is true
if($nitem !=' ' and $comment = ' ')
{$query = "UPDATE shop.titem SET item = $nitem
WHERE item = $item";
$sqlhandle = mysql_query($query, $connection);}
if (!$sqlhandle){echo 'item write failed' . mysql_error();
}else{break;}


//for where comment only is true
if($comment != ' ' and $nitem = ' ')
{$query = "UPDATE shop.titem SET 
        comment = $comment WHERE titem.item = $item";
$sqlhandle = mysql_query($query, $connection);}
if(!$sqlhandle){echo 'comment write failed' . mysql_error();
}
  • 写回答

1条回答 默认 最新

  • doulang2311 2014-03-12 01:21
    关注

    I think most of your validation checks for a space and some of them assigning spaces instead of validating (you had "=" instead of "==") , I have minimised and fixed some stuff from your code you posted and came up with following,

        $item   = $_POST['item'];
        $nitem  = $_POST['nitem'];
        $comment= $_POST['comment'];
    
       if(!isset($_POST['visible'])){
            $visible = 1;
       }else{
            $visible = $_POST['visible'];
       }
    
    
         //for when all is true
       if(trim($nitem)!= '' and trim($comment) != ''){
               $query = " item = '".$nitem."', comment = '".$comment."', visible = $visible ";
       }else{
               $message = 'please go back and edit a field.';   
       }
    
       //for when nitem and comment is true
       if(trim($nitem)!= '' and trim($comment) != ''){
           $query = " item = '".$nitem."', comment = '".$comment."' ";
       }
    
       //for when nitem only is true
       if(trim($nitem) != '' and trim($comment) == ''){
           $query = " item = '".$nitem ."' ";
       }
    
       //for where comment only is true
       if(trim($comment) != '' and trim($nitem) == ''){
           $query = " comment ='". $comment ."' " ;
       }
    
        $mainquery = "UPDATE shop.titem  SET ". $query ." WHERE titem.item = '".$item."'";
        $sqlhandle = mysql_query($mainquery, $connection);
    
       if(!$sqlhandle){
          echo $message . mysql_error();
       }
    

    If I were you, and I had this requirement I would take a different approach, but I didn't want to change your concept which will confuse you. Also I dont know whats the full requirement is. Anyway try to adapt my code and see how it goes, I have removed all the breaks and there is only one place that sql queries get execute.

    Think this will help you to fix the issue...

    P.S : Always try to write readable code, it will help you in future :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用