0

I have a problem with the editing data on uploaded to MySQL.

Here is my edit.php

  <html>
  <head>
  </head>

 <body>
 <table border=1>
 <tr>
  <td align=center>Form Edit Employees Data</td>
 </tr>
 <tr>
  <td>
   <table>
   <?
   include "dbconfig.php";//database connection
   error_reporting(E_ALL); 

   $query = "SELECT * FROM users where id='$id'";
   $result = mysql_query($query);
   $row = mysql_fetch_array($result);
   ?>
  <form method="post" action="updatepengguna.php">
  <input type="hidden" name="id" value="<?php echo "$row[id]"?>">
    <tr>        
      <td>Username</td>
      <td>
        <input type="text" name="username" 
    size="20" value="<?php echo "$row[username]"?>">
      </td>
    </tr>
    <tr>
      <td>Nama Lengkap</td>
      <td>
        <input type="text" name="namalengkap" size="40" 
      value="<?php echo "$row[namalengkap]"?>">
      </td>
    </tr>
    <tr>        
      <td>NIK</td>
      <td>
        <input type="text" name="nik" 
    size="20" value="<?php echo "$row[nik]"?>">
      </td>
    </tr>
    <tr>        
      <td>Password</td>
      <td>
        <input type="text" name="password" 
    size="20" value="<?php echo "$row[password]"?>">
      </td>
    </tr>
    <tr>        
      <td>Level</td>
      <td>
        <input type="text" name="level" 
    size="20" value="<?php echo "$row[level]"?>">
      </td>
    </tr>
    <tr>
      <td align="right">
        <input type="submit" 
      name="submit value" value="Edit">
      </td>
    </tr>
  </form>
  </table>
 </td>
 </tr>
 </table>
 </body>
 </html>

Here I have updatepengguna.php

 <?

 include "dbconfig.php";
 $id = $_POST['id'];
 $username = $_POST["username"];
 $namalengkap = $_POST["namalengkap"];
 $nik= $_POST["nik"];
 $password= $_POST["password"];
 $level= $_POST["level"];
 $query = "UPDATE users SET username='$username',  namalengkap='$namalengkap', nik='$nik', password='$password', level='$level' WHERE id='$id'";
 mysql_query($query);
 echo mysql_error();
 header("location:daftarpengguna.php");
 ?>

I get this error:

Notice: Undefined variable: id in C:\xampp\htdocs\aes\editpengguna.php on line 20

What's the problem?

vhu
  • 12,244
  • 11
  • 38
  • 48
John
  • 45
  • 8
  • When you write a question about an error, __always__ include details of the error. Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Pedro Lobito Oct 25 '15 at 16:51
  • In your *updatepengguna.php* file, did you get all the values using `$_POST["XXX"]` or are you facing any issues receiving them? – Manikiran Oct 25 '15 at 16:52
  • 1
    POST variables transferred from browser to the server do not magically get stored in arbitrary variables. Your variables like `$username` do not make sense. You may want to take a look into what the super global variable `$_POST` contains instead. – arkascha Oct 25 '15 at 17:01
  • iam sorry i allready update my post, whats that error, i think it should be work, i have a `id` on my table, – John Oct 25 '15 at 17:06
  • Try this `` on line 20 – Manikiran Oct 25 '15 at 17:07
  • thanks Manikiran, but actually line 20 is `$query = "SELECT * FROM users where id ='$id'";` – John Oct 25 '15 at 17:10
  • 1
    `$id` is not defined so `$query = "SELECT * FROM users where id='$id'";` fails. You also are open to SQL injections. – chris85 Oct 25 '15 at 17:17
  • why is not defined? i already use this query to other class, and its work fine, about SQL injections,iam sorry but this project just to learn php and mysql, and iam newbie on it, – John Oct 25 '15 at 17:26
  • Why would it be defined? You never set it to anything. – chris85 Oct 25 '15 at 17:28
  • i dont understand, could yo more spesified, like use the code to set it, iam sorry but my head spinning – John Oct 25 '15 at 17:36
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – andrewsi Oct 26 '15 at 02:07

1 Answers1

1

