我有一个管理页面,可以将所有信息加载到其输入字段中。然后,用户可以编辑该信息并将其提交回数据库。
我是否可以对INSERT INTO
使用类似的SQL查询?
像("UPDATE tableName WHERE username = '$username_entry")
并且所有字段都刷新该行中的所有字段。
我的代码来自INSERT
:
$insertSQL = sprintf("INSERT INTO business_info (timestamp,
username,
password,
business_name,
address1,
address2,
address3,
address4,
postcode,
tel,
email1,
email2,
website,
Facebook,
twitter,
opening_monday_from,
opening_monday_to,
opening_tuesday_from,
opening_tuesday_to,
opening_wednesday_from,
opening_wednesday_to,
opening_thursday_from,
opening_thursday_to,
opening_friday_from,
opening_friday_to,
opening_saturday_from,
opening_saturday_to,
opening_sunday_from,
opening_sunday_to,
business_type,
business_description,
managers_message,
image1,
image2,
image3,
image4,
image5)
VALUES (NOW(), %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['username_entry'], "text"),
GetSQLValueString($hashPass, "text"),
GetSQLValueString($_POST['business_name'], "text"),
GetSQLValueString($_POST['address1'], "text"),
GetSQLValueString($_POST['address2'], "text"),
GetSQLValueString($_POST['address3'], "text"),
GetSQLValueString($_POST['address4'], "text"),
GetSQLValueString($_POST['postcode'], "text"),
GetSQLValueString($_POST['tel'], "text"),
GetSQLValueString($_POST['email1'], "text"),
GetSQLValueString($_POST['email2'], "text"),
GetSQLValueString($_POST['website'], "text"),
GetSQLValueString($_POST['facebook'], "text"),
GetSQLValueString($_POST['twitter'], "text"),
GetSQLValueString($_POST['opening_monday_from'], "text"),
GetSQLValueString($_POST['opening_monday_to'], "text"),
GetSQLValueString($_POST['opening_tuesday_from'], "text"),
GetSQLValueString($_POST['opening_tuesday_to'], "text"),
GetSQLValueString($_POST['opening_wednesday_from'], "text"),
GetSQLValueString($_POST['opening_wednesday_to'], "text"),
GetSQLValueString($_POST['opening_thursday_from'], "text"),
GetSQLValueString($_POST['opening_thursday_to'], "text"),
GetSQLValueString($_POST['opening_friday_from'], "text"),
GetSQLValueString($_POST['opening_friday_to'], "text"),
GetSQLValueString($_POST['opening_saturday_from'], "text"),
GetSQLValueString($_POST['opening_saturday_to'], "text"),
GetSQLValueString($_POST['opening_sunday_from'], "text"),
GetSQLValueString($_POST['opening_sunday_to'], "text"),
GetSQLValueString($_POST['business_type'], "text"),
GetSQLValueString($_POST['business_description'], "text"),
GetSQLValueString($_POST['managers_message'], "text"),
GetSQLValueString($insert_upload1, "text"),
GetSQLValueString($insert_upload2, "text"),
GetSQLValueString($insert_upload3, "text"),
GetSQLValueString($insert_upload4, "text"),
GetSQLValueString($insert_upload5, "text"));
if (mysqli_query($link, $insertSQL)) {
echo "New record created successfully $username_entry";
} else {
echo "Error: " . $insertSQL . "<br>" . mysqli_error($link);
}
mysqli_close($link);
有什么方法可以修改这个以适应我的情况吗?
答案 0 :(得分:0)
假设您有一个匹配的主键,您可以使用REPLACE INTO查询。 (docs)
这将在插入之前删除旧行。
然而,这不是一种有效的方法。你不想使用UPDATE .. SET查询的原因是什么?表格中的列数?
编辑(解决评论): 假设您的管理页面具有的表单将所有输入名称设置为mysql中的确切列名称。你可以这么做:
$query = "UPDATE business_info SET";
foreach($_POST as $key => $value) {
$query .= " " . $key . " = '" . $value . "'";
}
$query .= " WHERE some_condition='value'";
或者如果您使用的是PDO:
$query = "UPDATE business_info SET";
$values = []
foreach($_POST as $key => $value) {
$query .= " " . $key . " = ?";
$values[] = $value;
}
$query .= " WHERE some_condition='value'";
这将根据您的帖子值构建更新查询。
注意:没有输入任何输入,请确保将其添加回来。