简体   繁体   English

如何同时插入和删除行数据

[英]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 在数据库中插入课程_finish已成功完成,但在course_entry表中未删除该行

You delete query must look like: 您删除的查询必须看起来像:

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

Second thing the mysql_* interface is deprecated. 第二件事,不建议使用mysql_ *接口。 You schould use mysqli* or better pdo and prepared statements 您应该使用mysqli *或更好的pdo和准备好的语句

$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 . Als注意$checkbox1的值将为one I don't know the type of course_id but if it's an int it won't succeed either. 我不知道course_id的类型,但是如果它是int类型,它也不会成功。

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 . 这在这里非常重要,因为您需要接受用户输入,这可能由于SQL注入而很危险。

Here is my recommended way to do it (OOP style): 这是我推荐的方法(OOP风格):

<?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. 也许您不喜欢该代码(我也不喜欢,但是因为我不喜欢mysqli haha​​),但实际上建议您尽可能使用准备好的语句。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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