Jump to content

Recommended Posts

I rewritten parts of my script that should add, edit and delete items from my mysql database. A simple task for most of you guys but I am running into trouble. Please tell me how to fix this script so I can learn from it..

 

Klanten.php, where I display and add data. This works fine.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Toevoegen</title>
</head>

<body>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Toevoegen</title>
</head>

<body>

<?php
# errors weergeven
ini_set('display_errors',1); // 1 == aan , 0 == uit
error_reporting(E_ALL | E_STRICT);
?>

<form method="post"> 
<table>
    <tr>
        <td>Klantnummer:</td>
        <td><input type="text" name="Klantnummer" /></td>
    </tr>
    <tr>
        <td>Bedrijf:</td>
        <td><input type="text" name="Bedrijf" /></td>
    </tr>
    <tr>
        <td>Contactpersoon:</td>
        <td><input type="text" name="Contactpersoon" /></td>
    </tr>
    <tr>
        <td> </td>
        <td><input type="submit" name="submit" value="add" /></td>
    </tr>
</table>
<?php
if (isset($_POST['submit']))
    {       
    include 'db_config.php';
                    $Klantnummer=$_POST['Klantnummer'] ;
                    $Bedrijf=$_POST['Bedrijf'] ;
                    $Contactpersoon= $_POST['Contactpersoon'] ;                    
                                                        
                                                
         mysql_query("INSERT INTO `klanten`(Klantnummer,Bedrijf,Contactpersoon) 
         VALUES ('$Klantnummer','$Bedrijf','$Contactpersoon')");

   }
?>
</form>
<table border="1">
    
            <?php
            error_reporting(E_ALL);
            include("db_config.php");
            
                
            $result=mysql_query("SELECT * FROM klanten");
            
            while($test = mysql_fetch_array($result))
            {
                $Klantnummer = $test['Klantnummer'];    
                echo "<tr align='center'>";
                echo"<td><font color='black'>" .$test['Klantnummer']."</font></td>";    
                echo"<td><font color='black'>" .$test['Bedrijf']."</font></td>";
                echo"<td><font color='black'>" .$test['Contactpersoon']."</font></td>";
                echo"<td> <a href ='edit.php?ID=$Klantnummer'>Edit</a>";
                echo"<td> <a href ='del.php?ID=$Klantnummer'><center>Delete</center></a>";
                                    
                echo "</tr>";
            }
            
            ?>
</table>

</body>
</html>                                                                            

del.php where offcouse I delete clients, it did work in the past before I tried to modiy the primary ID to Klantnummer

 

It shows:

Notice: Undefined variable: ID in /home/schoolme/public_html/ret/del.php on line 12

Warning: Cannot modify header information - headers already sent by (output started at /home/schoolme/public_html/ret/del.php:12) in /home/schoolme/public_html/ret/del.php on line 15

<?php
    # errors weergeven
    ini_set('display_errors',1); // 1 == aan , 0 == uit
    error_reporting(E_ALL | E_STRICT);
    
    include("db_config.php");  

    $id =$_REQUEST['ID'];
    
    
    // sending query
    mysql_query("DELETE FROM klanten WHERE Klantnummer = '$Klantnummer'")
    or die(mysql_error());      
    
    header("Location: klanten.php");
?>

Edit.php seems to work as it brings me to a page that shows all the data.

<?php
# errors weergeven
ini_set('display_errors',1); // 1 == aan , 0 == uit
error_reporting(E_ALL | E_STRICT);
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table>
  <tr>
    <td align="center">EDIT DATA</td>
  </tr>
  <tr>
    <td>
      <table border="1">
      <?
      include"db_config.php";//database connection
      $order = "SELECT * FROM klanten";
      $result = mysql_query($order);
      while ($row=mysql_fetch_array($result)){
        echo ("<tr><td>$row[Klantnummer]</td>");
        echo ("<tr><td>$row[Bedrijf]</td>");
        echo ("<td>$row[Contactpersoon]</td>");
        echo ("<td><a href=\"edit_form.php?Klantnummer=$row[Klantnummer]\">Edit</a></td></tr>");
      }
      ?>
      </table>
    </td>
   </tr>
</table>
</body>
</html>

Then we click edit to go to edit_form.php which shows this error

 

Notice: Undefined variable: Klantnummer in /home/schoolme/public_html/ret/edit_form.php on line 22

 

But the biggest problem here which in any of the scripts I was not able to fix is the fact that it only shows the blanc fields and not the existing data of that entry that I want to edit..

<?php
    ini_set('display_errors',1); // 1 == aan , 0 == uit
error_reporting(E_ALL | E_STRICT);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Form Edit Data</title>
</head>

<body>
<table border=1>
  <tr>
    <td align=center>Form Edit Employees Data</td>
  </tr>
  <tr>
    <td>
      <table>
      <?
      include "db_config.php";//database connection
      $order = "SELECT * FROM klanten where Klantnummer='$Klantnummer'";
      $result = mysql_query($order);
      $row = mysql_fetch_array($result);
      ?>
      <form method="post" action="edit_data.php">
        <tr>
          <td>Klantnummer</td>
          <td>
            <input type="text" name="Klantnummer" size="40" 
          value="<? echo "$row[Klantnummer]"?>">
          </td>
        </tr>
         <tr>
          <td>Bedrijf</td>
          <td>
            <input type="text" name="Bedrijf" size="40" 
          value="<? echo "$row[Bedrijf]"?>">
          </td>
        </tr>
        <tr>
          <td>Contactpersoon</td>
          <td>
            <input type="text" name="Contactpersoon" size="40" 
          value="<? echo "$row[Contactpersoon]"?>">
          </td>
        </tr>
        <tr>
          <td align="right">
            <input type="submit" 
          name="submit value" value="Edit">
          </td>
        </tr>
      </form>
      </table>
    </td>
  </tr>
</table>
</body>
</html>

If I click Edit there to save the new data it goes to edit data which shows loads of error

 

Notice: Undefined variable: Klantnummer in /home/schoolme/public_html/ret/edit_data.php on line 8

Notice: Undefined variable: Bedrijf in /home/schoolme/public_html/ret/edit_data.php on line 9

Notice: Undefined variable: Contactpersoon in /home/schoolme/public_html/ret/edit_data.php on line 11

Notice: Undefined variable: Klantnummer in /home/schoolme/public_html/ret/edit_data.php on line 11

Warning: Cannot modify header information - headers already sent by (output started at /home/schoolme/public_html/ret/edit_data.php:8) in /home/schoolme/public_html/ret/edit_data.php on line 13

<?
ini_set('display_errors',1); // 1 == aan , 0 == uit
error_reporting(E_ALL | E_STRICT);
//edit_data.php
include "db_config.php";
$order = "UPDATE klanten 
          SET Klantnummer='$Klantnummer', 
              Bedrijf='$Bedrijf',
              Contactpersoon='$Contactpersoon' 
          WHERE 
          Klantnummer='$Klantnummer'";
mysql_query($order);
header("location:edit.php");
?>

Please help, if someone can tell me what to fix I might be able to learn from that..

 

Much appriciated!

Link to comment
https://forums.phpfreaks.com/topic/282310-add-edit-delete-script-problems/
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.