douao7937 2015-05-02 14:35
浏览 20

在SQL查询PHP / PDO中避免

I have a problem. I'm trying to update a mysql table with data grabbed. The problem is there is an invisible on the end and it's not functioning properly.

DELETE FROM `tracker`.`gains` WHERE `gains`.`runescape_name` = 'Andy FFS
';
DELETE FROM `tracker`.`gains` WHERE `gains`.`runescape_name` = 'Cancerbear
';
DELETE FROM `tracker`.`gains` WHERE `gains`.`runescape_name` = 'Pandora25';
DELETE FROM `tracker`.`gains` WHERE `gains`.`runescape_name` = 'Pether
';

I have a file called memberlist and update.php loops through each line in memberlist.txt and inserts or updates the columns in the table. If you look at the above query (that is just me trouble shooting) I don't actually want to delete it's just to show you the at the end. It's bringing back 0 in all columns except for the name pandora25 (which happens to be at the bottom of memberlist.txt. What I don't understand is I have a php script to edit memberlist.txt on my website, and that's when the problem occurs. But say I open memberlist.txt via SFTP and upload it to my website, it works correctly. So maybe it's something to do with the editing of the txt file with php and it's adding and invisible at the end I'm not sure. But I have studied quite a bit on this issue and I've looked that you can insert into a MySQL table with ->prepare in PDO (I don't use mysqli).

I believe the loop variable [$k] plays an important role in this problem. Here is my code that I seem to be having a problem with:

        // Connection data (server_address, database, name, poassword)
        $hostdb = 'localhost';
        $namedb = 'tracker';
        $userdb = 'root';
        $passdb = 'password';

        try {
          // Connect and create the PDO object
          $conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
          $conn->exec("SET CHARACTER SET utf8");      // Sets encoding UTF-8

          // Query
          $sqlquery = ("
          INSERT IGNORE INTO 
                gains (runescape_name, hitpoints_starting_exp, magic_starting_exp, range_starting_exp) 
          VALUES 
                ('" . htmlspecialchars($name[$k]) . "',  '" . $hitpointsexp . "', '" . $magicexp . "', '" . $rangedexp . "') 
          ON DUPLICATE KEY UPDATE 
                hitpoints_starting_exp='" . $hitpointsexp . "', magic_starting_exp='" . $magicexp . "', range_starting_exp='" . $rangedexp . "'");
          if($conn->exec($sqlquery) !== false) echo 'completed';       // If the result is not false, display confirmation

          $conn = null;        // Disconnect
        }
        catch(PDOException $e) {
          echo $e->getMessage();
        }

The purpose of this script is to use start.php to insert the current grabbed data into hitpoints_starting_exp, magic_starting_exp and range_starting_exp. I then run stop.php which is a similar script and insert the data grabbed there into hitpoints_end_exp, magic_end_exp and range_end_exp. I then echo a table on index.php which calculates the difference between the two columns.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退