如何编辑和更新PHP?

时间:2014-11-17 07:26:03

标签: php

<?php

$id = $_GET['id'];

$selectDetails = $dbCon->query("SELECT * FROM cars WHERE id = $id");

if(isset($_GET['updated'])){

    print 'Biloplysninger opdateret';

}

?>



<?php

while($detailsRow = $selectDetails->fetch_object()){

?>

<form action="" method="post">

    <input type="text" name="brand" value="<?=$detailsRow->brand ?>" placeholder="Mærke" />
    <input type="text" name="model" value="<?=$detailsRow->model ?>" placeholder="Model" /><br />
    <select name="category">

<?php

    if($detailsRow->category_fk == 1){

?>

        <option value="1" selected>Personbil</option>
        <option value="2">Vrag</option>
        <option value="3">Lastvogne</option>

<?php

    }elseif($detailsRow->category_fk == 2){

?>

        <option value="1">Personbil</option>
        <option value="2" selected>Vrag</option>
        <option value="3">Lastvogne</option>

<?php

    }elseif($detailsRow->category_fk == 3){

?>

        <option value="1">Personbil</option>
        <option value="2">Vrag</option>
        <option value="3" selected>Lastvogne</option>

<?php

    }

?>

    </select>

    <input type="number" name="doors" value="<?=$detailsRow->doors; ?>" placeholder="Doors" /><br />
    <input type="number" name="year" value="<?=$detailsRow->year; ?>" placeholder="Årgang" />
    <input type="number" name="km" value="<?=$detailsRow->km; ?>" placeholder="Kilometre" /><br />
    <input type="number" name="price" value="<?=$detailsRow->price; ?>" placeholder="Pris" />
    <span id="ff">Solgt:</span> <input type="checkbox" name="sold" <?php ($detailsRow->sold == 1 ? print 'checked' : print ''); ?>/>
    <input type="submit" name="submit" value="Opdater" />

</form>

<?php

    $brand = $_POST['brand'];
    $model = $_POST['model'];
    $category = $_POST['category'];
    $doors = $_POST['doors'];
    $year = $_POST['year'];
    $km = $_POST['km'];
    $price = $_POST['price'];
    $sold = '';

    if(isset($_POST['sold'])){

        $sold = 1;

    }else{

        $sold = 0;

    }

    if(isset($_GET['submit'])){

        updateCar($id, $brand, $model, $category, $doors, $year, $km, $price, $sold);

    }


}

?>

1 个答案:

答案 0 :(得分:0)

表单已method="post",您正在使用$_GET['submit']检查提交。您是否尝试过 -

if (!empty($_POST)) {
  $brand = $_POST['brand'];
  $model = $_POST['model'];
  $category = $_POST['category'];
  $doors = $_POST['doors'];
  $year = $_POST['year'];
  $km = $_POST['km'];
  $price = $_POST['price'];
  $sold = '';

  if(isset($_POST['sold'])){

    $sold = 1;

  }else{

    $sold = 0;

  }

  if(isset($_POST['submit'])){

    updateCar($id, $brand, $model, $category, $doors, $year, $km, $price, $sold);

  }
}