Domanda

Hello everyone i'm able to display my record by passing an id by query string to another page, but i'm not able to update it, the problem is that when i click on update nothing happen, it return me a blank page, and there is no printed error, can someone help me please?

  <?php
   require 'db2.php';
   $id = null;
     if ( !empty($_GET['id'])) {
    $id = $_REQUEST['id'];

 $dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );
 $q = mysqli_query($dbc,"SELECT * FROM movie WHERE MovieID = '$id' ");
   while($r=mysqli_fetch_array($q))
     {   
    $title = $r["Title"];
    $tag = $r["Tag"];
    $year = $r["YEAR"];
    $cast = $r["Cast"];
    $comment = $r["Comment"];
    $IDBM = $r["IMDB"];
}


}

At this stage, the code display every information i need , the stage below is where i'm having a problem, i'm not able to get the id against and make the update when click on update button

elseif (!empty($_POST) and !empty($_GET['id']) ) {


    // keep track post values
    $cast = $_POST['cast'];
    $title = $_POST['title'];
    $comment =$_POST['comment'];
     $year = $_POST['year'];
      $tag = $_POST['tags'];
     $IDBM = $_POST['idbm'];
    $cast = htmlspecialchars($cast);
    $title = htmlspecialchars($title);
    $comment = htmlspecialchars($comment);

    // validate input
    $valid = true;
    if (empty($cast)) {
        $castError = 'Please enter Cast';
        $valid = false;
    }

    if (empty($title)) {
        $titleError = 'Please enter Title';
        $valid = false;
    }
      if (empty($comment)) {
        $commentError = 'Please enter Comment';
        $valid = false;
    }


    if ($valid) {
    $id = $_REQUEST['id'];

$valid_formats = array("jpg", "png", "gif", "bmp");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
    {
        $name = $_FILES['photoimg']['name'];
        $size = $_FILES['photoimg']['size'];

        if(strlen($name))
            {
                list($txt, $ext) = explode(".", $name);
                if(in_array($ext,$valid_formats))
                {
                if($size<(1024*1024))
                    {
                        $actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
                        $tmp = $_FILES['photoimg']['tmp_name'];
                        if(move_uploaded_file($tmp, $path.$actual_image_name))
                            {

                                 mysqli_query($dbc,"UPDATE movie SET Title='$title',Year = '$year',Cast='$cast',Cover='$actual_image_name',Tag='$tag',Comment='$comment',IMDB ='$IDBM' WHERE MovieID=".$id);
                                header ("Location: index.php");
                            }
                        else
                            echo "failed";
                    }
                    else
                    echo "Image file size max 1 MB";                    
                    }
                    else
                    echo "Invalid file format..";   
            }

        else
            echo "Please select image..!";

        exit;
    }


    }
    }
È stato utile?

Soluzione

First thing, when you get a blank page, check your error log. Or if you're lazy, add this at the begining of your file to get error messages.

<?php
error_reporting(-1);
ini_set('display_errors', 'On');
?>

It's hard to say, but just looking at your code quickly, I see a problem with your mixup of $_GET and $_POST. From what I gather, since your SELECTworks, you send data in $_GET, and your UPDATE block is only executed if you have $_POST data.

Change your html <form method="get"> for <form method="post">

And change your select block to check if( !empty($_POST['id'])) {

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top