SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    inserting and deleting records in php

    hi all,
    i have written a code in php for inserting and deleting multiple records using check boxes."test" is the database name. "emp" is the table name
    which contains the following records in my database.

    empno empname desig
    1111 raju pilot
    1112 ram chef
    1113 ramu doctor
    1114 paul engineer
    1115 ajay player

    now i want to delete one or more records and insert the records in to the above table..
    i have written the below code for deleting but the table values are not displaying and i need to delete also using check box.
    below is delete_multiple2.php
    Code:
    <?php
    $host="localhost"; // Host name
    $username="root"; // Mysql username
    $password=""; // Mysql password
    $db_name="test"; // Database name
    $tbl_name="emp"; // Table name
    // Connect to server and select databse.
    mysql_connect($host, $username, $password)or die("cannot connect");
    mysql_select_db($db_name)or die("cannot select DB");
    // Check if delete button active, start this
    if(isset($_POST['delete'])){
        if(sizeof($_POST['chkbx'])){
            foreach($_POST['chkbx'] AS $val){
                $sql = mysql_query("DELETE FROM $tbl_name WHERE id=".(int)$val)or die(mysql_error());
            }
        }
        // if successful redirect to delete_multiple.php
    
        if($sql){
            header('Location:delete_multiple2.php');//THIS WAS SAMPLE.PHP - YOU MIGHT WANT TO CHANGE IT BACK
        }
    }
     $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);
     ?>
    
    <table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td><form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <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>EmpNo</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>EmpName</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Desig</strong></td>
    </tr>
    <?php
    $x = 0;
    while($rows=mysql_fetch_array($result)){
    $x++;
    ?>
        <tr>
            <td align="center" bgcolor="#FFFFFF"><input name="chkbx[]" type="checkbox" id="checkbox_<? echo $x; // you need a unique id per checkbox ?>" value="<? echo $rows['id']; ?>"></td>
            <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
            <td bgcolor="#FFFFFF"><? echo $rows['empno']; ?></td>
            <td bgcolor="#FFFFFF"><? echo $rows['emoname']; ?></td>
            <td bgcolor="#FFFFFF"><? echo $rows['desig']; ?></td>
        </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
    </tr>
    <?
    mysql_close();
    ?>
    </table>
    </form>
    </td>
    </tr>
    </table>
    also i have written for inserting records into database.
    but it is not inserting.....
    below is insert.php
    Code:
    <?php
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password=""; // Mysql password 
    $db_name="test"; // Database name 
    $tbl_name="emp"; // Table name
    
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);
    
    // Count table rows 
    $count=mysql_num_rows($result);
    
    <?php
    while($rows=mysql_fetch_array($result))
    {
    ?>
    
    <tr>
    <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="y" /></td>
    <td align="center"><input name="empno[]" type="text" id="name" value="<? echo $rows['empno'];?>"></td>
    <td align="center"><input name="empname[]" type="text" id="empname" value="<? echo $rows['empname'];?>"></td>
    <td align="center"><input name="desig[]" type="text" id="desig" value="<? echo $rows['desig'];?>"></td>
    </tr>
    
    <?php
    }
    ?>
    <input type="submit" name="Submit" value="Submit">
    
    <?php
    
    // Get values from form 
    $no=$_POST['empno'];
    $name=$_POST['empname'];
    $desig=$_POST['desig'];
    
    // Check if button name "Submit" is active, do this 
    if($checkbox){
    for($i=0;$i<$count;$i++)
    {
    $sql1="INSERT INTO $tbl_name (empno, empname, desig)VALUES('$no[$i]', '$name[$i]', '$desig[$i]')";
    $result1=mysql_query($sql1);
    }
    }
    i want to integrate these two things so that it will be easy for inserting
    and deleting any no of times..
    kindly tell me how to do it....
    where is my code went wrong.......

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Okay. Some notes that I made reading this.

    #1: Dont use $_POST['delete'] as your check. This is liable to the IE Submit bug. Add a hidden field to the table instead and check against that.
    #2: if(sizeof) is useless if you're just going to wrap it around a foreach - if a foreach is run on an empty array, nothing happens.
    #3: Why are we reloacting people on success? Do you not want to see the list again after deletion?
    #4: For inserting, You're... inserting the data you already have, and will duplicate your data a lot with that code. To insert a flexibly-many records at once, you'll need Javascript; otherwise, you'll have to define how many records the user may enter at one time.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •