如何使用datefield中的不同日期更新数据库

时间:2011-12-03 06:00:13

标签: php mysql web-services

我有3张桌子  1. 事件: event_id,title,location,latitude,longitude,address,description,email
 2. event_time show_id,event_id,start_time,duration,start_date,end_date,date
 3. 类别: cat_id,event_id

我已编写此代码来更新数据, 如果我使用更新查询,则不会在date字段中对其进行编辑,因为日期字段不是由用户输入的,而是在内部计算并填写在字段中。因此,它将相同的日期值带到具有相同event_id的所有字段。 但我希望所有其他字段相同但只有日期字段不同, 所以我试图删除数据库中的整个事件并重新插入新数据。所以我写了这个,

<?php
include_once("webconfig.php");
include_once("webdatabase.php");
$title= isset($_REQUEST['title'])?trim($_REQUEST['title']):"";
$u_id= isset($_REQUEST['user_id'])?trim($_REQUEST['user_id']):"";
$username = isset($_REQUEST['username'])?trim($_REQUEST['username']):"";
$location = isset($_REQUEST['location'])?trim($_REQUEST['location']):"";
$add = isset($_REQUEST['add'])?trim($_REQUEST['add']):"";
$des = isset($_REQUEST['des'])?trim($_REQUEST['des']):"";
$latitude = isset($_REQUEST['latitude'])?trim($_REQUEST['latitude']):"";
$long = isset($_REQUEST['long'])?trim($_REQUEST['long']):"";
$event_id=isset($_REQUEST['event_id'])?trim($_REQUEST['event_id']):"";
$sdate = isset($_REQUEST['sdate'])?trim($_REQUEST['sdate']):"";
$edate=isset($_REQUEST['edate'])?trim($_REQUEST['edate']):"";
$stime = isset($_REQUEST['stime'])?trim($_REQUEST['stime']):"";
$dur = isset($_REQUEST['dur'])?trim($_REQUEST['dur']):"";
//$date=isset($_REQUEST['date'])?trim($_REQUEST['date']):"";

$aryDeparture = explode(":", "$stime"); 
$aryDuration = explode(":", "$dur"); 

$timeDeparture = mktime($aryDeparture[0], $aryDeparture[1]); 

$arrive = date("H:i", strtotime("+" . $aryDuration[0] . " hours +" . $aryDuration[1] . " minutes " , $timeDeparture)); 
$sql ="Delete events,event_time,category from events join event_time join category on(events.event_id=event_time.event_id and event_time.event_id=category.event_id) where event_time. event_id='$event_id'";
mysql_query($sql);

$sql1= "insert into events(event_id,user_id,title,email,location,address,latitude,longitude,description)values('','$u_id','$title','$username','$location','$add','$latitude','$long','$des')";
mysql_query($sql1);
$lastid=mysql_insert_id();
$init_date = strtotime($sdate);
$dst_date = strtotime($edate);
$offset = $dst_date-$init_date;

$dates = floor($offset/60/60/24) + 1;
for ($i = 0; $i< $dates; $i++)
{
$newdate = date("Y-m-d", mktime(12,0,0,date("m", strtotime($sdate)),
(date("d", strtotime($sdate)) + $i), date("Y", strtotime($sdate))));
$sql2="insert into event_time(show_id,event_id,start_date,start_time,end_date,duration,date,end_time) values('','$lastid','$sdate','$stime','$edate','$dur','$newdate','$arrive')";
mysql_query($sql2);
//$sql1="update event_time join events on(events.event_id=event_time.event_id) set events.title='$title',events.location='$location',events.address='$add',events.latitude='$latitude',events.longitude='$long',events.description='$des',event_time.start_time='$stime',event_time.duration='$dur',event_time.start_date='$sdate',event_time.end_date='$edate',event_time.end_time='$arrive',event_time.date='$newdate' where event_time.event_id='$event_id'";
//mysql_query($sql1);
}


//echo $newdate ."<br>";
if(mysql_affected_rows()!=0)
{
$successMsg ="Event Updated Successfully!.";
$xml=
header("Content-type: text/xml");
echo "<root>";
echo "<post>";
echo "<status>True</status>";
echo "<message>$successMsg</message>";
echo "</post>";
echo "</root>";
return $xml;
}
else
{
$xml .= '<root>';
$xml .= '<post>';
$xml .= '<status>false</status>';
$xml .= "<message>event not found</message>";
$xml .= '</post>';
$xml .= '</root>';
echo $xml;
}
?> 

现在我能够插入新数据以及我可以在日期字段中获取不同的数据,但我没有删除旧的事件数据。请任何人帮助我。

1 个答案:

答案 0 :(得分:0)

在删除之前,您没有定义变量$event_id。 您的删除查询很乱,请尝试执行单独的查询并检查错误。 不要忘记在字段周围使用反引号来避免与保留字相关的错误。