Jump to content

downah

Members
  • Posts

    127
  • Joined

  • Last visited

Posts posted by downah

  1. Hi, sorry I am new here and also very new to PHP & MySQL. this is another script that is trying to do the same thing (deleting rows with checkbox)

     

     

    <?php
    include "connect.php";
    
    $result = mysql_query("SELECT * FROM users");
    
    if(isset($_POST['delete'])){
       if(is_array($_POST['id'])){
         echo '<pre>';
         print_r($_POST);
         echo '<pre>';
         foreach($_POST['id'] as $id){
         $query = "DELETE FROM users WHERE id=".$id;
         mysql_query($query)or die(mysql_error()); 
         echo "User Deleted";
         
         }
    
    } 
    }
    ?>
    <form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">  
    <?
    echo "<table border='0'>
    <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Location</th>
    <th>Email</th>
    <th>Website</th>
    <th>userID</th>
    
    
    </tr>";
    
    while($row = mysql_fetch_array($result))
      {
    
      echo "<tr>";
      echo "<td>" . $row['firstname'] . "</td>";
      echo "<td>" . $row['lastname'] . "</td>";
      echo "<td>" . $row['location'] . "</td>";
      echo "<td>" . $row['email'] . "</td>";
      echo "<td>" . $row['website'] . "</td>";
      echo "<td>" . $row['userID'] . "</td>";
      ?> <td><input type="checkbox" name="id[]" value="<?php $row['id'] ?>" /></td> <?
      echo "</tr>";
      }
    echo "</table>";
      echo "<input type='submit' value='Delete Users' name='delete' />";
    
    
    
    
    mysql_close($con);
    ?> 
    

     

    this gives me this error:

     

    Array

    (

        [id] => Array

            (

                [0] =>

            )

     

        [delete] => Delete Users

    )

     

    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 '' at line 1

     

    And the first script does not seem to do anything at all, no error or anything.

  2. Hi guys,

     

    I am trying out this code but it does not work, any idea what I am doing wrong?

     

    <?php

    include 'connect.php';

    $tbl_name="users";

     

    $sql="SELECT * FROM $tbl_name";

    $result=mysql_query($sql);

     

    $count=mysql_num_rows($result);

     

    ?>

    <table width="400" border="0" cellspacing="1" cellpadding="0">

    <tr>

    <td><form name="form1" method="post" action="">

    <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">

    <tr>

    <td bgcolor="#FFFFFF"> </td>

    <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>

    </tr>

    <tr>

    <td align="center" bgcolor="#FFFFFF">#</td>

    <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>

    <td align="center" bgcolor="#FFFFFF"><strong>Firstname</strong></td>

    <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>

    <td align="center" bgcolor="#FFFFFF"><strong>Location</strong></td>

    <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>

    <td align="center" bgcolor="#FFFFFF"><strong>Website</strong></td>

    </tr>

    <?php

    while($rows=mysql_fetch_array($result)){

    ?>

    <tr>

    <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>

    <td bgcolor="#FFFFFF"><? echo $rows['userID']; ?></td>

    <td bgcolor="#FFFFFF"><? echo $rows['firstname']; ?></td>

    <td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>

    <td bgcolor="#FFFFFF"><? echo $rows['location']; ?></td>

    <td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>

    <td bgcolor="#FFFFFF"><? echo $rows['website']; ?></td>

    </tr>

    <?php

    }

    ?>

    <tr>

    <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>

    </tr>

    <?

     

    if($delete){

    for($i=0;$i<$count;$i++){

    $del_id = $checkbox[$i];

    $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";

    $result = mysql_query($sql);

    }

     

     

    if($result){

    echo "<meta http-equiv=\"refresh\" content=\"0;URL=showusers2.php\">";

    }

    }

    mysql_close();

    ?>

    </table>

    </form>

    </td>

    </tr>

    </table>

×
×
  • 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.