0

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;");
            }
Laith
  • 428
  • 4
  • 10
  • Sidenote: `UPDATE/SET` does not use `OR` http://dev.mysql.com/doc/refman/5.7/en/update.html - So `SET locked = '$status' OR '$status1'` is failing here and checking for errors with `mysql_error()` on the query would have thrown you a syntax error about it. – Funk Forty Niner Oct 08 '16 at 01:34
  • Plus, where are the named inputs for all the POST arrays including the `
    ` itself?
    – Funk Forty Niner Oct 08 '16 at 01:36
  • Then I see `$_POST['image']` which suggests file handling and if so, then you need to use `$_FILES` and a post method with a valid enctype, which are unknownst to us. – Funk Forty Niner Oct 08 '16 at 01:38

1 Answers1

0

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 divs, 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 i.e. 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.

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • Have a look at [this comment](http://stackoverflow.com/questions/39927379/select-with-options-mysql-php-update#comment67139072_39927379) I left under the question ;-) that's what's the biggest thing which is failing them and possibly more that we don't know about, as said under my 2nd and 3rd comments. – Funk Forty Niner Oct 08 '16 at 01:34
  • @Fred-ii- yeah, I had noticed that `locked = '$status' OR '$status1'`. I assumed that OP is confused about what to put in there, that's why `locked = '$status' OR '$status1'` thingy. I'm actually waiting for any update from the OP. – Rajdeep Paul Oct 08 '16 at 01:40
  • No idea where the guy is, so wishing you well with this ;-) – Funk Forty Niner Oct 08 '16 at 01:42
  • @Fred-ii- OP might have gone for a good night sleep after posting the question. ;-) – Rajdeep Paul Oct 08 '16 at 01:44