Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 05-05-2009
Member
 
Join Date: Nov 2008
Posts: 1,066
SQL query to delete a row in database

What I would like to achieve is to perform an SQL query according to retrieve a value in a javascript function to delete a row in a database. The javascript function is in the test.php file and will display the function function.php.

function.php file:

PHP Code:
function displays_bdd_request_modifier($request_modified)
{    
    
$execution mysql_query($request_modified);
    if(!
$execution)
    {
        echo 
"<font color='red'>Request invalid</font>".mysql_error();    
        exit();    
    }
 
    
$nblines=mysql_num_rows($execution); // numbers of rows in the table
    
$nbfields=mysql_num_fields($execution); // number of fields
    
    
echo "<table border CELLSPACING=\"0\" ><tr>";
    echo 
"<input type='submit' value='Add' onclick='    Add();'>";
    
    
//will display the names of columns
    
echo "<thead>";
    echo 
"<tr><td></td><td></td>";
    echo 
"<th>Id_cle</th>";
    echo 
"<th>Subnet</th>";
    echo 
"<th>Site</th>";
    echo 
"</tr>";
    echo 
"</thead>";
    
    for(
$i=0$i $nblines$i++) 
    {
        
$line mysql_fetch_row($execution); // retrieves data from the line
        
echo "<tr>";                
        
$tab = array();
        
        
//aid is the display of icons on each row of a table display
        
echo "<td align='center'>";
        echo 
"<a href='modifier.php'";
        echo 
"onclick='return confirmLink(this)'>";
        echo 
"<img class='icon' width='16' height='16' src='images/b_edit.png' alt='Modifier' title='Modifier' /></a></td>";
        
        echo 
"<td align='center'>";
        echo 
"<a onclick='Remove($line[0])'>";
        echo 
"<img class='icon' width='16' height='16' src='images/b_drop.png' alt='Remove' title='Remove' /></a></td>";
        
        for(
$j=0$j $nbfields$j++) 
        {        
            switch(
$tab[$i])
            {            
                case (
$tab[$i] <= 100 && $tab[$i] >= 86):
                echo 
"<td align=\"center\"><font color='green'>$line[$j]</font></td>";    
                break;
                    
                case (
$tab[$i] <= 85 && $tab[$i] >= 45):                            
                echo 
"<td align=\"center\"><font color='orange'>$line[$j]</font></td>";                                    
                break;
                    
                case(
$tab[$i]<= 44 && $tab[$i] >= 0):                                
                echo 
"<td align=\"center\"><font color='red'>$line[$j]</font></td>";                                
                break;            
            }
        }    
    echo 
"</tr>";    
    }
    echo 
"</table>";

test.php file:

PHP Code:
<html> 
    <body>
        <head>
            <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
            <link rel="stylesheet" type="text/css" title="Style" href="style.css" />
        </head>
        
        <script type="text/javascript">
            function printWindow()
            {
                bV = parseInt(navigator.appVersion);
                if (bV >= 4) window.print();
            }
            function Add()
            {
                document.location="add.php";
            }
            function Modifier()
            {
                document.location="modifier.php";
            }
            function Remove(choice)
            {
                var choice = confirm('Are you sure you want to delete this line' + choice);
                
                if (choice==true)
                {
                    alert("Yes");
                    return true;
                                        /I get good value to put in a SQL query to delete
                }
                else
                {
                    return false;
                }    
            }
        </script>
        
        <table>
            <td>
                <div>                    
                    <?php
                        
include 'function.php';
                        
bdd_network_connection();
                        
                        
$request_modifier "SELECT * FROM site_subnet ORDER BY subnet";
                        
                        
$request_modifier stripslashes($request_modifier);
                        
                        if (!
$open_file fopen("W:/var/www/Stage v2/request_modifier.txt",'w'))
                        {
                            echo 
"Unable to open file.";
                            exit;
                        }
                        if (
fwrite($open_file$request_modifier) === FALSE
                        {
                            echo 
"Unable to write to the file.";
                            exit;
                        }
                        
fclose($open_file);
displays_bdd_request_modifier($request_modifier);
                        
mysql_close();
                        echo 
'<a href="subnet.php">Back</a>';
                        echo 
'<br>';
                        echo 
'</div>';
                        echo 
'</td>';
                        echo 
'</table>';
                    
?>
    </body>
<html>
Reply With Quote
  #2  
Old 05-05-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: SQL query to delete a row in database

The JavaScript code sends information to the PHP script at what point? I can not see it in the code that you showed
Reply With Quote
  #3  
Old 05-05-2009
Member
 
Join Date: Nov 2008
Posts: 1,066
Re: SQL query to delete a row in database

My code to display the elements of the database at a click the Delete link that is in the poster. When clicked the link I apply a function delete () which opens a confirm (OK Cancel) to confirm the selection. When choosing to let me perform a SQL query to delete the row in the database. Because for the moment the function performs a JavaScript alert saying yes and confirm if the right line that was selected.
Reply With Quote
  #4  
Old 05-05-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: SQL query to delete a row in database

To confirm the JavaScript deletion with your function you can make a form like this:

PHP Code:
<form action = "remove.php" method = "post" onsubmit = "return Delete (<? php echo $row [0];?>)" 
      <input type = "hidden" name = "id" value = "<? php echo $row [0];?>" /> 
      <input type = "submit" value = "Delete <? php echo $row [0];?>" /> 
  </form>
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "SQL query to delete a row in database"
Thread Thread Starter Forum Replies Last Post
Interrogating database with the query in MySQL Xiomar Software Development 3 21-12-2010 05:41 AM
Query for columns in oracle database Gerri Windows Software 4 10-02-2010 08:27 PM
Php database query garfield1 Software Development 3 05-12-2009 01:40 PM
Having asp form with multiple checkboxes to query the database. Broot Software Development 3 09-07-2009 05:12 PM
Query Was Empty- error after inseting data into database Gefry Software Development 2 18-05-2009 06:43 PM


All times are GMT +5.5. The time now is 02:07 AM.