Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/forums/php/)
-   -   inserting and deleting using php (http://www.go4expert.com/forums/inserting-deleting-using-php-t26490/)

ravi951 12Aug2011 17:30

inserting and deleting using php
 
hi all,
i have written a script for deleting more than one records using check box.
but it is not working.below is the code ...
Code:

<script>
function fundel(sno)
{
rv=confirm("u want to delete");
if(rv==true)
{
location="delete.php?seno="+sno;
}
}
</script>
<form method="post" action="delrec.php">
<table border='1'>
<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
$data=mysql_query("select * from emp");
while($rec=mysql_fetch_row($data))
{
echo"<tr><td><input type='checkbox' name=$rec[2]'
value='$rec[2]'><td>$rec[0]<td>$rec[1]<td>$rec[2]<td><input type='button' value='delete'
onclick='fundel($rec[2])'>";
}
?>
</table>
<input type='submit' value='delrec'>
</form>

below ids the delrec.php.
Code:

/*<?php
$qs=$_REQUEST['seno'];
mysql_connect("localhost","root","");
mysql_select_db("test");
mysql_query("delete from emp where sno=$qs");
header('location:getrec.php");
?>*/
<script>
function delrec(sno)
{
rv=confirm("u want to delete");
if(rv==true)
{
location="delete.php?seno="+sno;
}
}
</script>

also is the delete.php..
Code:

<?php
$qs=$_REQUEST['seno'];
mysql_connect("localhost","root","");
mysql_select_db("test");
mysql_query("delete from emp where sno=$qs");
header("location:getrec.php");
?>
/*<script>
function delrec(sno)
{
rv=confirm("u want to delete");
if(rv==true)
{
location="delete.php?seno="+sno;
}
}
</script>
*/

also i want to insert values of three fields using php code.
i have written the code.when i click the insert button it should get inserted
into database....
below is the code for insert function..
Code:

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="ins.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><strong>Insert Data Into mySQL Database </strong></td>
</tr>
<tr>
<td width="71">Name</td>
<td width="6">:</td>
<td width="301"><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td>Lastname</td>
<td>:</td>
<td><input name="lastname" type="text" id="lastname"></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>

also the ins.php..
Code:

<?php

/*$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name*/

// Connect to server and select database.
mysql_connect("localhost", "root", "")or die("cannot connect");
mysql_select_db("test")or die("cannot select DB");

// Get values from form
$empno=$_POST['empno'];
$empname=$_POST['empname'];
$desig=$_POST['desig'];

// Insert data into mysql
$sql="INSERT INTO emp(empno, empname, desig)VALUES('$empno', '$empname', '$desig')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}

else
{
echo "ERROR";
}
// close connection
mysql_close();
?>

i want all these to be integrated into one form so that i can easily insert,delete using php
kindly tell me how to do it....

pein87 14Aug2011 13:43

Re: inserting and deleting using php
 
You need to use a query to get the id of each item and use that as the value attribute of the input element.

PHP Code:

$q mysql_query("SELECT * FROM tB");
while(
$d mysql_fetch_assoc($q))
{
echo 
"<input type=\"checkbox\" name=\"id[]\" value=\"$d['id']\" />" $d['name'];


then use my function to delete them

PHP Code:

dbMultiDelete("id","tB","id"); 

PHP Code:

function dbMultiDelete($pV,$tN,$cN)
{
    
$postVar $_POST[$pV]; //get value of id which should be an array


if($postVar)//check if the value has a value
{
    if(
is_array($postVar))// postvar has a value, lets see if its an array and if it is loop through the 

values else perform single delete action
    
{
        
//is array, loop through and perform delete
        
$s count($postVar);
        
$e 0;
        foreach(
$postVar as $v)
        {
            
$Q mysql_query("DELETE FROM $tN WHERE $cN = '".$v."'");//this code will be executed 

until no more indexes exist in the array.
            
$e += mysql_affected_rows($Q); // count rows affected and use to check if all rows were 

deleted
        
}
        
        
$r = array('itemsToDelete' => $s,'itemsDeleted' => $e);
        return 
$r;    
    }
    else
    {
        
$Q mysql_query("DELETE FROM $tN WHERE $cN = '".$postVar."'");
        
$e mysql_affected_rows($Q);
        
$r = array('itemsToDelete' => 1,'itemsDeleted' => $e);
        return 
$e;
    }
}



doing it this way will ensure everything that is selected will be deleted and if the item is already gone the return array values can be subtracted to see how many were selected vs how many where deleted. Make sure your name attribute is followed by [] i.e

HTML Code:

<input type="checkbox" name="yourNameHere[]" />
This way the values will be grouped and sent as an array with the value attribute being the value of each index of the array.


All times are GMT +5.5. The time now is 15:40.