artied Posted May 2, 2009 Share Posted May 2, 2009 Hi I have a small script that is meant to trap a HTML POST message, clean it, check it, parse it and then 1 append it to a table in a MySQL db that traps all the messages 2 see if it is a 'new' message and if so append it to the 'latest' table or update the previous message in the latest table there are two sections in the script where the return from a SELECT is tested to see if there is a result or a NULL. in the first it runs fine at line 78 in the second it does not seem to at line 91 here i get this errmsg Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /*****/******/public_html/tally/uploader1.php on line 93 my setup is thus hosting on Bluehost localhost * Server version: 5.0.75-community-log * Protocol version: 10 * Server: Localhost via UNIX socket phpMyAdmin - 2.11.9.4 * MySQL client version: 5.0.75 * Used PHP extensions: mysql any thoughts....???? many thks in advance [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/156552-inconsistent-behaviour-on-v-similar-code-sections/ Share on other sites More sharing options...
artied Posted May 2, 2009 Author Share Posted May 2, 2009 <?php // A JavaME app will be installed on the mobilephones of a number of users (999 < X > 20). // The app is a counter program to count instances of predefined events which it displays to the user and HTML POSTs to a predefined HTML Server. // The app has 2 user configured values. On first run the user is asked for their mob no. // On each iteration of the app the user is asked to input a reference number ( box no ) // The app will send messages to the HTML server at each XX count of instances and when the user indicates that this count episode is finished. // Count episodes will comprise a max of 1000 and an avg of 300 instances. // This script recieves an HTML POST message, tests it for valid composition and cleans it up for safe interaction with a MySQL db // The script, if it can talk to the Server and the DB, will see if the sender has permission to write to the DB // The script will append the message to the message store. // The script will check whether the message is the first from the sender for the box. // If so the message will be added to the latest store. // If not the existing message is updated with the new values. // Thus the table 'messageStore' will trap all messages and the table 'latestStore' will hold the mast recent and/or last messages for each box/count. // The block that starts '$senderCheck' works correctly - line 78 // It is the test to see if the mobile phn no is one we are expecting and will allow to append messages - perhaps unneccessary but security is its own reward // It composes an SQL query to see if the string $sender matches any string in the phn no column of the table 'permitted users' // It submits this query to the DB - a valid MySQL communication always returns an object to the PHP interpreter // If the result is successful the object will be an array of 1 or more rows of data with a start count of row 0 // If the result is a failure the object will be a 'NULL' ie no rows of data // Therefore it is not sufficient to know that the query has been responded to - we must check the nature of what has been sent. // We must see if we have any rows of data // This code is repeated at line 91 - here it does NOT work and returns an err saying the argument is invalid.......... require "db.inc"; require_once "HTML/Template/ITX.php"; // Test for user input - If none of the data elements are missing then strip and save the tally string // No error checking at this stage - just catch a well formed string if (!empty($_POST["count_name"]) && !empty($_POST["sender"]) && !empty($_POST["box"]) && !empty($_POST["n1"]) && !empty($_POST["n2"]) && !empty($_POST["n3"]) && !empty($_POST["n4"]) && !empty($_POST["n5"]) && !empty($_POST["n6"]) && !empty($_POST["n7"]) && !empty($_POST["n8"]) && !empty($_POST["n9"]) && !empty($_POST["n10"]) && !empty($_POST["n11"]) && !empty($_POST["n12"]) && !empty($_POST["c1"]) && !empty($_POST["c2"]) && !empty($_POST["c3"]) && !empty($_POST["c4"]) && !empty($_POST["c5"]) && !empty($_POST["c6"]) && !empty($_POST["c7"]) && !empty($_POST["c8"]) && !empty($_POST["c9"]) && !empty($_POST["c10"]) && !empty($_POST["c11"]) && !empty($_POST["c12"]) ) { // Attempt to connect to DBserver if (!($connection = @ mysql_connect($hostName, $username, $password))) die("Could not connect to database"); // Select the bits of the message string that we want to save ie. the count, box & tallys and the sender // Clean the user set values so as to be SAFE // Also for sender and box take out non numeric chars and interior spaces { $countname = mysqlclean($_POST, "count_name", 10, $connection); $sender = mysqlclean($_POST, "sender", 15, $connection); $sender = trim($sender, " -/+(.)"); $sender = str_replace(" ", "", $sender); $box = mysqlclean($_POST, "box", 10, $connection); $box = trim($box, " -/+(.)"); $box = str_replace(" ", "", $box); $c1 = mysqlclean($_POST, "c1", 5, $connection); $c2 = mysqlclean($_POST, "c2", 5, $connection); $c3 = mysqlclean($_POST, "c3", 5, $connection); $c4 = mysqlclean($_POST, "c4", 5, $connection); $c5 = mysqlclean($_POST, "c5", 5, $connection); $c6 = mysqlclean($_POST, "c6", 5, $connection); $c7 = mysqlclean($_POST, "c7", 5, $connection); $c8 = mysqlclean($_POST, "c8", 5, $connection); $c9 = mysqlclean($_POST, "c9", 5, $connection); $c10 = mysqlclean($_POST, "c10", 5, $connection); $c11 = mysqlclean($_POST, "c11", 5, $connection); $c12 = mysqlclean($_POST, "c12", 5, $connection); } // Check that the phn no and the box are properly formatted if (!ereg ("^[0][0-9]{9}", $sender)) die ("Incorrectly formatted phone number."); if (!ereg ("^[0-9]{1,4}", $box)) die ("Incorrectly formatted box number."); // Attempt to connect to DB if (!mysql_select_db($databaseName, $connection)) showerror(); // Check that the sender has permission to append data - if phn no not found then DIE $senderCheck = "SELECT * FROM permitted_users WHERE permitted_phn = {$sender}"; // build the query - the string $senderCheck $result = @ mysql_query ($senderCheck, $connection); // submit the query - the result is called $result $returned_rows = mysql_num_rows ($result); // how many rows of data returned if ($returned_rows == 0) die ("Phone number not accepted."); // if none returned then data insertion not allowed // Build the new SQL stmt to insert the new message $storeMessage = "INSERT INTO message_store VALUES (NULL, NULL, '{$countname}', '{$sender}', '{$box}', '{$c1}', '{$c2}', '{$c3}', '{$c4}', '{$c5}', '{$c6}', '{$c7}', '{$c8}', '{$c9}', '{$c10}', '{$c11}', '{$c12}')"; // Insert the new message into the message store if (!(@mysql_query ($storeMessage, $connection))) showerror(); // compare this current message to the contents of the LATEST table - insert or replace as appropriate - allow for first strings for new boxes $latestcheck = "SELECT * FROM latest_store WHERE countName = {$countname} AND sender = {$sender} AND boxNo = {$box}"; // build the query - the string $latestCheck $latestresult = @ mysql_query ($latestcheck, $connection); // submit the query - $lrr = mysql_num_rows ($latestresult); // how many rows of data returned print ( "latest rows" . $lrr . "<br>") ; // If none returned then this is a first message for a new box and is to be inserted if ($lrr == 0) { // Build the new SQL stmt to insert the new message $insertlatest = "INSERT INTO latest_store VALUES (NULL, NULL, '{$countname}', '{$sender}', '{$box}', '{$c1}', '{$c2}', '{$c3}', '{$c4}', '{$c5}', '{$c6}', '{$c7}', '{$c8}', '{$c9}', '{$c10}', '{$c11}', '{$c12}')"; // Insert the new message into the message store if (!(@mysql_query ($insertlatest, $connection))) showerror(); echo "Insertion"; } // If row is returned then this is an update message for a box and the record is to be updated else{ // Build the new SQL stmt to update with the new values $updatelatest = "UPDATE latest_store SET count_1 = {$c1}, count_2 = {$c2}, count_3 = {$c3}, count_4 = {$c4}, count_5 = {$c5}, count_6 = {$c6}, count_7 = {$c7}, count_8 = {$c8}, count_9 = {$c9}, count_10 = {$c10}, count_11 = {$c11}, count_12 = {$c12}, WHERE countName = {$countname} AND sender = {$sender} AND boxNo = {$box}"; // Insert the new message into the message store if (!(@mysql_query ($updatelatest, $connection))) showerror(); echo "Update"; } // display what was entered if debug is on else display update confirmation if ($debug) { $template = new HTML_Template_ITX("./templates"); $template->loadTemplatefile("uploader1.tpl", true, true); $template->setCurrentBlock(); $template->setVariable("COUNTNAME", $countname); $template->setVariable("SENDER", $sender); $template->setVariable("BOX", $box); $template->setVariable("C1", $c1); $template->setVariable("C2", $c2); $template->setVariable("C3", $c3); $template->setVariable("C4", $c4); $template->setVariable("C5", $c5); $template->setVariable("C6", $c6); $template->setVariable("C7", $c7); $template->setVariable("C8", $c8); $template->setVariable("C9", $c9); $template->setVariable("C10", $c10); $template->setVariable("C11", $c11); $template->setVariable("C12", $c12); $template->parseCurrentBlock(); $template->show(); } else echo "Valid Message received and processed"; } // if empty() else{ // Missing data: Go back to the <form> header("Location: formmenu.html"); } ?> Link to comment https://forums.phpfreaks.com/topic/156552-inconsistent-behaviour-on-v-similar-code-sections/#findComment-824337 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.