0

Using the codes below i can select multiple items to delete them. i am using form to redirect to deleteselected.php. But I now want to move multiple items to another database by selecting them as i did it for deleting. and the codes for moving items to another database is in file moveselected.php. but how can i do this?I cannot redirect to moveselected.php because using multiple forms is not allowed.

echo "<TR><TD>Select</td><TD>S.N.</td><td><B>Full Name</B></td><td><B>Options</B></td></TR>";
while ($myrow = $result->fetch_assoc())
{
    echo "<form method='POST' action='deleteselected.php'>";
    echo "<tr><td><input type='checkbox' name='mycheck[]' value=".$myrow['id']."></td>";
    echo "<TD>".$myrow['id']."</TD>";
    echo "<TD>".$myrow['name']." </TD>";
    echo "<TD><a href=\"view1.php?id=".$myrow['id']."\"><input type='button' value='View'></a> ";
    echo "<a href=\"delete1.php?id=".$myrow['id']."\"><input type='button' value='Delete'></a> ";
    echo "<a href=\"edit1.php?id=".$myrow['id']."\"><input type='button' value='Edit'disabled></a>";

;
}

echo "</TABLE>";

}
?>
<input type='submit' name= 'ptext' value='Delete selected'>
</form>

this is my deleteselected.php if required.

<?php
extract($_POST);
$db = new mysqli("localhost", "root", "","learndb");
$totalCheckboxChecked = sizeof($_POST['mycheck']);

for($i=0;$i<$totalCheckboxChecked;$i++)
{
    $idToDelete = $mycheck[$i];

    $Query = "DELETE FROM studentrecords WHERE id = '$idToDelete'";

    $result=$db->query($Query);
    include_once"viewstudentrecords1.php";
    echo "<center><h3 style=\"color:red\">Information Deleted</h3></center>";
    echo "";
}

And here is my moveselected.php

<?php
extract($_POST);
$db = new mysqli("localhost", "root", "","learndb");
$totalCheckboxChecked = sizeof($_POST['mycheck']);

for($i=0;$i<$totalCheckboxChecked;$i++)
{
    $idToMove = $mycheck[$i];


$sql1="INSERT INTO passivestudents (select* FROM studentrecords WHERE id=$idToMove)";
$sql2="DELETE FROM studentrecords WHERE id=$idToMove";

$result=$db->query($sql1);
$db->query($sql2);



    echo "<center><h3 style=\"color:red\">Information Moved!</h3></center>";
    echo "";
}
?>
micky
  • 277
  • 1
  • 13
  • 39
  • `I cannot redirect to moveselected.php because using multiple forms is not allowed.` Not allowed by who?? That is a very very strange constraint – Steve Feb 09 '16 at 12:45
  • @Steve i mean to say form inside form is not valid, i think. – micky Feb 09 '16 at 12:49
  • 2
    Use one form with two buttons. Put both functions (delete and move) in the same file. At the start of the file check the value of the button (move or delete) then perform the related function. – RST Feb 09 '16 at 12:52
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). – Jay Blanchard Feb 09 '16 at 12:53
  • 1
    @RST please explain with some codes in answer. your comment looks good. – Sanzeeb Aryal Feb 09 '16 at 12:58
  • thanks all. solved now. – micky Feb 09 '16 at 13:38

1 Answers1

0

At the end of your form you can add the move-button

?>
<input type='submit' name= 'ptext' value='Delete selected'>
<input type='submit' name= 'ptext' value='Move selected'>
</form>

The $_POST['ptext'] variable will have the either the value Delete selected or Move selected

In your php file you can do something like

<?php
  if ( 'Delete selected' == $_POST['ptext'] ) {
    delete_selected();
  } else {
    move_selected();
  }
?>

this assumes you have a function delete_selected() and move_selected() in the same file. These functions will hold the actions to take to delete or move the selected entries.

Another way to make these functions available is by including the files that hold the functions.

RST
  • 3,899
  • 2
  • 20
  • 33