0

I'm having a problem in PHP code. I got some code from an e-book, but when I tried to run the system, it's giving some errors. I fixed some of the errors by giving proper quotation marks, but now I'm stuck with some other errors. I want edit->apply forms with mysql

I'd be more than happy if some one helps me with the following error:

Notice: Undefined index: id in C:\xampp\htdocs\kayit_defteri\edit.php on line 17

My edit.php File

<!DOCTYPE html>
<html>
<head>
 <title>Kayıt Defteri</title>
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
 <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
 <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
 <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</head>
<body>

 <?php

include ("db.php");

 $id = $_GET["id"];

 $select = $db->prepare("SELECT * FROM adres WHERE id = :id");
 $select->bindParam(":id", $id, PDO::PARAM_INT);
 $select->execute();
 $row = $select->fetch(PDO::FETCH_ASSOC);

  ?>

<div class="container" style="padding:20px;">
  <div class="row">
   <div class="col-md-12">
    <h2 style="color:royalblue; font-size:28px;">Hemen Kayıt Ekleyin</h2>
   </div>
   
  </div>
  <div class="row">
   <div class="col-md-4">

    <form method="GET" action="edit.php">
   <label>İsim</label>
    <input type="text" class="form-control" name ="name" value="<?php echo $row["name"];?>">
    <label>Soyisim</label>
    <input type="text" class="form-control" name ="surname" value="<?php echo $row["surname"];?>">
    <label>Telefon</label>
    <input type="number" class="form-control" name ="telephone" value="<?php echo $row["telephone"];?>">
    <label>Web Sitesi</label>
    <input type="text" class="form-control" name="web" value="<?php echo $row["web"];?>">
    <label>Ödeyeceği Tutar</label>
    <input type="text" class="form-control" name ="tutar" value="<?php echo $row["tutar"];?>">
    <label>Notlar</label>
    <textarea rows="2" cols="46" class="form-control" placeholder="Notunuz varsa yazın..." name="note"><?php echo $row["note"];?></textarea>
  <br>
  <input type="hidden" name="id" value="<?php echo $row["id"]; ?>">

  <button type="submit" class="btn btn-primary">Gönder</button>
  <a href="list.php"><button type="button" class="btn btn-danger">Vazgeç</button></a>
</form>



   </div>
  </div>
 </div>
</body>
</html>

My update.php File

<?php 

include ("db.php");

if($_POST) {

 $name   = $_POST["name"];
 $surname  = $_POST["surname"];
 $telephone  = $_POST["telephone"];
 $web   = $_POST["web"];
 $tutar  = $_POST["tutar"];
 $note  = $_POST["note"];
 $id     = $_POST["id"];


 $data = array(

 "name"   => $name,
 "surname"  => $surname,
 "telephone" => $telephone,
 "web"   => $web,
 "tutar"  => $tutar,
 "note"   => $note,
 "id"   => $id,

);


$update = $db->prepare("UPDATE adres SET 
 name=:name,
 surname=:surname,
 telephone=:telephone,
 web=:web,
 tutar=:tutar,
 note=:note,
 WHERE id=:id

 ");

$result = $update->execute($data);

if($result) {
 echo "Kayıt işlemi başarılı bir şekilde gerçekleşti" . "<hr>" . "<a href='list.php'>Geri Dönün</a>";
} else {
 echo "Kayıt işlemi pekte başarılı geçmedi. Tekrar deneyin.";
}

}

 ?>
SECRAX
  • 43
  • 6
  • What URL are you using to load the `edit.php` page? Does it include an `id` query parameter, eg `edit.php?id=123`? – Phil Apr 27 '20 at 03:34
  • I can see the SQL data in the form. But unfortunately, when I want to edit the page is refreshed without the error message – SECRAX Apr 27 '20 at 03:40
  • 1
    You also have a trailing comma in `note=:note,` < right there. I've another duplicate in addition to the one that @Phil used. – Funk Forty Niner Apr 27 '20 at 03:49
  • *"without the error message"* - You probably didn't have error reporting working for you. – Funk Forty Niner Apr 27 '20 at 03:50
  • 1
    @SECRAX - dont take this closure as discouragement. You actually did a great question post that had all relevant code and at least 'enough' context for the simple type of problem it is. The only step you missed is the pretty important one to step back a tiny bit from your problem and just google around the error, which was just a simple variable error. And then the same for the next error and so on. Thats how you learn, by doing, making a mess and figuring out why by finding other people who already did the same :) – Daniel Brose Apr 27 '20 at 03:56

1 Answers1

0

From the line number in your error msg, looks like its telling you the $_GET param (url parameter) isnt found

Here are two simple solutions, requiring just one line change.

Ternary operator:

$id = (isset($_GET["id"]) ? $_GET["id"] : 0);

Swap 0 out with whatever your default should be, if no url parameter called id is passed in.

https://www.php.net/manual/en/language.operators.comparison.php

The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

Null Coalescing Operator (only php 7.0+):

$id = $_GET['id'] ?? 0;

Same doco link, just scroll down again: https://www.php.net/manual/en/language.operators.comparison.php

I personally prefer standard ternary as its backwards compatible too, always dealers choice though if backwards compatibility is not important.

Daniel Brose
  • 1,394
  • 10
  • 24
  • Parse error: syntax error, unexpected ';' in C:\xampp\htdocs\kayit_defteri\edit.php on line 19 – SECRAX Apr 27 '20 at 03:33
  • Copy paste typo, count the `(` and `)`, updating now – Daniel Brose Apr 27 '20 at 03:34
  • I can see the SQL data in the form. But unfortunately, when I want to edit the page is refreshed without the error message – SECRAX Apr 27 '20 at 03:42
  • @Phil, thanks, added it too, so used to working in older systems i always use old style format :) – Daniel Brose Apr 27 '20 at 03:43
  • @SECRAX, well, this solved the problem you posted. If your wanted to test the page, just google around "$_POST and $_GET php", for example: https://www.w3schools.com/php/php_superglobals_get.asp. Its easy to mimic a form submissions by using url parameters, and you might even find $_POST a better way to go if having issues affecting the url. – Daniel Brose Apr 27 '20 at 03:44
  • Personal opinion but there's too many parentheses in your ternary. `$id = isset($_GET["id"]) ? $_GET["id"] : 0;` will lead to fewer typos – Phil Apr 27 '20 at 03:44
  • 1
    @Phil, i actually do the extra parentheses to help avoid typos ;) Personal preference always exists though, i just find them more readable and clear, especially if they are (though you never should do this) nested. The php doco link above explains why not to nest, if anyone reading this later :) – Daniel Brose Apr 27 '20 at 03:46
  • 1
    I avoid nested ternary's like the plague (no, the other plague). For one, the [LTR evaluation](https://www.php.net/manual/language.operators.comparison.php#example-105) does my head in – Phil Apr 27 '20 at 03:48
  • @Phil Me too, pain in the "you know what". – Funk Forty Niner Apr 27 '20 at 03:51