好的,所以我花了一年的时间从事网络开发以追求其他职业兴趣,然后这周回来检查一个客户网站,该网站的发布已经暂停了一段时间,我发现自从我的托管公司更新了服务器上的 PHP 版本,包含日期转换功能的脚本会抛出错误,因为 split() 已被弃用(这将教会我花一年的时间!)。
在环顾这里和其他地方之后,我从不同的角度读到我应该使用 preg_split()、explode() 和 strtotime()。理论上听起来很简单,但是当我尝试过它们时,要么日期格式错误(例如 /--15/5/12),要么我收到未定义的偏移警告,或者脚本似乎工作但打印的确认更新的工作显示了可怕的 1-1-70 日期,所以现在不仅我的脚本不起作用,而且我的数据库有一些奇怪的空白条目并且奇怪地添加了总计(至少数据库相对容易找到和修复错误 - 一旦我记得我把服务器密码放在哪里!)。
我知道我可以隐藏 split() 错误,但这不是最佳做法,我想尝试让它按应有的方式工作。有问题的脚本如下。我想我的问题的简短版本是,我需要做什么来重写这个脚本以便它再次工作?
<?php
require_once('/home/thebooks/admins/connect.php');
$id = stripslashes($_POST['id']);
$date = stripslashes($_POST['dateinput']); // get the data from the form
$amountraised = stripslashes($_POST['amountraised']);
$comments = stripslashes($_POST['comments']);
// *** function dateconvert ***
// dateconvert converts data from a variable (posted from a form or just stored)
// into a format mysql will be able to store and converting the
// database date back into the british standard of date month year.
// The script accepts day.month.year or day/month/year or day-month-year.
// @param string $date - Date to be converted
// @param string $func - which function is to be used (1 for input to mysql, 2 for output from mysql)
require_once('/home/thebooks/admins/connect.php');
$id = stripslashes($_POST['id']);
$date = stripslashes($_POST['dateinput']); // get the data from the form
$amountraised = stripslashes($_POST['amountraised']);
$comments = stripslashes($_POST['comments']);
// using type 1
$date = dateconvert($date,1); // Would convert to e.g. 2005-12-19 which is the format stored by mysql
function dateconvert($date,$func) {
if ($func == 1){ //insert conversion
list($day, $month, $year) = split('/-', $date);
$date = "$year-$month-$day";
return $date;
}
if ($func == 2){ //output conversion
list($year, $month, $day) = split('/-', $date);
$date = "$day/$month/$year";
return $date;
}
}
$update = "UPDATE fundraisingtotal SET date = '$date', amountraised = '$amountraised', comments = '$comments' WHERE id='$id' ";
$result = mysql_query($update) or die(mysql_error());
$realdate = dateconvert($date,2); // convert date to British date
if ($result) {
echo "<p class=\"dbpara\">Thank you. Your update to the record was successful.</p>";
echo "<p class=\"dbpara\">The record has been amended to a date of <b>$realdate</b> and amount of <b>$amountraised</b>. Comments: <b>$comments</b></p>";
}
else {
echo "<p>Nothing has been changed.</p>";
}
mysql_close();
?>