这是我的代码,用于将数据插入数据库表,我想从一个表中删除数据并在另一个表中插入数据
<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");
}
?>
在数据库中插入到课程_finish是成功完整但在course_entry表中该行不能删除
答案 0 :(得分:0)
删除查询必须如下所示:
delete from course_entry where course_id = ".$checkbox1." and username ='".$user."'
第二件事是不推荐使用mysql_ *接口。你应该使用mysqli *或更好的pdo和预备语句
答案 1 :(得分:0)
$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."'");
答案 2 :(得分:0)
从
更改您的删除声明delete from course_entry where (course_id,username) values ('".$checkbox1."','".$user."'"
到
delete from course_entry where course_id='".$checkbox1."' and user_name='".$user."'
答案 3 :(得分:0)
试试这个:
$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注意到$checkbox1
的值为one
。我不知道course_id
的类型,但如果它是int
,它也不会成功。
答案 4 :(得分:0)
完整的解决方案
<?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");
}
?>
答案 5 :(得分:0)
我很惊讶其他答案都没有涵盖 prepared statements !这一点非常重要,因为您因<{3}}而接收 可能导致危险的用户输入。
这是我推荐的方法(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();
也许你不喜欢这些代码(我也不喜欢,但因为我不喜欢mysqli haha),但你最好尽可能使用准备好的语句。< / p>
希望这有帮助。