简体   繁体   中英

Select with options MYSQL PHP Update

I have a column that manage if a project is open or locked. This is column is enum with 0 and 1. I need help to update this column to correct value.

Here is my check for option list.

$stripped = mysql_real_escape_string($_GET['id']);
$getModul = mysql_query("SELECT * FROM cms_moduler WHERE locked = '0' AND id = '$stripped'"); 
    if($modulsinfo = mysql_fetch_array($getModul))      
           {    
            echo'
                <div class="form-group" style="width:60%;">
                    <select class="form-control">
                        <option selected>Open</option>
                        <option name="status" id="status" value="status">Locked</option>                
                    </select>
                </div>';
           }
    else 
           {
                echo'                   
                    <div class="form-group" style="width:60%;">
                        <select class="form-control">
                            <option name="status1" id="status1" value="status1">Open</option>
                            <option selected>Locked</option>
                        </select>
                   </div>';
          {

 }}}
 ?>
  1. I can't figure out how to insert the selected option.
  2. How can I convert "open" AND "locked" to 1 or 0.

Here is my insert so far:

$stripped = mysql_real_escape_string($_GET['id']);
$title = mysql_real_escape_string($_POST['title']);
$mytextarea = mysql_real_escape_string($_POST['mytextarea']);
$image = mysql_real_escape_string($_POST['image']);
$status = mysql_real_escape_string($_POST['status']);
$status1 = mysql_real_escape_string($_POST['status1']);

if(isset($_POST['moduledit']))
{

    if (empty($_POST['title']))
    {
        echo    '<div class="alert bg-danger" role="alert">
                    <svg class="glyph stroked cancel"><use xlink:href="#stroked-cancel"></use></svg> Tittel mangler på modulen.</span></a>
                </div>';
    }

else {
       if (empty($_POST['mytextarea']))
            {
                echo '<div class="alert bg-danger" role="alert">
                        <svg class="glyph stroked cancel"><use xlink:href="#stroked-cancel"></use></svg> Modulen kan ikke være tom.</span></a>
                    </div>';
            }   

        else 
            {   

                $result = mysql_query("UPDATE cms_moduler SET locked = '$status' OR '$status1', image = '$image', title = '$title', longstory = '$mytextarea' WHERE id = $stripped;");
            }

First of all, remove this locked = '0' condition from the WHERE clause, otherwise the SELECT query will fetch only the open projects. So your SELECT query should be like this:

// your code
$getModul = mysql_query("SELECT * FROM cms_moduler WHERE id = '$stripped'");

Second, instead of creating two different div s, create only one div to show whether the fetched project is in open or locked state, like this:

if(mysql_num_rows($getModul)){
    $modulsinfo = mysql_fetch_array($getModul); 
    ?>
    <div class="form-group" style="width:60%;">
        <select class="form-control" name="status">
            <option<?php if($modulsinfo['locked'] == 0){ echo ' selected="selected"'; } ?> value='0'>Open</option>
            <option<?php if($modulsinfo['locked'] == 1){ echo ' selected="selected"'; } ?> value='1'>Locked</option>        
        </select>
    </div>
    <?php
}

Finally after form submission, simply use $_POST['status'] to get the status of project ie 0 for open and 1 for locked , like this:

// your code
// $status would be either 0 or 1
$status = mysql_real_escape_string((int)$_POST['status']);

// Now perform your UPDATE operation

Sidenote: Don't use mysql_ database extensions, they are deprecated as of PHP 5.5.0 and are removeda altogether in PHP 7.0.0. Use mysqli or PDO driver instead. And this is why you shouldn't use mysql_* functions .

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