我想同时更新表中的两个字段,但是不起作用
任何人都可以帮助我

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
    $updateSQL = sprintf(
        "UPDATE child,list SET child.list_num = list.list_id  WHERE child.user_id = list.user_id",
        GetSQLValueString($_POST['list_num'], "int"),
        GetSQLValueString($_POST['id'], "int")
    );

    mysql_select_db($database_amar, $amar);
    $Result1 = mysql_query($updateSQL, $amar) or die(mysql_error());

    $updateGoTo = "list_child.php";
    if (isset($_SERVER['QUERY_STRING'])) {
        $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
        $updateGoTo .= $_SERVER['QUERY_STRING'];
    }
    header(sprintf("Location: %s", $updateGoTo));
}

$colname_edit = "-1";
if (isset($_GET['id'])) {
    $colname_edit = $_GET['id'];
}

mysql_select_db($database_amar, $amar);
$query_edit = sprintf(
    "SELECT * FROM child, list WHERE id= %s",
    GetSQLValueString($colname_edit, "int")
);
$edit = mysql_query($query_edit, $amar) or die(mysql_error());
$row_edit = mysql_fetch_assoc($edit);
$totalRows_edit = mysql_num_rows($edit);

最佳答案

我已经尝试过您的查询及其完美的工作方式

"UPDATE child,list SET child.list_num = list.list_id  WHERE child.user_id = list.user_id"


但是我对您的实现方式有疑问,为什么在这里使用sprintf?

$updateSQL = sprintf("UPDATE child,list SET child.list_num =                      list.list_id  WHERE child.user_id = list.user_id ",
                     GetSQLValueString($_POST['list_num'], "int"),
                   GetSQLValueString($_POST['id'], "int"));


它表明您的要求与您的实现有所不同。您在查询中缺少要使用参数的地方

GetSQLValueString($_POST['list_num'], "int") and GetSQLValueString($_POST['id'], "int").


目前,您的查询将不经任何检查就更新表,这不是在遵循sprintf的要求。尝试

$query_edit = sprintf("SELECT * FROM child, list WHERE id= %s",               GetSQLValueString($colname_edit, "int"));

关于php - 同时更新表中的两个字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30963223/

10-16 21:55