Client Side Things
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesClient Side Things

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old October 17th, 2004, 06:30 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
Help needed on non - working php script

Hi everyone,

If someone has some time, would you please have a look at the scripts I have pasted below, and explain to me why it isn't working...I keep getting an error saying that 'Duplicate entry for key...' when I try to update, and if I try to delete it doesn't work.
If anyone would have the time, I would be very appreciative...
thanks in advance for any help forthcoming.
Kindest Regards
Paul Stewart
If you could please respond to paulstewart@maxnet.co.nz that would be great.

Main code

php Code:
Original - php Code
  1.  
  2. <html>
  3. <body>
  4. <?php
  5. require("database_abstraction.inc.php");
  6. $db = new removals_db();
  7. $db->Connect();
  8. if ($submit) {
  9.    if ($id) {         
  10.     if (!$db->UpdateDB('client',$id,'client_id'))
  11.       die($db->GetError());
  12.      
  13.     echo "Record Updated";
  14.   } else {
  15.  
  16.     if (!$db->AddToDB('client'))
  17.       die($db->GetError());
  18.      
  19.     echo "Record Added";
  20.   }
  21. } elseif ($delete) {
  22.     $sql = "DELETE FROM client WHERE client_id= $id";   
  23.     $result = mysql_query($sql);
  24.     echo "$sql Record deleted!<p>";
  25. } else {
  26.   if (!$id) {
  27.     $result = mysql_query("SELECT * FROM client");
  28.     while ($myrow = mysql_fetch_array($result)) {
  29.  
  30.       printf("<a href="%s?id=%s">%s %s</a> n", $PHP_SELF, $myrow["client_id"], $myrow["client_first_name"], $myrow["client_last_name"]);
  31.  
  32.       printf("<a href="%s?id=%s&delete=yes">(DELETE)</a><br>", $PHP_SELF, $myrow["client_id"]);
  33.     }
  34.   }
  35.   ?>
  36.  
  37.   <form method="post" action="<?php echo $PHP_SELF?>">
  38.   <?php
  39.   if ($id) {
  40.     $sql = "SELECT * FROM client WHERE client_id=$id";
  41.     $result = mysql_query($sql);
  42.     $myrow = mysql_fetch_array($result);
  43.    
  44.    
  45.     $client_id = $myrow["client_id"];
  46.     $client_first_name = $myrow["client_first_name"];
  47.     $client_last_name = $myrow["client_last_name"];
  48.     $client_building_no = $myrow["client_building_no"];
  49.     $client_street = $myrow["client_street"];
  50.    
  51.     $client_suburb = $myrow["client_suburb"];
  52.     $client_city = $myrow["client_city"];
  53.     $client_state = $myrow["client_state"];
  54.     $client_post_code = $myrow["client_post_code"];
  55.     $client_phone = $myrow["client_phone"];
  56.    
  57.     $client_mobile_phone = $myrow["client_mobile_phone"];
  58.     $client_email = $myrow["client_email"];
  59.     $client_notes = $myrow["client_notes"];
  60.    
  61.     // print the id for editing
  62.     ?>
  63.    
  64.     <?php
  65.   }
  66.   ?>
  67.  
  68.  
  69.  
  70.   <div id="Layer1" style="position:absolute; left:137px; top:22px; width:861px; height:88px; z-index:1">
  71.   <hr>
  72.   <p><b><font face="Arial, Helvetica, sans-serif" size="6">Client Administration</font></b></p>
  73. </div>
  74.   <div id="Layer3" style="position:absolute; left:136px; top:118px; width:718px; height:516px; z-index:3">
  75.     <table width="75%" border="0" height="75">
  76.       <tr>
  77.         <td width="24%"><font face="Arial, Helvetica, sans-serif">Client ID:</font></td>
  78.         <td width="76%">
  79.           <input type="text" name="client_id" size="20" value="<?php echo $client_id ?>">
  80.         </td>
  81.       </tr>
  82.       <tr>
  83.         <td width="24%"><font face="Arial, Helvetica, sans-serif">First Name:</font></td>
  84.         <td width="76%">
  85.           <input type="text" name="client_first_name" size="45" value="<?php echo $client_first_name ?>">
  86.         </td>
  87.       </tr>
  88.       <tr>
  89.         <td width="24%"><font face="Arial, Helvetica, sans-serif">Last Name:</font></td>
  90.         <td width="76%">
  91.           <input type="text" name="client_last_name" size="45" value="<?php echo $client_last_name ?>">
  92.         </td>
  93.       </tr>
  94.       <tr>
  95.         <td width="24%"><font face="Arial, Helvetica, sans-serif">Building No:</font></td>
  96.         <td width="76%">
  97.           <input type="text" name="client_building_no" size="20" value="<?php echo $client_building_no ?>">
  98.         </td>
  99.       </tr>
  100.       <tr>
  101.         <td width="24%"><font face="Arial, Helvetica, sans-serif">Street:</font></td>
  102.         <td width="76%">
  103.           <input type="text" name="client_street" size="45" value="<?php echo $client_street ?>">
  104.         </td>
  105.       </tr>
  106.       <tr>
  107.         <td width="24%"><font face="Arial, Helvetica, sans-serif">Suburb:</font></td>
  108.         <td width="76%">
  109.           <input type="text" name="client_suburb" size="30" value="<?php echo $client_suburb ?>">
  110.         </td>
  111.       </tr>
  112.       <tr>
  113.         <td width="24%"><font face="Arial, Helvetica, sans-serif">City:</font></td>
  114.         <td width="76%">
  115.           <input type="text" name="client_city" size="20" value="<?php echo $client_city ?>">
  116.         </td>
  117.       </tr>
  118.       <tr>
  119.         <td width="24%"><font face="Arial, Helvetica, sans-serif">State:</font></td>
  120.         <td width="76%">
  121.           <input type="text" name="client_state" size="20" value="<?php echo $client_state ?>">
  122.         </td>
  123.       </tr>
  124.       <tr>
  125.         <td width="24%"><font face="Arial, Helvetica, sans-serif">Post Code:</font></td>
  126.         <td width="76%">
  127.           <input type="text" name="client_post_code" size="20" value="<?php echo $client_post_code ?>">
  128.         </td>
  129.       </tr>
  130.       <tr>
  131.         <td width="24%"><font face="Arial, Helvetica, sans-serif">Phone:</font></td>
  132.         <td width="76%">
  133.           <input type="text" name="client_phone" size="20" value="<?php echo $client_phone ?>">
  134.         </td>
  135.       </tr>
  136.       <tr>
  137.         <td width="24%"><font face="Arial, Helvetica, sans-serif">Mobile Phone:</font></td>
  138.         <td width="76%">
  139.           <input type="text" name="client_mobile_phone" size="20" value="<?php echo $client_mobile_phone ?>">
  140.         </td>
  141.       </tr>
  142.       <tr>
  143.         <td width="24%"><font face="Arial, Helvetica, sans-serif">Email:</font></td>
  144.         <td width="76%">
  145.           <input type="text" name="client_email" size="45" value="<?php echo $client_email ?>">
  146.         </td>
  147.       </tr>
  148.       <tr>
  149.         <td width="24%" height="85"><font face="Arial, Helvetica, sans-serif">Notes:</font></td>
  150.         <td width="76%" height="85">
  151.           <TEXTAREA name="client_notes" rows="3" cols="35" ><?php echo $client_notes ?></textarea>
  152.         </td>
  153.       </tr>
  154.     </table>
  155.      
  156.  
  157.  
  158.   <input type="Submit" name="submit" value="Enter information">
  159.   </form>
  160. <?php
  161. }
  162. ?>
  163. </body>
  164. </html>


