Undefined index

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • leesyaa
    New Member
    • Jan 2014
    • 20

    #1

    Undefined index

    I want to update my data, so I have this code

    admin.php
    Code:
    <?php
    include('config.php');
    
    	$bil = 0;
    	//$option = '';
    	$results = mysql_query("SELECT * FROM pemohon");
    	//$option .='<select>';
    	//$count=mysql_num_rows($results);
    	//
    	while ($row = mysql_fetch_array($results, MYSQL_ASSOC))
    	{
    	$bil++;
    	
    	
    
    	echo "<form name='update $bil' action=update.php method=post>"; ?>
    	<tr>
    		<td><center><?php echo $bil; ?></center></td>
    		<td><?php echo $row['name']; ?></td>
    		<td><?php echo $row['unit']; ?></td>
    		<td><?php echo $row['telefon']; ?></td>
    		<td><?php echo $row['email']; ?></td>
    		<td><?php echo $row['data']; ?></td>
    		<td><?php echo $row['tujuan']; ?></td>
    		<td><?php echo $row['tindakan']; ?></td>
    		<td><?php echo $row['agihan']; ?></td>
    		<td><input type='submit' name='submit' value='kemaskini'>
    			<input type='hidden' name='id' value=<?php echo $row['id']; ?></td>
    		
    		<!--<td><center><select name="myselect" id="myselect" size="0">
    		<option <//?php if( $row['tindakan'] == '-'){ echo "selected";}?> >-</option>
    		<option <//?php if( $row['tindakan'] == 'lulus'){ echo "selected";}?> >Diluluskan</option>
    		<option <//?php if( $row['tindakan'] == 'taklulus'){ echo "selected";}?> >Tidak Diluluskan</option>
    		</select></center></td>
    		<td><center><select name="myselect" id="myselect" size="0">
    		<option <//?php if( $row['agihan'] == '-'){ echo "selected";}?> >-</option>
    		<option <//?php if( $row['agihan'] == 'a'){ echo "selected";}?> >Encik A</option>
    		<option <//?php if( $row['agihan'] == 'b'){ echo "selected";}?> >Puan B</option>
    		</select></center></td>-->
    	</tr>
    	
    <?php 
    }
    update.php
    Code:
    <div id="welcome">
        <?php
        include('config.php');
    
        $sql = "SELECT * FROM pemohon WHERE id='".$_POST['id']."'";
        $results = mysql_query($sql);
    
        $row = mysql_fetch_array($results);
        //$user = $row['username'];
    
    
        $id = $row['id'];
        $name = $row['name'];
        $jawatan = $row['jawatan'];
        $unit = $row['unit'];
        $kementerian = $row['kementerian'];
        $telefon = $row['telefon'];
        $faks = $row['faks'];
        $email = $row['email'];
        $data = $row['data'];
        $tujuan = $row['tujuan'];
        $tarikh = $row['tarikh'];
        $tindakan = $row['tindakan'];
    
        $id = $_POST['id'];
        $submit = $_POST['submit'];
        ?>
    
        <form name="update" action="update2.php" method="post">
            <table width="763" align="center">
                <tr>
                    <td>No. ID</td>
                    <td>:</td>
                    <td><?php echo "$id"; ?></td>
                </tr>
                <tr>
                    <td width="173">Nama Pemohon</td>
                    <td width="10">:</td>
                    <td width="564"><?php echo "$name"; ?></td>
                </tr>
                <tr>
                    <td>Jawatan</td>
                    <td>:</td>
                    <td><?php echo "$jawatan"; ?></td>
                </tr>
                <tr>
                    <td>Unit/Jabatan/Bahagian</td>
                    <td>:</td>
                    <td><?php echo "$unit"; ?></td>
                </tr>
                <tr>
                    <td>Kementerian/Institusi/Agensi</td>
                    <td>:</td>
                    <td><?php echo "$jawatan"; ?></td>
                </tr>
                <tr>
                    <td>No. Telefon</td>
                    <td>:</td>
                    <td><?php echo "$telefon"; ?></td>
                </tr>
                <tr>
                    <td>No. Faks</td>
                    <td>:</td>
                    <td><?php echo "$faks"; ?></td>
                </tr>
                <tr>
                    <td>E-mail</td>
                    <td>:</td>
                    <td><?php echo "$email"; ?></td>
                </tr>
                <tr>
                    <td>Data/Laporan Yang Dipohon</td>
                    <td>:</td>
                    <td><?php echo "$data"; ?></td>
                </tr>
                <tr>
                    <td>Tujuan</td>
                    <td>:</td>
                    <td><?php echo "$tujuan"; ?></td>
                </tr>
                <tr>
                    <td>Tarikh Permohonan</td>
                    <td>:</td>
                    <td><?php echo "$tarikh"; ?></td>
                </tr>
                <tr>
                    <td>Tindakan</td>
                    <td>:</td>
                    <td><label>
                        <select name="tindakan" id="tindakan">
                        <option selected="selected"></option>
                        <option>Diluluskan</option>
                        <option>Tidak Diluluskan</option>
                        </select>
                        </label>
                </tr>
                <tr>
                    <td>Agihan</td>
                    <td>:</td>
                    <td><label>
                        <select name="agihan" id="agihan">
                        <option selected="selected"></option>
                        <option>Pn. Suryana Binti Embong</option>
                        <option>Pn. Rohaida Binti Ali</option>
                        <option>En. Hasril Rezuan Bin Suboh</option>
                        </select>
                        </label>
                </tr>
    
        <p align="center">
        <input type="hidden" name="noid" value="<?php echo $id; ?>">
        <input type="hidden" name="name" value="<?php echo $name; ?>">
        <input name="submit" type="submit" id="submit" value="Kemaskini" />
       
        </p>
        </form>
        </div>
    update2.php
    Code:
    <?php
    
    $name = $_POST['name'];
    
    //if (!isset($_POST['id'])) {
          $id = $_POST['id'];
    //} else {
    //      $id = 999999999;
    //}
    
    //$tarikh = $_POST['tarikh'];
    $tindakan = $_POST['tindakan'];
    $agihan = $_POST['agihan'];
    
    $connection = mysql_connect("localhost","root","") or die ("Could Not Connect To Server".mysql_error());
    $selection = mysql_select_db("permohonan_data") or die ("Could Not Connect To Database".mysql_error());
    
    //$sql= "UPDATE pemohon SET tarikh='$tarikh', tindakan='$tindakan', agihan='$agihan', WHERE id='$noid'";
    $sql= "UPDATE pemohon SET tindakan='$tindakan', agihan='$agihan', WHERE id='$id'";
    $result = mysql_query($sql) or die (mysql_error());
    
    if($result)
    {   
    ?>
        <script language="javascript">
        alert("Permohonan <?php echo "$name"; ?> telah dikemaskini");
        location.href="admin.php";
        </script>
    
    <?php
    }
    
    ?>
    but it turned out to be errors which I didn't know what to do. please help me

    this is the errors,

    Notice: Undefined index: id in C:\wamp\www\FOR M\update2.php on line 6

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id=''' at line 1
  • edgeworks
    New Member
    • Feb 2014
    • 2

    #2
    POST is not sending any variable named "id" , please check you HTML form.

    Comment

    • leesyaa
      New Member
      • Jan 2014
      • 20

      #3
      but I have the id? :(

      Code:
      <div id="welcome">
          <?php
          include('config.php');
      
          $sql = "SELECT * FROM pemohon WHERE id='".$_POST['id']."'";
          $results = mysql_query($sql);
      
          $row = mysql_fetch_array($results);
          //$user = $row['username'];
      
      
          $id = $row['id'];
          $name = $row['name'];
          $jawatan = $row['jawatan'];
          $unit = $row['unit'];
          $kementerian = $row['kementerian'];
          $telefon = $row['telefon'];
          $faks = $row['faks'];
          $email = $row['email'];
          $data = $row['data'];
          $tujuan = $row['tujuan'];
          $tarikh = $row['tarikh'];
          $tindakan = $row['tindakan'];
      
          $id = $_POST['id'];
          $submit = $_POST['submit'];
          ?>
      
          <form name="update" action="update2.php" method="post">
              <table width="763" align="center">
                  <tr>
                      <td>No. ID</td>
                      <td>:</td>
                      <td><?php echo "$id"; ?></td>
                  </tr>
                  <tr>
                      <td width="173">Nama Pemohon</td>
                      <td width="10">:</td>
                      <td width="564"><?php echo "$name"; ?></td>
                  </tr>
                  <tr>
                      <td>Jawatan</td>
                      <td>:</td>
                      <td><?php echo "$jawatan"; ?></td>
                  </tr>
                  <tr>
                      <td>Unit/Jabatan/Bahagian</td>
                      <td>:</td>
                      <td><?php echo "$unit"; ?></td>
                  </tr>
                  <tr>
                      <td>Kementerian/Institusi/Agensi</td>
                      <td>:</td>
                      <td><?php echo "$jawatan"; ?></td>
                  </tr>
                  <tr>
                      <td>No. Telefon</td>
                      <td>:</td>
                      <td><?php echo "$telefon"; ?></td>
                  </tr>
                  <tr>
                      <td>No. Faks</td>
                      <td>:</td>
                      <td><?php echo "$faks"; ?></td>
                  </tr>
                  <tr>
                      <td>E-mail</td>
                      <td>:</td>
                      <td><?php echo "$email"; ?></td>
                  </tr>
                  <tr>
                      <td>Data/Laporan Yang Dipohon</td>
                      <td>:</td>
                      <td><?php echo "$data"; ?></td>
                  </tr>
                  <tr>
                      <td>Tujuan</td>
                      <td>:</td>
                      <td><?php echo "$tujuan"; ?></td>
                  </tr>
                  <tr>
                      <td>Tarikh Permohonan</td>
                      <td>:</td>
                      <td><?php echo "$tarikh"; ?></td>
                  </tr>
                  <tr>
                      <td>Tindakan</td>
                      <td>:</td>
                      <td><label>
                          <select name="tindakan" id="tindakan">
                          <option selected="selected"></option>
                          <option>Diluluskan</option>
                          <option>Tidak Diluluskan</option>
                          </select>
                          </label>
                  </tr>
                  <tr>
                      <td>Agihan</td>
                      <td>:</td>
                      <td><label>
                          <select name="agihan" id="agihan">
                          <option selected="selected"></option>
                          <option>Pn. Suryana Binti Embong</option>
                          <option>Pn. Rohaida Binti Ali</option>
                          <option>En. Hasril Rezuan Bin Suboh</option>
                          </select>
                          </label>
                  </tr>
      
          <p align="center">
          <input type="hidden" name="noid" value="<?php echo $id; ?>">
          <input type="hidden" name="name" value="<?php echo $name; ?>">
          <input name="submit" type="submit" id="submit" value="Kemaskini" />
         
          </p>
          </form>
          </div>

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        but I have the id? :(
        where ?

        Comment

        • leesyaa
          New Member
          • Jan 2014
          • 20

          #5
          its not the $id = $_POST['id']?

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            but PHP tells you there is no $_POST['id'] (and I agree with that).

            Comment

            • leesyaa
              New Member
              • Jan 2014
              • 20

              #7
              @Dormilich can you please tell me where I code wrong? :(

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                in your HTML form.

                Comment

                • Luuk
                  Recognized Expert Top Contributor
                  • Mar 2012
                  • 1043

                  #9
                  Change line #4 from
                  Code:
                  $id = $_POST['id'];
                  to something like:
                  Code:
                  if (!isset($_POST['id']) {
                         $id = $_POST['id']; 
                  } else {
                         $id = 999999999;
                  }
                  and........
                  Dont ask where '999999999' is coming from ;)

                  Comment

                  • leesyaa
                    New Member
                    • Jan 2014
                    • 20

                    #10
                    @Luuk it's still displaying the same error :(

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      show your actual code.

                      Comment

                      • leesyaa
                        New Member
                        • Jan 2014
                        • 20

                        #12
                        @dormilich I have update the code, see above :)

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          weeelll, if you uncomment Luuk’s code, essentially resulting in the original code, what else do you expect than the original error?

                          Comment

                          • leesyaa
                            New Member
                            • Jan 2014
                            • 20

                            #14
                            @Dormilich, ah I forget to mention, but I test Luuk's code already, which is resulting the same error.

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              then you’re not or wrongly using Luuk’s code. it is coded exactly to prevent that error. (that it does not fix the real cause is a different matter)

                              Comment

                              Working...