0

我有两个表:table_a(名称,ID)和 table_b(任务)。

我可以选择将 table_a.name 中的值插入 table_b.Task。

但是,当我想从 table_a.name 中删除一个值并且在 table_b.Task 中找到该值时,必须使用 table_a.name 中删除的值的先前值更新 table_b.Task 值。

这是代码:

  $delete=$_POST['deletevalue'];
  if(isset($_POST['_submit'])){

$id=mysql_query("SELECT ID FROM table_a WHERE name='$delete'");


$x=mysql_query("SELECT MAX(ID) FROM table_a WHERE ID<$id");

$task=mysql_query("SELECT name FROM table_a WHERE ID=$x");

$query1=mysql_query("delete from table_a where name='$delete'");

$query2=mysql_query("UPDATE table_b SET Stare='$task' WHERE Task='$delete'");

PS ID 是自动递增的

当我使用此代码并显示 table_b 时,应该更新的值是空白的。请帮我让它工作

4

1 回答 1

0

In php, the function mysql_query returns a resultset object.

To fetch the actual value, you need to fetch the value from the resultset. This should help:

$objId=mysql_query("SELECT ID FROM table_a WHERE name='$delete'");
$rowId=mysql_fetch_row($objId);
$id=$rowId[0];

Similarly, you can get the values for the other instances.

As an aside, you can combine these queries:

$id=mysql_query("SELECT ID FROM table_a WHERE name='$delete'");
$x=mysql_query("SELECT MAX(ID) FROM table_a WHERE ID<$id");
$task=mysql_query("SELECT name FROM table_a WHERE ID=$x");

as

$objTask=mysql_query("SELECT a.name FROM table_a a WHERE a.ID=(SELECT MAX(b.ID) from table_a b WHERE b.ID<(SELECT c.ID FROM table_a c WHERE c.name='$delete'))");
$rowTask=mysql_fetch_row($objTask);
$task=$rowTask[0];

This will fetch the value you require.

EDIT: Update and delete statements don't generate any resultsets as output.

于 2012-09-05T09:55:59.617 回答