Okay so lets go through the code and your error message you have 20 lines of code:

  <html>
  <head>
  </head>

 <body>
 <table border=1>
 <tr>
  <td align=center>Form Edit Employees Data</td>
 </tr>
 <tr>
  <td>
   <table>
   <?
   include "dbconfig.php";//database connection
   error_reporting(E_ALL); 

   $query = "SELECT * FROM users where id='$id'";

I presume include "dbconfig.php"; has three lines of code.

So line 20 is

$query = "SELECT * FROM users where id='$id'";

So your error message

Notice: Undefined variable: id in C:\xampp\htdocs\aes\editpengguna.php on line 20

is telling you that $id is not defined. Which if you look at the previous 20 lines of code it is not.

Maybe you meant to do

$id = (int)$_GET['id'];

earlier in your code? Possibly POST but that is the error.

You also should look into using PDO or mysqli as your driver. The mysql_ functions are deprecated and don't support prepared/paramaterized queries. You could use http://php.net/manual/en/function.mysql-real-escape-string.php. Since you are learning I'd recommend learning with a live and up to date driver though.

chris85
  • 23,846
  • 7
  • 34
  • 51
  • yesterday i got these error about mysql is deprecated, now i understand, thank you so much. yes i used that now it edit.php is work, but when click on update, it redirect to update.php but its blank page, which should redirect to daftarpengguna.php, because i use `header("location:daftarpengguna.php");` on update.php, why its turn blank? – John Oct 25 '15 at 17:45
  • its not resolved yet bro, its turn a blank page, but thanks a lot about your explanation, – John Oct 25 '15 at 17:52
  • When does it give you the blank page on the form submission or on the main page? – chris85 Oct 25 '15 at 17:53
  • The update page actually has the same issue as well. Add `$id = (int)$_POST['id'];` on updatepengguna.php. – chris85 Oct 25 '15 at 17:54
  • it give me blank page on the form submission, – John Oct 25 '15 at 17:55
  • Okay, that page has the same issue. You should add the error reporting on there as well. – chris85 Oct 25 '15 at 17:57
  • yes after you give me explanation up there, i already add it to updatepengguna.php to, but even i didnt give `$id = (int)$_POST['id'];`(undo) on the updatepengguna.php it give me same thing, – John Oct 25 '15 at 18:01
  • Oo also everything you are trying to update is also undefined. You aren't getting notices/warnings about that? – chris85 Oct 25 '15 at 18:08
  • yes, i didnt get notice anything, its just turn blank, but now i already update the post, and i already set it, but it turn me the same thing, what i miss now bro? – John Oct 25 '15 at 18:17
  • Is it redirecting? Is the blank page `daftarpengguna.php` or `updatepengguna.php`? – chris85 Oct 25 '15 at 18:21
  • i ask on different forum, some body says `give
    a name as well
    ` whats that mean?
    – John Oct 25 '15 at 18:29
  • No idea why you'd be given that advice. Wouldn't affect the processing or cause a fatal error on `updatepengguna.php`. Can you update `updatepengguna.php` so it is just ` echo 'test';` and then execute the form again? – chris85 Oct 25 '15 at 18:35
  • 1
    @John This is a separate issue. I recommend asking a new question on StackOverflow for this, and marking chris85's answer on this one as correct, since it does solve the issue you describe in the question. – 11684 Oct 25 '15 at 18:36
  • @chris85 yes i just trying that and it show me the echo `test`, @11684 how to marking the answer? iam newbie on this forum, – John Oct 25 '15 at 18:43
  • Okay, revert that than. Is the page's status code 500, 200, other? – chris85 Oct 25 '15 at 18:46
  • @chris85 its solved bro, big thanks to you, it blank page because the code `` and i already change to ` – John Oct 25 '15 at 18:47
  • @john http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – 11684 Oct 25 '15 at 18:48
  • Oh, alright. Glad to help. I thought you had short tags enabled because your other code used that. – chris85 Oct 25 '15 at 18:49
  • i already marked accepted your answer, thanks a lot,and iam so happy finally it work after i googling it a whole day :D – John Oct 25 '15 at 18:49