Jump to content

deleting using javascript

- - - - -

  • Please log in to reply
3 replies to this topic

#1
ravi951

ravi951

    Newbie

  • Members
  • PipPip
  • 18 posts
hi all,
i want javascript function to delete the selected records for the given php program below....
also i am not getting field values in correct order one field is more than its field...
kindly tell me how to do it......

<?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 emp";

$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 align="center" bgcolor="white"><strong>EmpNo</strong></td>

<td align="center" bgcolor="white"><strong>EmpName</strong></td>

<td align="center" bgcolor="white"><strong>Desig</strong></td>

</tr>

<?php

while($rows=mysql_fetch_array($result, MYSQL_ASSOC))

{


?>

<tr>

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

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

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

<td bgcolor="#FFFFFF"><?php 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>

<?php

// Check if delete button active, start this

if($_POST['delete'])

{

//print_r($_POST);

//exit;

for($i=0;$i<count($_POST['checkbox']);$i++)

{

$del_id = $checkbox[$i];

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

$result = mysql_query($sql);

}


// if successful redirect to delete_multiple5.php

if($result){

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

}

}

mysql_close();

?>

</table>

</form>

</td>

</tr>

</table>



#2
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
$del_id = $checkbox[$i];
Should be
$del_id = $_POST['checkbox'][$i];

by the way, this code isn't secure at all...

#3
ravi951

ravi951

    Newbie

  • Members
  • PipPip
  • 18 posts
i have changed like u told but also not deleting....
below is the modified 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 emp";

$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 align="center" bgcolor="white"><strong>EmpNo</strong></td>

<td align="center" bgcolor="white"><strong>EmpName</strong></td>

<td align="center" bgcolor="white"><strong>Desig</strong></td>

</tr>

<?php

while($rows=mysql_fetch_array($result, MYSQL_ASSOC))

{


?>

<tr>

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

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

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

<td bgcolor="#FFFFFF"><?php 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>

<?php

// Check if delete button active, start this

if($_POST['delete'])

{

//print_r($_POST);

//exit;

for($i=0;$i<count($_POST['checkbox']);$i++)

{

$del_id = $_POST['checkbox'][$i];

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

$result = mysql_query($sql);

}


// if successful redirect to delete_multiple5.php

if($result){

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

}

}

mysql_close();

?>

</table>

</form>

</td>

</tr>

</table>



#4
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
This is probably not the main error but you should use
if(isset($_POST['delete']))
Insted of
if($_POST['delete'])

And if possible never use an * in a sql query, it's a lot slower than if you write the name of the colomns.

But I'm having doubt that this is the problem.
Just before the for, can you do a
var_dump($_POST);
And post the result here?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users