Krzysztof Piszko
Krzysztof Piszko

Reputation: 1445

PHP - editing data in db issue

I'm going to keep it short and simple. I'm writing a really basic code in php which adds content to mysql db and I've run into an issue with editing. This is my form:

if($idExists)
{
   Print '
      <form action="editIt.php" method="POST">
      <div name="id"> '. $id . '</div>
      Enter new detail: <input type="text" name="details"/><br/>
      public post? <input type="checkbox" name="public[]" value="yes"/><br/>
      <input type="submit" value="Update List"/>
      </form>
   ';
}

And this is my editIt.php

//session start and stuff
if(filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_STRING) == "POST")
{
    echo "<script type='text/javascript'>alert('EDITIT!');</script>";
    mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("WebSiteDB") or die ("Cannot connect to database");
    $id = $_POST['id'];
    $details = mysql_real_escape_string($_POST['details']);
    $time = strftime("%X");
    $date = strftime("%B %d, %Y");
    $isPublic = 'no';

    foreach($_POST['public'] as $eachCheck)
    {
        if($eachCheck != NULL)
            $isPublic = "yes";
    }

    mysql_query("UPDATE list SET details='$details', dateEdited='$date', timeEdited= '$time', public='$isPublic' WHERE id='$id'");
    header("location: home.php");
}

I can't really find an issue with this code (which is not really strange, I'm a newbie at web stuff) and yet it just goes to home.php and does not change data in DB. Please, help me jump this ledge, so I can go on with my life.

Upvotes: 2

Views: 97

Answers (1)

prava
prava

Reputation: 3986

I think, the problem is in this line $id = $_POST['id'];. On form submit, the input field value will only be submitted, not the DIV value.

So, please change from :

if($idExists)
{
   Print '
      <form action="editIt.php" method="POST">
      <div name="id"> '. $id . '</div>
      Enter new detail: <input type="text" name="details"/><br/>
      public post? <input type="checkbox" name="public[]" value="yes"/><br/>
      <input type="submit" value="Update List"/>
      </form>
   ';
}

To :

if($idExists)
{
   Print '
      <form action="editIt.php" method="POST">
      <input type="hidden" name="id" value="' . $id . '">
      Enter new detail: <input type="text" name="details"/><br/>
      public post? <input type="checkbox" name="public[]" value="yes"/><br/>
      <input type="submit" value="Update List"/>
      </form>
   ';
}

Upvotes: 2

Related Questions