简体   繁体   中英

how to insert and delete row data same time

hi this is my code for insert data into a database table and i want to delete data from one table and insert data in another table

<form action="page1.php" method="POST">
<input type="checkbox" name="chk1" value="one" /> <br/>

<input type="submit" name="submit" value="submit"/>
</form>



</body>
</html>
<?php
include 'config.php';
session_start();
$user=$_SESSION['sess_user'];
$checkbox1=$_POST['chk1'];
if ($_POST["submit"]=="submit")
{

$result = mysql_query("SELECT std_id,course_name,stdname FROM course_entry WHERE course_id = '".$checkbox1."' and username= '".$user."'");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row = mysql_fetch_row($result);

echo $row[0]; echo $row[1]; 
echo $row[2];

$query= "INSERT INTO course_finish (course_id,course_name,username,finishdate) values ('".$row[0]."','".$row[1]."','".$row[3]."',now())"; 
 "delete from course_entry where (course_id,username) values ('".$checkbox1."','".$user."'";
mysql_query($query) or die (mysql_error());


echo "Record is inserted";
header("Location: profile.php");    

}
?>

in database insert into course _finish is success full but in course_entry table the row isn't delete

You delete query must look like:

delete from course_entry where course_id = ".$checkbox1." and username ='".$user."'

Second thing the mysql_* interface is deprecated. You schould use mysqli* or better pdo and prepared statements

$query= "INSERT INTO course_finish (course_id,course_name,username,finishdate) values ('".$row[0]."','".$row[1]."','".$row[3]."',now())"; 
mysql_query("delete from course_entry where course_id='".$checkbox1."'");

Change your delete statement from

delete from course_entry where (course_id,username) values ('".$checkbox1."','".$user."'"

to

delete from course_entry where course_id='".$checkbox1."' and user_name='".$user."'

Try this:

$insertQuery = "INSERT INTO course_finish (course_id,course_name,username,finishdate) values ('".$row[0]."','".$row[1]."','".$row[3]."',now())"; 
mysql_query($insertQuery) or die (mysql_error());

$deleteQuery = "DELETE from course_entry where course_id = '".$checkbox1."' AND username = '".$user."'";
mysql_query($deleteQuery ) or die (mysql_error());

Als note that the value of $checkbox1 will be one . I don't know the type of course_id but if it's an int it won't succeed either.

Complete solution

    <?php 

<form action="page1.php" method="POST">
<input type="checkbox" name="chk1" value="one" /> <br/>

<input type="submit" name="submit" value="submit"/>
</form>



</body>
</html>
<?php
include 'config.php';
session_start();
$user=$_SESSION['sess_user'];
$checkbox1=$_POST['chk1'];
if ($_POST["submit"]=="submit")
{

$result = mysql_query("SELECT std_id,course_name,stdname FROM course_entry WHERE course_id = '".$checkbox1."' and username= '".$user."'");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row = mysql_fetch_row($result);

echo $row[0]; echo $row[1]; 
echo $row[2];

$query= "INSERT INTO course_finish (course_id,course_name,username,finishdate) values ('".$row[0]."','".$row[1]."','".$row[3]."',now())"; 
$deletequry=mysql_query("delete from course_entry where course_id='".$checkbox1."'");
mysql_query($query) or die (mysql_error());

if($deletequry){
    echo "deleted";
}

echo "Record is inserted";
header("Location: profile.php");    

}
?>

I am surprised that none of the other answers covers prepared statements ! This is pretty important here since you take user input which can be dangerous because of SQL injection .

Here is my recommended way to do it (OOP style):

<?php
$db = new mysqli(HOST, USERNAME, PASSWORD, DBNAME);
$db->set_charset('utf8');

$stmt1 = $db->prepare("SELECT std_id,course_name,stdname FROM course_entry WHERE course_id = ? and username = ? LIMIT 1");
$stmt1->bind_param('is', $checkbox1, $user);
$stmt1->execute();
$result1 = $stmt1->get_result();
$stmt1->close();
$row = $result1->fetch_row();
$stmt2 = $db->prepare("INSERT INTO course_finish (course_id,course_name,username,finishdate) values (?, ?, ?, NOW())");
$stmt2->bind_param('iss', $row[0], $row[1], $row[2]); //i think it's $row[2], not $row[3]
$stmt2->execute();
$stmt2->close();
$stmt3 = $db->prepare("delete from course_entry where (course_id, username) values (?, ?)");
$stmt3->bind_param('is', $checkbox1, $user);
$stmt3->execute();
$stmt3->close();
$db->close();

Maybe you don't like the code (I don't, too, but because I don't like mysqli haha), but it is really advisable to use prepared statements where you can.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM