为什么我的PHP MySQLi Query不会插入数据库?

时间:2013-05-31 13:38:46

标签: php database mysqli

PHP版本:5.4.3 MySQLi版本:5.5.24

我已经盯着这段代码了几天,解决了很多小错误,仍然无法弄清楚为什么它不能成功添加到我的数据库。

破碎的代码:

// All of my posts have been excluded
// Post Example:
// $server = $_POST['server'];

mysqli_query($link, sprintf(
    'INSERT INTO `tribe-server-inv`.`serverdb` (' .

    //Server Name
    '`Record`, `Server`, `Active Server`, `Server Location`, `Deactivated Server`, ' .

    //System Overview
    '`Operating System`, `Serial Number`, `Model`, `CPU`, `Power Requirements`, `Location`, `Memory`, ' .
    '`Total Physical Disk`, ' .

    //System Information
    '`Antivirus Installed`, `Warrenty Start Date`, `Physical Server`, `Install Date`, `Antivirus Version`, ' . 
    '`Warrenty End Date`, `Virtual Server`, `Deactivated Date`, ' .

    //Availability
    '`Operation Availability Days`, `Availability Downtime Days`, `Operation Availability Hours`, ' .
    '`Availability Downtime Hours`, ' .

    //Support Information
    '`Hardware Vender`, `Associated Servers 1`, `System Owner`, `Associated Servers 2`, ' .
    '`Database Owner`, `Associated Servers 3`, `Application Owner`, `Associated Servers 4`, ' .
    '`Software Owner`, `Associated Servers 5`, ' .

    //Network Info
    '`IP Address 1`, `MAC Address 1`, `Trunked IP Address 1`, `IP Address 2`, `MAC Address 2`, ' .
    '`Trunked IP Address 2`, `IP Address 3`, `MAC Address 3`, `Management Port IP Address`, ' .
    '`IP Address 4`, `MAC Address 4`, ' .

    //Storage Info
    '`Sans Storage 1`, `Sans Storage 1 Drive Space 1`, `Sans 1 Initiator Address`, `Sans Storage 2`, ' .
    '`Sans Storage 2 Drive Space 2`, `Sans 2 Initiator Address`, ' .
    '`Promise Storage 1`, `Promise Storage 1 Drive Space 1`, `Promise 1 Initiator Address`, ' .
    '`Promise Storage 2`, `Promise Storage 2 Drive Space 2`, `Promise 2 Initiator Address`, ' .

    //System Backup
    '`Appassure`, `Backupexec`, `Other Backup`, `Backup Server`, `Replication Backup Server`, ' .
    '`Backup Server Location`, `Replication Backup Location`, `Backup Server Size`, ' .
    '`Replication Backup Size`, `Offsite / Archive Backups`, `Backup Image`, `Archive Offsite Location`, ' .
    '`Backup Image Location`, `Archive Size`, `Backup Misc Info`, `Backup Daily`, ' .
    '`Daily Backup Space Used`, `Backup Weekly`, `Weekly Backup Space Used`, `Backup Quarterly`, ' .
    '`Quarterly Backup Space Used`, `Backup Yearly`, `Yearly Backup Space Used`, ' .

    //Disaster Recovery
    '`Number of Users`, `Daily Bandwidth Consumption`, `Equipment Replacement Time`, ' .
    '`Total Local Data Storage`, `Vender Disaster Turn Around`, `Vender Disaster Cost`, ' .
    '`Total Network Storage`, `Approximate Tech Hours`, `Est Replacement Cost`, ' .
    '`Approximate Total Replacement Time`, `Disaster Recovery Scenario 1 Loss of Software`, ' .
    '`Disaster Recovery Scenario 2 Loss of Hardware`, `Application`)' .

    //Values
    'VALUES ' .

    //Server Name
    '(NULL, \'%s\', \'%s\', \'%s\', \'%s\', ' .

    //System Overview
    '\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', ' .

    //System Information
    '\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', ' .
    '\'%s\', \'%s\', ' .

    //Availability
    '\'%s\', \'%s\', \'%s\', \'%s\', ' .

    //Support Information
    '\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', ' .
    '\'%s\', \'%s\', \'%s\', ' .

    //Network Info
    '\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', ' .

    //Storage Info
    '\'%s\', \'%s\', \'%s\', \'%s\', ' .
    '\'%s\', \'%s\', ' .
    '\'%s\', \'%s\', \'%s\', ' .
    '\'%s\', \'%s\', ' .

    //System Backup
    '\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', ' .
    '\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', ' .
    '\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', ' .
    '\'%s\', \'%s\', ' .

    //Disaster Recovery
    '\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', ' .
    '\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\')',

    //Names
    mysqli_real_escape_string($link, $server),
    mysqli_real_escape_string($link, $activeServer),
    mysqli_real_escape_string($link, $serverLocation),
    mysqli_real_escape_string($link, $deactiveServer),

    //Overview
    mysqli_real_escape_string($link, $operating),
    mysqli_real_escape_string($link, $serial),
    mysqli_real_escape_string($link, $model),
    mysqli_real_escape_string($link, $cpu),
    mysqli_real_escape_string($link, $powerReq),
    mysqli_real_escape_string($link, $location),
    mysqli_real_escape_string($link, $memory),
    mysqli_real_escape_string($link, $totPhyDisk),

    //System Info
    mysqli_real_escape_string($link, $antiVirusInstall),
    mysqli_real_escape_string($link, $warrentyStart),
    mysqli_real_escape_string($link, $phyServer),
    mysqli_real_escape_string($link, $installDate),
    mysqli_real_escape_string($link, $antiVirusVersion),
    mysqli_real_escape_string($link, $warrentyExpire),
    mysqli_real_escape_string($link, $virtualServer),
    mysqli_real_escape_string($link, $deactivatedDate),

    //Availability
    mysqli_real_escape_string($link, $availableDays),
    mysqli_real_escape_string($link, $downtimeDays),
    mysqli_real_escape_string($link, $availableHours),
    mysqli_real_escape_string($link, $downtimeHours),

    //Support Info
    mysqli_real_escape_string($link, $hardwareVendor),
    mysqli_real_escape_string($link, $assoServ1),
    mysqli_real_escape_string($link, $systemOwner),
    mysqli_real_escape_string($link, $assoServ2),
    mysqli_real_escape_string($link, $databaseOwn),
    mysqli_real_escape_string($link, $assoServ3),
    mysqli_real_escape_string($link, $appOwner),
    mysqli_real_escape_string($link, $assoServ4),
    mysqli_real_escape_string($link, $softwareOwn),
    mysqli_real_escape_string($link, $assoServ5),

    //Network Info
    mysqli_real_escape_string($link, $IP1),
    mysqli_real_escape_string($link, $MAC1),
    mysqli_real_escape_string($link, $trunk1),
    mysqli_real_escape_string($link, $IP2),
    mysqli_real_escape_string($link, $MAC2),
    mysqli_real_escape_string($link, $trunk2),
    mysqli_real_escape_string($link, $IP3),
    mysqli_real_escape_string($link, $MAC3),
    mysqli_real_escape_string($link, $manPortIP),
    mysqli_real_escape_string($link, $IP4),
    mysqli_real_escape_string($link, $MAC4),

    //Storage
    mysqli_real_escape_string($link, $sanStor1),
    mysqli_real_escape_string($link, $driveSpace1),
    mysqli_real_escape_string($link, $initiatorAddress1),
    mysqli_real_escape_string($link, $sanStor2),
    mysqli_real_escape_string($link, $driveSpace2),
    mysqli_real_escape_string($link, $initiatorAddress2),
    mysqli_real_escape_string($link, $promStor1),
    mysqli_real_escape_string($link, $promStorSpace1),
    mysqli_real_escape_string($link, $prom1InitAddress),
    mysqli_real_escape_string($link, $promStor2),
    mysqli_real_escape_string($link, $promStorSpace2),
    mysqli_real_escape_string($link, $prom2InitAddress),

    //System Backup
    mysqli_real_escape_string($link, $appAssure),
    mysqli_real_escape_string($link, $backupExec),
    mysqli_real_escape_string($link, $otherBackup),
    mysqli_real_escape_string($link, $backupServ),
    mysqli_real_escape_string($link, $repBackupServ),
    mysqli_real_escape_string($link, $backupServLoc),
    mysqli_real_escape_string($link, $repBackupLoc),
    mysqli_real_escape_string($link, $backupSize),
    mysqli_real_escape_string($link, $repBackupSize),
    mysqli_real_escape_string($link, $archiveBackup),
    mysqli_real_escape_string($link, $backupImage),
    mysqli_real_escape_string($link, $arcOffsiteLoc),
    mysqli_real_escape_string($link, $backupImageLoc),
    mysqli_real_escape_string($link, $arcBackSize),
    mysqli_real_escape_string($link, $backupMiscInfo),
    mysqli_real_escape_string($link, $backupDaily),
    mysqli_real_escape_string($link, $dailySpaceUsed),
    mysqli_real_escape_string($link, $backupWeekly),
    mysqli_real_escape_string($link, $weeklySpaceUsed),
    mysqli_real_escape_string($link, $backupQuarter),
    mysqli_real_escape_string($link, $quarterlySpaceUsed),
    mysqli_real_escape_string($link, $backupYear),
    mysqli_real_escape_string($link, $yearlySpaceUsed),

    //Disaster Recovery
    mysqli_real_escape_string($link, $numUsers),
    mysqli_real_escape_string($link, $bandwidth),
    mysqli_real_escape_string($link, $equipReplaceTime),
    mysqli_real_escape_string($link, $totalLocStor),
    mysqli_real_escape_string($link, $vendorTurn),
    mysqli_real_escape_string($link, $venDisCost),
    mysqli_real_escape_string($link, $totNetStor),
    mysqli_real_escape_string($link, $approxTechTime),
    mysqli_real_escape_string($link, $estRepCost),
    mysqli_real_escape_string($link, $totReplaceTime),
    mysqli_real_escape_string($link, $disasterRecover1),
    mysqli_real_escape_string($link, $disasterRecover2),
    mysqli_real_escape_string($link, $Applications)));

工作代码:

      mysqli_query($link, sprintf(
    'INSERT INTO serverdb (' .

    //Server Name
    'Server,' .
    '`Server Location`)' .

    //Values
    'VALUES ' .

    //Server Name
    '(\'%s\', \'%s\')',

    //Names
    mysqli_real_escape_string($link, $server),
    mysqli_real_escape_string($link, $serverLocation)));

两组代码的结构完全相同,我无法弄清楚它有什么问题。

2 个答案:

答案 0 :(得分:1)

  1. 确保您可以看到PHP错误 创建有意的语法错误,看看是否可以在屏幕上的日志中找到它。如果不能 - 将PHP配置为记录错误或在屏幕上显示它们:

    error_reporting = E_ALL
    display_errors = On
    log_errors = On
    

    在php.ini或other places

  2. 然后确保再次看到PHP错误。
  3. 确保您可以看到mysqli错误。以这种方式运行所有查询:

    mysqli_query($link, $query) or trigger_error(mysqli_error($link));
    
  4. 观察错误并采取适当的措施。

答案 1 :(得分:0)

由于时间限制,表格重建暂时不在选项范围内。解决这个问题的方法不是修复上面的代码,而是在没有mysqli_real_escape_strings的情况下从头开始重写整个查询。

我工作的查询发布在下面。为了您的一些解脱,我从头开始构建另一个表来跟踪客户端计算机,该代码将在下面发布。除非我在此之前找到/做出改进,否则该代码将是我重做服务器的方式。

$q  = 'INSERT INTO `tribe-server-inv`.`serverdb` (`Server`,'.
      '`Server Location`, `Operating System`, `CPU`, `Memory`, `Total Physical Disk`, 

`Serial Number`, `Model`, `Location`, `IP Address 1`, `IP Address 2`,'.
  '`IP Address 3`, `IP Address 4`, `Trunked IP Address 1`, `Trunked IP Address 2`, `Management Port IP Address`, `Physical Server`, `Virtual Server`, `Warrenty Start Date`, `Warrenty End Date`, `Active Server`, `Install Date`, `Deactivated Server`, `Deactivated Date`,'.
          '`Power Requirements`, `Sans Storage 1`, `Sans Storage 2`, `Sans Storage 1 Drive Space 1`, `Sans Storage 2 Drive Space 2`, `Sans 1 Initiator Address`, `Sans 2 Initiator Address`, `Promise Storage 1`,'.
          '`Promise Storage 2`, `Promise Storage 1 Drive Space 1`, `Promise Storage 2 Drive Space 2`, `Promise 1 Initiator Address`, `Promise 2 Initiator Address`, `Operation Availability Hours`, `Operation Availability Days`,'.
        '`Availability Downtime Days`, `Availability Downtime Hours`, `Application`, `Associated Servers 1`, `Associated Servers 2`, `Associated Servers 3`, `Associated Servers 4`, `Associated Servers 5`, `Hardware Vender`, `System Owner`, `Database Owner`, `Application Owner`, `Software Owner`, `MAC Address 1`, `MAC Address 2`, `MAC Address 3`,'.
        '`MAC Address 4`, `Backup Server`, `Backup Server Location`, `Backup Server Size`, `Replication Backup Server`, `Replication Backup Location`, `Replication Backup Size`, `Offsite / Archive Backups`, `Archive Offsite Location`, `Archive Size`, `Backup Daily`, `Backup Weekly`, `Backup Quarterly`, `Backup Yearly`, `Backup Misc Info`, `Backup Image`, `Backup Image Location`, `Disaster Recovery Scenario 1 Loss of Software`, `Disaster Recovery Scenario 2 Loss of Hardware`, `Daily Backup Space Used`, `Weekly Backup Space Used`, `Quarterly Backup Space Used`, `Yearly Backup Space Used`, `Antivirus Installed`, `Antivirus Version`, `Est Replacement Cost`, `Number of Users`,'.
        '`Daily Bandwidth Consumption`, `Total Local Data Storage`, `Total Network Storage`, `Equipment Replacement Time`, `Vender Disaster Turn Around`, `Vender Disaster Cost`, `Approximate Tech Hours`, `Approximate Total Replacement Time`, `Appassure`, `Backupexec`, `Other Backup`)'.
        'VALUES (\''.$server.'\', \''.$serverLocation.'\', \''.$operating.'\', \''.$cpu.'\', \''.$memory.'\', \''.$totPhyDisk.'\', \''.$serial.'\', \''.$model.'\', \''.$location.'\', \''.$IP1.'\', \''.$IP2.'\', \''.$IP3.'\', \''.$IP4.'\', \''.$trunk1.'\', \''.$trunk2.'\', \''.$manPortIP.'\', \''.$phyServer.'\', \''.$virtualServer.'\', \''.$warrentyStart.'\', \''.$warrentyExpire.'\', \''. $activeServer.'\', \''.$installDate.'\', \''.$deactiveServer.'\', \''.$deactivatedDate.'\', \''.$powerReq.'\', \''.$sanStor1.'\', \''.$sanStor2.'\', \''.$driveSpace1.'\', \''.$driveSpace2.'\', \''.$initiatorAddress1.'\', \''.$initiatorAddress2.'\', \''.$promStor1.'\', \''.$promStor2.'\', \''.$promStorSpace1.'\', \''.$promStorSpace2.'\', \''.$prom1InitAddress.'\', \''.$prom2InitAddress.'\', \''.$availableHours.'\', \''.$availableDays.'\', \''.$downtimeDays.'\', \''.$downtimeHours.'\', \''.$Applications.'\', \''.$assoServ1.'\', \''.$assoServ2.'\', \''.$assoServ3.'\', \''.$assoServ4.'\', \''.$assoServ5.'\', \''.$hardwareVendor.'\', \''.$systemOwner.'\', \''.$databaseOwn.'\', \''.$appOwner.'\', \''.$softwareOwn.'\', \''.$MAC1.'\', \''.$MAC2.'\', \''.$MAC3.'\', \''.$MAC4.'\', \''.$backupServ.'\', \''.$backupServLoc.'\', \''.$backupSize.'\', \''.$repBackupServ.'\', \''.$repBackupLoc.'\', \''.$repBackupSize.'\', \''.$archiveBackup.'\', \''.$arcOffsiteLoc.'\', \''.$arcBackSize.'\', \''.$backupDaily.'\', \''.$backupWeekly.'\', \''.$backupQuarter.'\', \''.$backupYear.'\', \''.$backupMiscInfo.'\', \''.$backupImage.'\', \''.$backupImageLoc.'\', \''.$disasterRecover1.'\', \''.$disasterRecover2.'\', \''.$dailySpaceUsed.'\', \''.$weeklySpaceUsed.'\', \''.$quarterlySpaceUsed.'\', \''.$yearlySpaceUsed.'\', \''.$antiVirusInstall.'\', \''.$antiVirusVersion.'\', \''.$estRepCost.'\', \''.$numUsers.'\', \''.$bandwidth.'\', \''.$totalLocStor.'\', \''.$totNetStor.'\', \''.$equipReplaceTime.'\', \''.$vendorTurn.'\', \''.$venDisCost.'\', \''.$approxTechTime.'\', \''.$totReplaceTime.'\', \''.$appAssure.'\', \''.$backupExec.'\', \''.$otherBackup.'\')';