Database abstraction code
php Code:
Original - php Code
  1.  
  2. <?php
  3. Class removals_db
  4. {
  5.   var $SQLStatement = "";
  6.   var $Error = "";   
  7.  
  8.   function Connect()
  9.   {
  10.     include("db_password.inc.php");   
  11.     $this->db = mysql_connect($mysql_host,$mysql_user,$mysql_passw  ord) or die("MYSQL ERROR: ".mysql_error());
  12.     mysql_select_db($mysql_db) or die("MYSQL ERROR: ".mysql_error());
  13.   }
  14.  
  15.   function Disconnect()
  16.   {
  17.     mysql_close($this->db) or die("MYSQL ERROR: ".mysql_error());
  18.   }
  19.  
  20.   function GetError()
  21.   {
  22.     return $this->Error;
  23.   }
  24.  
  25.   function GetLastSQL()
  26.   {
  27.     return $this->SQLStatement;
  28.   }
  29.  
  30.  
  31.   function AddToDB($tbl)
  32.   {
  33.     $sql_columns = array();
  34.     $sql_columns_use = array();
  35.     $sql_value_use = array();
  36.  
  37.     $pull_cols = mysql_query("SHOW COLUMNS FROM ".$tbl) or die("MYSQL ERROR: ".mysql_error());
  38.  
  39.     while ($columns = mysql_fetch_assoc($pull_cols))
  40.       $sql_columns[] = $columns["Field"];
  41.     foreach( $_POST as $key => $value )
  42.     {
  43.       if ( in_array($key, $sql_columns)&& trim($value) )
  44.       {
  45.         if ($value == "DATESTAMP")
  46.           $sql_value_use[] = "NOW()";
  47.         else
  48.         {
  49.           if ( is_numeric($value) )
  50.             $sql_value_use[] = $value;
  51.           else
  52.             $sql_value_use[] =( get_magic_quotes_gpc() ) ?"'".$value."'" : "'".addslashes($value)."'";
  53.         }
  54.         $sql_columns_use[] = $key;
  55.       }
  56.     }
  57.     if ( (sizeof($sql_columns_use) == 0) ||(sizeof($sql_value_use) == 0) )
  58.     {
  59.       $this->Error = "Error: No values were passed that matched any columns.";
  60.       return false;
  61.     }
  62.     else
  63.     {
  64.       $this->SQLStatement = "INSERT INTO ".$tbl." (".implode(",",$sql_columns_use).") VALUES (".implode