Reply
Newbie: Mysql Example Edit Table....
Old 05-06-2004, 06:05 AM Newbie: Mysql Example Edit Table....
nasos's Avatar
Junior Talker

Posts: 2
Hi webmasters,

I would like to start PHP with mysql with a simple example by my own , so i made the following that tries to edit and remove a row from a table.

the table is named: shop
the database: test2
fields: article,dealer,price

So, I want to have the row displayed and beside every row to have a checkbox (not checked).
There will be two additional buttons below the rows that will display EDIT and DELETE (row).

I started with this:

<html>
<head>
<title>::: EDIT DATABASE :::</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#999999">
PHP Code:
<?php

 $connection 
mysql_connect("localhost"""""); 

   
mysql_select_db("test2"); 

   { 
      
$result mysql_query("SELECT article,dealer,price FROM shop"); 
      
      
$num_rows mysql_num_rows($result);
      
$itemchoice 0;       

      if ((
$row  mysql_fetch_array($result)) != 0
      { 
         echo 
"<table border=0>"

         echo 
"<tr bgcolor=#cccccc> 
            <td align=center>Article</td> 
            <td align=center>Dealer</td> 
            <td align=center>Price</td> 
            </tr>"


         do 
         { 
            
$name $row['article']; 
            
$owner $row['dealer']; 
            
$species $row['price']; 
            
$itemchoice $itemchoice 1;    
             
            echo 
"<form method='post' action='<? $PHP_SELF ?>'> 
               <tr> 
               <td align='center'> 
               <input disabled type='text' name='article' value='" 
$name "' size='30' maxlength='30'></td> 
               <td align='center'> 
               <input disabled type='text' name='dealer' value='" 
$owner "'></td> 
               <td align='center'> 
               <input disabled type='text' name='price' value='" 
$species "' size='10' maxlength='10'></td> 
               <td align='center'> 
               <input style='background-color:#CCFF33' type=checkbox name='item' id='$itemchoice'>
               </tr></font>"

           } 
              
         while (
$row mysql_fetch_array($result)); 

         echo 
"<form method='post' action='<? $_POST[$PHP_SELF] ?>'>"
         echo 
"<tr><td><input type='submit' name='delete' value='Delete'>"
         echo 
"<input type='submit' name='edit' value='Edit'></td></tr></table></form>";          
      } 

      else 
      {    
      echo 
"No records found!"
      }    
   } 
      
  if (isset(
$_REQUEST["delete"]);
$sql_del mysql_query("DELETE FROM shop WHERE &itemchoice = ".$HTTP_POST_VARS['$item']); 
      }
if (!
mysql_query ($sql_del)) { 
    
$success 0
    
$msg 'Record could not be deleted'
    } 
else { 
       
$msg 'Record for item '.$HTTP_POST_VARS['item'].' was deleted. Refreshing display...'
     } 

?>
</body>
</html>


This of course doesnt work ... so any help will be appreciated.!

Last edited by nasos : 05-06-2004 at 06:11 AM.
nasos is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 05-06-2004, 07:28 AM
RonnieTheDodger's Avatar
Extreme Talker

Posts: 150
Location: Central USA
You might want to try getting rid of the second <Form> tag. I have not ran your code yet, but that stuck out like a sore thumb.

Remove the echo statement following the while statement.

PHP Code:
while ($row = mysql_fetch_array($result)); 

         echo "<form method='post' action='<? $_POST[$PHP_SELF?>'>";
__________________
Ronnie T. Dodger

[Webstractions - Web Development] [Band of Gonzos - Webmaster Forum]
RonnieTheDodger is offline
Reply With Quote
View Public Profile Visit RonnieTheDodger's homepage!
 
Old 05-06-2004, 09:33 AM EDIT ta
nasos's Avatar
Junior Talker

Posts: 2
Thanks for replying,

well i followed your suggestion ...
but i d like to add this.

I know that I dont know much, but should I use an array for check buttons and use a variable for each when checked? It will be validated only when the DELETE button is pressed.

i removed the part:

PHP Code:
 if (isset($_POST["delete"]);
//update the database (corresonding to your select-query)
     
$sql_del mysql_query("DELETE FROM shop WHERE &itemchoice = " .$HTTP_POST_VARS['$item']);
      
      }
if (!
mysql_query ($sql_del)) { 
    
$success 0
    
$msg 'Record could not be deleted'
    } 
else { 
       
$msg 'Record for item '.$HTTP_POST_VARS['item'].' was deleted. Refreshing display...'
     } 
and added the array in javascript:
PHP Code:
 echo "<form method='post' action='<? $PHP_POST ?>'> 
               <tr> 
               <td align='center'> 
               <input disabled type='text' name='article' value='" . $name . "' size='30' maxlength='30'></td> 
               <td align='center'> 
               <input disabled type='text' name='dealer' value='" . $owner . "'></td> 
               <td align='center'> 
               <input disabled type='text' name='price' value='" . $species . "' size='10' maxlength='10'></td> 
               <td align='center'> 
               <input style='background-color:#CCFF33' type=checkbox value='$message'>"
               ?>
               <SCRIPT language=JavaScript><!--
               var foo=1;function CheckAll(){
                var i=0;for(i=0;i<document.list.elements.length;i++){
                     if(document.list.elements[i].name=='message'){
                             document.list.elements[i].checked=foo  }
                }; 
                 if(foo == 1){
                    foo=0}
                       else {
                          foo=1}
                  }
                //--></SCRIPT>
               <?php
               
echo "</tr></font>"
           }


and it is working ... no the buttons arent working !
nasos is offline
Reply With Quote
View Public Profile
 
Old 05-08-2004, 09:27 PM
RonnieTheDodger's Avatar
Extreme Talker

Posts: 150
Location: Central USA
I am not very good at javascript. But I do know that you need to call the function CheckAll() at some point to run it.

You might want to go to some of the scripting sites and look for a routine that you can adapt for your purposes....something that is already written and known to work. If you are just starting to learn php, there is no better way than learning by example.

Also, I do not know why you were mixing javascript into your php like that. Try to stick with the php.
__________________
Ronnie T. Dodger

[Webstractions - Web Development] [Band of Gonzos - Webmaster Forum]
RonnieTheDodger is offline
Reply With Quote
View Public Profile Visit RonnieTheDodger's homepage!
 
Reply     « Reply to Newbie: Mysql Example Edit Table....
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.15014 seconds with 12 queries