我正在尝试允许用户更新他们的个人资料,并使用以下功能来执行此操作:
function update_user($update_data){
global $session_MemberID;
$update = array();
array_walk($update_data, 'array_sanitize');
foreach($update_data as $field=>$data){ //loop through update data in update_info.php
$update[] = '`' . $field . '` = \'' . $data . '\'';
}
//print_r($update);
//die();
mysql_query("UPDATE `oddjob` SET " . implode(', ', $update). " WHERE `MemberID` = $session_MemberID") or die (mysql_error());
}
if (logged_in() ===true) {
$session_MemberID = $_SESSION['MemberID'];//grabbing value from login
$user_data= user_data($session_MemberID,'MemberID','Name','Address','Postcode','DOB','Mobile','CoinsAvailable','Email','Password','RepeatPassword','OddJobName','Description','DaysAvailable','profile');
exit();
}
这是更新页面。(仅限相关代码)
if (isset($_POST['OddJobName']) && isset($_POST['Description']) && isset($_POST['DaysAvailable']) && empty($errors) === true){//if (empty($_POST) === false && empty($errors) === true) {
$daysavailable='';
foreach ($_POST['DaysAvailable'] as $value)
{
$daysavailable .=$value." ";
}
$update_data = array (
'MemberID' => $MemberID,
'OddJobName' => $_POST['OddJobName'],
'Description' => $_POST['Description'],
'DaysAvailable' => $daysavailable,
);
update_user ($update_data);
if(success){
header('Location: member.php?username='.$username);
exit ();
}
} else if (empty($errors) === false){
//otherwise output errors
echo output_errors($errors);
}
?>
当我为用户输入新信息时,我收到以下错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
当我打印$update
时,我得到了:
Array ( [0] => `MemberID` = '30' [1] => `OddJobName` = 'test' [2] => `Description` = 'test' [3] => `DaysAvailable` = 'Friday ' )
这看起来很好,所以我不确定我的查询有什么问题。如果我将更新查询放入phpmyadmin,如:
UPDATE `oddjob` SET `OddJobName`= test,`Description`=test,`DaysAvailable`=Friday, WHERE `MemberID` = 30
我收到错误
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE MemberID = 30' at line 1
(oddjob表中的MemberID是外键。 MemberID是成员表中的主键。)
我对这个SQL的东西没有天赋......正如你所知道的那样,所以请尽可能帮忙。
答案 0 :(得分:1)
首先:
Please, don't use mysql_*
functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDO或MySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial。
关于您的问题,您正在使用global $session_MemberID
,即使您的$update_data
数组中已有该ID。我敢打赌$session_MemberID
是空的,这就是你的第一个查询失败的原因。
尝试在代码中使用此查询:
$sql = "UPDATE `oddjob` SET " . implode(', ', $update). " WHERE `MemberID` = ".intval($update['MemberID']);
关于phpMyAdmin查询,在WHERE
之前出现错误,尾随逗号,请尝试使用此查询:
UPDATE `oddjob` SET `OddJobName`= 'test',`Description`='test',`DaysAvailable`='Friday' WHERE `MemberID` = 30