新客户代码:

$sql    = "SELECT id FROM `tribe-server-inv`.`client_main` ORDER BY id DESC LIMIT 1";
                        $result = mysqli_query($link, $sql);

                        while($row  = mysqli_fetch_assoc($result))
                        {
                            $id = $row['id']+1;
                        }


                        //Client Main
                        $q1 =   "INSERT INTO `tribe-server-inv`.`client_main` (`id` ,`serial` ,`make` ,`model` ," .
                                "`warrentyStart` ,`warrentyEnd` ,`status` ,`type`)VALUES (".$id." , '" .
                                $serial."', '".$make."', '".$model."','".$warrentyStart."', '".$warrentyExpire."', '".$status."', '".$type."');";

                        //Client Deploy
                        $q2 =   "INSERT INTO  `tribe-server-inv`.`client_deploy` (`id` , `ticket` ,`techID` ,`installDate` ,`updateDate`)".
                                "VALUES (".
                                "'".$id."',  '".$ticket."',  '".$tech."',  '".$installDate."',  '".$updateDate."');";

                        //Client Profile
                        $q3 =   "INSERT INTO  `tribe-server-inv`.`client_profile` (`id` ,`username` ,`department` ,`buildID` ,`notes`)" .
                                "VALUES (" .
                                "'".$id."' ,  '".$user."',  '".$department."',  '".$building."',  '".$notes."');";


                        //Client Hardware
                        $q4 =   "INSERT INTO  `tribe-server-inv`.`client_hardware` (" .
                                "`id` ,`cpu` ,`memory` ,`diskSpace1` ,`diskSpace2` ,`diskSpace3`)".
                                "VALUES (".
                                "'".$id."' ,  '".$cpu."',  '".$memory."',  '".$diskSpace1."',  '".$diskSpace2."',  '".$diskSpace3."');";

                        //Client Software
                        $q5 =   "INSERT INTO  `tribe-server-inv`.`client_software` (`id` ,`operating` ,`operating_license` ,`antivirus` ,".
                                "`antivirus_license` ,`office` ,`office_license`)".
                                "VALUES (".
                                "'".$id."',  '".$operating."',  '".$osLicense."',  '".$antivirusInstalled."',  '".$antivirusVersion."',".
                                "'".$officeVersion."',  '".$officeLicense."');";

                        //Client Network
                        $q6 =   "INSERT INTO  `tribe-server-inv`.`client_network` (`id` ,`ip` ,`physicalMAC` ,`wirelessMAC` ,`port` ,`dns1` ,".
                                "`dns2` ,`dns3` ,`dns4`)".
                                "VALUES (".
                                "'".$id."',  '".$ip."',  '".$phyMac."',  '".$wireMac."',  '".$port."',  '".$dns1."',  '".$dns2."',  '".$dns3."',  '".$dns4."');";