简体   繁体   中英

PHP-SQL: Date does not update the database

(Before posting this, I took a look at all the many related questions here, and adjusted the code, to no effect). So here is the situation: I have a books database to which I have added a date field(Bookdate). Now I want to update the date on the previous records, which I am doing with the following code:

$stamp      = get_post($conn, 'stamp');
$stamp      = date('Y-m-d H:i:s', strtotime($_POST[$stamp]));

(the get_post function simply returns $conn->real_escape_string($_POST[$var]) ). However the date I enter into the form never gets updated in the database, instead it reads 1970-01-01 - which I understand is the Unix epoc thing. I accept the date details throught a form where:

   Date <input type="text" name="stamp" value="<?php echo $row[8]; ?>">  

$row[8] here is the Bookdate field that I wish to update.

The code chunk with the SQL update query is below. Pls note the code works for updating all other fields , but updating date has no effect - it remains at 1970-01-01, while the other records which I haven't updated yet stand at 0000-00-00. Bookdate=NOW() in the below query nicely updates the field to the current date. So what is wrong with the variable I am passing ie. $stamp.?? Would appreciate any help on this.

  if ($_FILES['image2'])
  {
    $ed_author   = get_post($conn, 'ed_author');
    $ed_title    = get_post($conn, 'ed_title');
    $ed_category = get_post($conn, 'ed_category');
    $ed_year     = get_post($conn, 'ed_year');
    $hid_isbn     = get_post($conn, 'hid_isbn');
    $stamp      = get_post($conn, 'stamp');
    $stamp      = date('Y-m-d H:i:s', strtotime($_POST[$stamp]));

    if($_FILES['image2']['error'] != UPLOAD_ERR_NO_FILE){
        $filetmp = $_FILES["image2"]["tmp_name"];
        $filename = $_FILES["image2"]["name"];
        $filetype = $_FILES["image2"]["type"];
        $filepath = "images/".$filename;

        move_uploaded_file($filetmp, $filepath);        

        $query    = "UPDATE classics 
                         SET author='$ed_author', title='$ed_title', 
                            `type`='$ed_category', `year`='$ed_year', 
                            filename='$filename', filepath='$filepath', 
                            filetype='$filetype', Bookdate='$stamp' 
                      WHERE isbn='$hid_isbn'";

        $result   = $conn->query($query);

        if (!$result) echo "EDIT with image failed: $query<br>" .
          $conn->error . "<br><br>";
          $result   = $conn->query($query);     

        } else {
            $query  = "UPDATE classics 
                           SET author='$ed_author', title='$ed_title', 
                               `type`='$ed_category', `year`='$ed_year', 
                               Bookdate='$stamp' 
                        WHERE isbn='$hid_isbn'";

            $result   = $conn->query($query);

            if (!$result) 
               echo "EDIT with image failed: $query<br>" . $conn->error . "<br><br>";

        }
    }    

Here is the form that takes in the date (also displays what's already there):

  <form action="news.php" method="post" enctype="multipart/form-data">  <pre>   
        Author <input type="text" name="ed_author" value="<?php echo $row[0]; ?>">
         Title <input type="text" name="ed_title" value="<?php echo $row[1]; ?>">
      Category <input type="text" name="ed_category" value="<?php echo $row[2]; ?>">
          Year <input type="text" name="ed_year" value="<?php echo $row[3]; ?>">
          ISBN <input type="text" name="hid_isbn" value="<?php echo $row[4]; ?>"readonly>
          Date <input type="text" name="stamp" value="<?php echo $row[8]; ?>">        

               <input type="hidden" name="oldfilename" value="<?php echo $row[5]; ?>">  
               <input type="hidden" name="oldfilepath" value="<?php echo $row[6]; ?>">
               <input type="hidden" name="oldfiletype" value="<?php echo $row[7]; ?>">

               <input type="file" name="image2">
               <input type="hidden" name="edit" value="yes">
               <input type="submit" name="btn2" value="SUBMIT EDIT">

        </pre></form>

And for clarity, here it the get_post function:

  function get_post($conn, $var)
  {
    return $conn->real_escape_string($_POST[$var]);
  }   

UPDATE : var_dump shows $_POST['stamp'] as NULL right after get_post; Also the following errors:

Notice: Undefined index: 2000-01-01 in C:\\xampp\\htdocs\\experiment\\woldofbooks\\news.php on line 69: $stamp = get_post($conn, 'stamp'); var_dump($_POST[$stamp]);

Notice: Undefined index: 2000-01-01 in C:\\xampp\\htdocs\\experiment\\woldofbooks\\news.php on line 70: $stamp = date('Ymd H:i:s', strtotime($_POST[$stamp]));var_dump($_POST[$stamp]);

Notice: Undefined index: 1970-01-01 01:00:00 in C:\\xampp\\htdocs\\experiment\\woldofbooks\\news.php on line 70

You are using the variable $stamp as both the key and the value in the POST array.

$stamp      = get_post($conn, 'stamp');
$stamp      = date('Y-m-d H:i:s', strtotime($_POST[$stamp]));

Line 1 here returns the contents of the $_POST['stamp'] , then you are using line 2 to turn the $_POST value into a date.

What you are doing in line 2 is telling PHP to look up a $_POST variable with the key that is actually the returned contents value from Line 1.

Notice: Undefined index: 1970-01-01 01:00:00 in C:\\xampp\\htdocs\\experiment\\woldofbooks\\news.php on line 70

Describes it perfectly and this should show you the exact error and its solution.

The solution:

You do not need to run $_POST in Line 2, as the $_POST value was returned in Line 1, so:

$stamp      = get_post($conn, 'stamp');
$stamp      = date('Y-m-d H:i:s', strtotime($stamp));

Specifically answering your original query, your MySQL is being given a NULL value and so that is why the date field is not being updated.

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