Cron php更新/从另一个插入一个sql表并从旧表中删除数据

时间:2016-11-05 08:55:27

标签: php mysql cron

嗨恩尼可以帮我解决这个问题吗?我需要在18:00h的每一个dey复制表格中的所有数据" texirequest"到表" dispo"并删除表格上的所有数据" texirequest"在chek table" dispo"如果" dauer_auftrag"值1将所有数据复制到" texirequest"使用Cronjob

<?php
$dauerauftrag_array = array(1);
$con = mysql_connect("localhost","admin","password");
if(!$con) {
    die('could not connect: '.mysql_error());
}
mysql_select_db('admin_transport', $con);

foreach($dauerauftrag_array AS $dauerauftrag) {
    $result = mysql_query("SELECT * FROM dispo WHERE dauer_auftrag = '".$dauerauftrag."'");
    while($row = mysql_fetch_array($result)) {
        mysql_query("INSERT INTO texirequest (id, driver_id, driver_email, driver_name, sender_id, name, typ, sbehalt, Transportar, bfahrer, zinfo, ein_stieg, aus_stieg, dauer_auftrag, status, phoneM, phoneF, termin, termin_austirg, phone, droplocation, location, latitude, longitude, timedate, accept) 
        VALUES ('".$row['id']."', '".$row['driver_id']."', '".$row['driver_email']."', '".$row['driver_name']."', '".$row['sender_id']."', '".$row['name']."', '".$row['typ']."', '".$row['sbehalt']."', '".$row['Transportar']."', '".$row['bfahrer']."', '".$row['zinfo']."', '".$row['ein_stieg']."', '".$row['aus_stieg']."', '".$row['dauer_auftrag']."', '".$row['status']."', '".$row['phoneM']."', '".$row['phoneF']."', '".$row['termin']."', '".$row['termin_austirg']."', '".$row['phone']."', '".$row['droplocation']."', '".$row['location']."', '".$row['latitude']."', '".$row['longitude']."', '".$row['timedate']."', '".$row['accept']."') ON DUPLICATE KEY UPDATE id = '".$row['id']."', '".$row['driver_id']."', '".$row['driver_email']."', '".$row['driver_name']."', '".$row['sender_id']."', '".$row['name']."', '".$row['typ']."', '".$row['sbehalt']."', '".$row['Transportar']."', '".$row['bfahrer']."', '".$row['zinfo']."', '".$row['ein_stieg']."', '".$row['aus_stieg']."', '".$row['dauer_auftrag']."', '".$row['status']."', '".$row['phoneM']."', '".$row['phoneF']."', '".$row['termin']."', '".$row['termin_austirg']."', '".$row['phone']."', '".$row['droplocation']."', '".$row['location']."', '".$row['latitude']."', '".$row['longitude']."', '".$row['timedate']."', '".$row['accept']."'");
    }
}
mysql_close($con);
?>
  

表中没有任何副本&#34; dispo&#34;表格&#34; texirequest&#34;如果dauer_auftrag = 1

我的texirequest表

CREATE TABLE `texirequest` (
  `id` int(11) NOT NULL,
  `driver_id` varchar(100) NOT NULL,
  `driver_email` varchar(100) NOT NULL,
  `driver_name` varchar(100) NOT NULL,
  `sender_id` varchar(100) NOT NULL,
  `name` varchar(100) NOT NULL,
  `typ` varchar(100) NOT NULL,
  `sbehalt` varchar(100) NOT NULL,
  `Transportar` varchar(100) NOT NULL,
  `bfahrer` varchar(100) NOT NULL,
  `zinfo` varchar(100) NOT NULL,
  `ein_stieg` varchar(100) NOT NULL,
  `aus_stieg` varchar(100) NOT NULL,
  `dauer_auftrag` varchar(100) NOT NULL,
  `status` varchar(100) NOT NULL,
  `phoneM` varchar(100) NOT NULL,
  `phoneF` varchar(100) NOT NULL,
  `termin` varchar(100) NOT NULL,
  `termin_austirg` varchar(100) NOT NULL,
  `phone` varchar(100) NOT NULL,
  `droplocation` varchar(100) NOT NULL,
  `location` text NOT NULL,
  `latitude` varchar(100) NOT NULL,
  `longitude` varchar(100) NOT NULL,
  `timedate` text NOT NULL,
  `accept` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

这是我的&#34; dispo&#34;表

CREATE TABLE `dispo` (
  `id` int(11) NOT NULL,
  `driver_id` varchar(100) NOT NULL,
  `driver_email` varchar(100) NOT NULL,
  `driver_name` varchar(100) NOT NULL,
  `sender_id` varchar(100) NOT NULL,
  `name` varchar(100) NOT NULL,
  `typ` varchar(100) NOT NULL,
  `sbehalt` varchar(100) NOT NULL,
  `Transportar` varchar(100) NOT NULL,
  `bfahrer` varchar(100) NOT NULL,
  `zinfo` varchar(100) NOT NULL,
  `ein_stieg` varchar(100) NOT NULL,
  `aus_stieg` varchar(100) NOT NULL,
  `dauer_auftrag` varchar(100) NOT NULL,
  `status` varchar(100) NOT NULL,
  `phoneM` varchar(100) NOT NULL,
  `phoneF` varchar(100) NOT NULL,
  `termin` varchar(100) NOT NULL,
  `termin_austirg` varchar(100) NOT NULL,
  `phone` varchar(100) NOT NULL,
  `droplocation` varchar(100) NOT NULL,
  `location` text NOT NULL,
  `latitude` varchar(100) NOT NULL,
  `longitude` varchar(100) NOT NULL,
  `timedate` text NOT NULL,
  `accept` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

3 个答案:

答案 0 :(得分:2)

admin_transport是变量还是数据库名称。如果它是一个数据库名称,则在名称周围使用引号,如果变量名称之前缺少变量$

答案 1 :(得分:0)

只需编写admin_transport之类的'admin_transport',因为这不是已定义的关键字。

答案 2 :(得分:0)

  

我已完成的脚本所有错误已解决

#!/usr/bin/php
     <?php
    $dauerauftrag_array = array(1); // Select DauerAuftrag Value 1=ON
    $mid_array = array(0, 1, 2, 3);  // Set Curent accept value to delete tables like 1,2,3,4 0 = for Dauerauftrag

    $con = mysql_connect("localhost","username","password");
    if(!$con) {
        die('could not connect: '.mysql_error());
    }
    mysql_select_db('admin_transport', $con);

    foreach($mid_array AS $mid) 
    $d=mysql_query("DELETE FROM texirequest WHERE accept = '".$mid."'");

    if ($d) {print("Update Durchgefürt.");}

    foreach($dauerauftrag_array AS $id) {
        $result = mysql_query("SELECT * FROM dispo WHERE dauer_auftrag = '".$id."'");
        while($row = mysql_fetch_array($result)) {
        mysql_query("INSERT INTO texirequest (id, driver_id, driver_email, driver_name, sender_id, name, typ, sbehalt, Transportar, bfahrer, 
    zinfo, ein_stieg, aus_stieg, dauer_auftrag, status, phoneM, phoneF, termin, termin_austirg, phone, droplocation, location, latitude, longitude, timedate, accept) 
    VALUES (
    '".$row['id']."', '".$row['driver_id']."', '".$row['driver_email']."', '".$row['driver_name']."', '".$row['sender_id']."', '".$row['name']."', '".$row['typ']."', 
    '".$row['sbehalt']."', '".$row['Transportar']."', '".$row['bfahrer']."', '".$row['zinfo']."', '".$row['ein_stieg']."', '".$row['aus_stieg']."', '".$row['dauer_auftrag']."', 
    '".$row['status']."', '".$row['phoneM']."', '".$row['phoneF']."', '".$row['termin']."', '".$row['termin_austirg']."', '".$row['phone']."', '".$row['droplocation']."', 
    '".$row['location']."', '".$row['latitude']."', '".$row['longitude']."', '".$row['timedate']."', '".$row['accept']."') ON DUPLICATE KEY UPDATE id = '".$row['id']."', 
    driver_id = '".$row['driver_id']."', driver_email = '".$row['driver_email']."', driver_name = '".$row['driver_name']."', sender_id = '".$row['sender_id']."', 
    name = '".$row['name']."', typ = '".$row['typ']."', sbehalt = '".$row['sbehalt']."', Transportar = '".$row['Transportar']."', bfahrer = '".$row['bfahrer']."', 
    zinfo = '".$row['zinfo']."', ein_stieg = '".$row['ein_stieg']."', aus_stieg = '".$row['aus_stieg']."', dauer_auftrag = '".$row['dauer_auftrag']."', 
    status = '".$row['status']."', phoneM = '".$row['phoneM']."', phoneF = '".$row['phoneF']."', termin = '".$row['termin']."', termin_austirg = '".$row['termin_austirg']."', 
    phone = '".$row['phone']."', 
    droplocation = '".$row['droplocation']."', location = '".$row['location']."', latitude = '".$row['latitude']."', longitude = '".$row['longitude']."', 
    timedate = '".$row['timedate']."', accept = '".$row['accept']."'");

     }
    }
    mysql_close($con);
    ?>
  

Crontab配置

# Crontab Auto Update 
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
0 18 * * * /usr/bin/php /var/www/vhosts/xyz.com/httpdocs/admin/cron0_allauftrag.php
4 18 * * * /usr/bin/php /var/www/vhosts/xyz.com/httpdocs/admin/cron1_dauerauftrag.php
8 18 * * * /usr/bin/php /var/www/vhosts/xyz.com/httpdocs/admin/cron2_auftrag.php