2

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.

2
  • Can you edit your query line as mysql_query(...) or die(mysql_error()); so we can see what error message is being thrown? Commented Oct 3, 2014 at 8:37
  • You should avoid using mysql_* because it's deprecated. Have you tried dumping id? var_dump($id) ? You can then check if there is a row with such id, run simple sql SELECT * from list WHERE id =[your_id]; Commented Oct 3, 2014 at 8:40

1 Answer 1

2

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>
   ';
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes! That was it. Thank you very much, that also cleared my confusion with <form> stuff!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.