Jump to content

ncurran217

Members
  • Posts

    83
  • Joined

  • Last visited

ncurran217's Achievements

Member

Member (2/5)

1

Reputation

  1. This worked amazing! Thank you very much!
  2. Thanks for the replies and help. I will try your guys suggestions and see what works best.
  3. I am working on trying to get php code to take a record set from mysql table and rotate the records, so when you refresh the page, the first record would show at the top of the list, then record 2 and so on down the line. If you were a new person to come to the page, record 2 would show at the top of the list, then record 3 and down the line, and record 1 would now be at the bottom. I have a position column in the table and when the page loads it will update the position field of each record to be one less then it was before, so when the next person to come to the page, it will be up one spot in the table. My issue is that when the page gets refreshed to quickly or I have had it not finish updating the records and then there is multiple records with the same position number. I am looking to only have 9 records only for the example, so if Position is set to 1 it will be set to 9, if not it will take Position - 1. if(isset($_GET["page"])){ } else{ $sql = "SELECT ID,Name,Position FROM tableA ORDER BY Position DESC"; $result = $conn->query($sql); while ($row = $result->fetch_assoc()) { if($row["Position"] == 1){ $p = 9; } else{ $p = $row["Position"]-1; } $conn->query("UPDATE tableA SET Position = $p WHERE ID = $row[ID]"); } mysqli_free_result($result); } Am I going down the right path? It just seems like it will not work 100% of the time. Hopefully I explained what is going wrong fully and clearly. Thank in advance.
  4. I am trying to remove some information off urls that are placed into my database. I have this query that I am using: Select substring_index(refurl,'?gclid',1) as refurl, Count(*) from leads group by substring_index(refurl,'?gclid',1) But for the delimiter I want it to clear off ?gclid or &gclid, is this possible by doing an OR statement within the substring or is it something completely different to get this done?
  5. Well I figured it out. It had to do with how the first IF then ELSE statement were setup.
  6. Well I have this login script, and there are if statements if something doesn't match up and it will set $error with a sentence of what is wrong. Then at the bottom of the code has the login form, with an if statement that says if $error is set, then echo $error. But when actually put in wrong credentials, it goes to a blank page and doesn't echo $error. If I put in the correct credentials it goes to the next page properly. Here is my code, what am I doing wrong? Thanks for the help in advance! <?php include 'includes/db_connect.php'; $self = $_SERVER['PHP_SELF']; //-----------------------------------Anti-Injection Function----------------------------------------------- function anti_injection($sql) { $sql = preg_replace("/(from|select|insert|delete|where|drop table|show tables|#|\*|--|\\\\)/","",$sql); $sql = trim($sql); $sql = strip_tags($sql); $sql = addslashes($sql); return $sql; } //---------------------------------------------------------------------------------------------------------- if(isset($_POST['user']) && $_POST['pass']){ $user = anti_injection($_POST['user']); $pass = anti_injection($_POST['pass']); $crypt_pass = md5($pass); $query1 = "SELECT username FROM Logins WHERE Username = '".$user."'"; $params1 = array(); $options1 = array( "Scrollable" => SQLSRV_CURSOR_KEYSET ); $result1 = sqlsrv_query( $connection, $query1 , $params1, $options1 ); $count1 = sqlsrv_num_rows($result1); $query2 = "SELECT password FROM Logins WHERE Username = '".$user."'"; $result2 = sqlsrv_query( $connection, $query2); while ($row2=sqlsrv_fetch_array( $result2, SQLSRV_FETCH_ASSOC)) { $sysadmin_password = $row2['password']; } if($count1 == '0') { $error = 'This account is not found in the database.'; } elseif($sysadmin_password != $crypt_pass) { $error = 'Wrong password. Try again.'; } elseif($_GET['login'] != 'login' && $count1 == '0') { $error = 'Login Error, Please login again.'; } else { session_start(); $_SESSION['valid_sysadmin'] = $user; echo "<script type='text/javascript' language='javascript'> window.location.href = 'index.php';</script>"; exit; // Dont forget to and your session // session_destroy(); // End secure content } } else { // build form echo "<title>Mail Report</title>"; echo "<form name='auth' action='$self' method='post'>\n"; echo "<table align=center width=210 border=0 cellpadding=7 cellspacing=1>\n"; echo " <tr class=right_main_text><td colspan=2 height=35 align=center valign=top><h2>Mail Report Login</h2></td></tr>\n"; echo " <tr class=right_main_text><td align=left>Username:</td><td align=right><input type='text' name='user' maxlength='30'></td></tr>\n"; echo " <tr class=right_main_text><td align=left>Password:</td><td align=right><input type='password' name='pass' maxlength='16'></td></tr>\n"; echo " <tr class=right_main_text><td align=center colspan=2><input type='submit' value='Login'> </td></tr>\n"; if (isset($error)) { echo "<tr class=right_main_text><td align=center colspan=2><font color='red'>'".$error."'</font></td></tr>\n"; } echo "</table>\n"; echo "</form>\n"; } ?>
  7. I have this import code and it works if I take the header out of the csv file, but I would like to keep the header in the file so I can keep a template for my co-workers to use so there is no issues with what columns are which. Here is my code: <?php /* Format the errors and die */ function get_last_error() { $retErrors = sqlsrv_errors(SQLSRV_ERR_ALL); $errorMessage = 'No errors found'; if ($retErrors != null) { $errorMessage = ''; foreach ($retErrors as $arrError) { $errorMessage .= "SQLState: ".$arrError['SQLSTATE']."<br>\n"; $errorMessage .= "Error Code: ".$arrError['code']."<br>\n"; $errorMessage .= "Message: ".$arrError['message']."<br>\n"; } } die ($errorMessage); } /* connect */ function connect() { if (!function_exists('sqlsrv_num_rows')) { // Insure sqlsrv_1.1 is loaded. die ('sqlsrv_1.1 is not available'); } /* Log all Errors */ sqlsrv_configure("WarningsReturnAsErrors", TRUE); // BE SURE TO NOT ERROR ON A WARNING sqlsrv_configure("LogSubsystems", SQLSRV_LOG_SYSTEM_ALL); sqlsrv_configure("LogSeverity", SQLSRV_LOG_SEVERITY_ALL); include '/includes/db_connect.php'; if ($connection === FALSE) { get_last_error(); } return $connection; } function query($connection, $query) { $result = sqlsrv_query($connection, $query); if ($result === FALSE) { get_last_error(); } return $result; } /* Prepare a reusable query (prepare/execute) */ function prepare ( $connection, $query, $params ) { $result = sqlsrv_prepare($connection, $query, $params); if ($result === FALSE) { get_last_error(); } return $result; } /* do the deed. once prepared, execute can be called multiple times getting different values from the variable references. */ function execute ( $stmt ) { $result = sqlsrv_execute($stmt); if ($result === FALSE) { get_last_error(); } return $result; } function fetch_array($query) { $result = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC); if ($result === FALSE) { get_last_error(); } return $result; } $connection = connect(); /* prepare the statement */ $query = "INSERT Records values ( ? , ? , ? )"; $param1 = null; // this will hold col1 from the CSV $param2 = null; // this will hold col2 from the CSV $param3 = null; // this will hold col3 from the CSV $params = array( &$param1, &$param2, &$param3 ); $prep = prepare ( $connection, $query, $params ); // $result = execute ( $prep ); //get the csv file $file = $_FILES["importrecords"]["tmp_name"]; /* Here is where you read in and parse your CSV file into an array. That may get too large, so you would have to read smaller chunks of rows. */ $csv_array = file($file); $_SESSION['records_row_count'] = 0; foreach ($csv_array as $row_num => $row) { $_SESSION['records_row_count']++; $row = trim ($row); $column = explode ( ',' , $row ); $param1 = $column[0]; $param2 = $column[1]; $param3 = $column[2]; // insert the row $result = execute ( $prep ); } /* Free statement and connection resources. */ sqlsrv_close($connection); header( "Location: import.php?successrecords=1" ); ?> How would I accomplish leaving the header in the csv file but skipping on the import? I tried adding a count, but that didn't work. Thanks for the help in advance!
  8. That worked perfectly. Thank you so much. I was looking at it for so long, just needed a second set of eyes I guess. Thanks again. Kicken, when I did what you said, it still added the row of NULL values. Also, adding the & to $params in the function came back as deprecated. Thank you for the input as well!
  9. I have found some code that I am trying to work with to import some data from a CSV file into a table of my database. It works and imports the data, but always inserts a NULL row for each column. Here is the code: <?php $self = $_SERVER['PHP_SELF']; $request = $_SERVER['REQUEST_METHOD']; if (!isset($_GET['success'])) { $get_success = ""; } else { $get_success = $_GET['success']; } if (!empty($_FILES)) { /* Format the errors and die */ function get_last_error() { $retErrors = sqlsrv_errors(SQLSRV_ERR_ALL); $errorMessage = 'No errors found'; if ($retErrors != null) { $errorMessage = ''; foreach ($retErrors as $arrError) { $errorMessage .= "SQLState: ".$arrError['SQLSTATE']."<br>\n"; $errorMessage .= "Error Code: ".$arrError['code']."<br>\n"; $errorMessage .= "Message: ".$arrError['message']."<br>\n"; } } die ($errorMessage); } /* connect */ function connect() { if (!function_exists('sqlsrv_num_rows')) { // Insure sqlsrv_1.1 is loaded. die ('sqlsrv_1.1 is not available'); } /* Log all Errors */ sqlsrv_configure("WarningsReturnAsErrors", TRUE); // BE SURE TO NOT ERROR ON A WARNING sqlsrv_configure("LogSubsystems", SQLSRV_LOG_SYSTEM_ALL); sqlsrv_configure("LogSeverity", SQLSRV_LOG_SEVERITY_ALL); $conn = sqlsrv_connect('cslogs', array ( 'UID' => 'mailreport', 'PWD' => '123456', 'Database' => 'Mail', 'CharacterSet' => 'UTF-8', 'MultipleActiveResultSets' => true, 'ConnectionPooling' => true, 'ReturnDatesAsStrings' => true, )); if ($conn === FALSE) { get_last_error(); } return $conn; } function query($conn, $query) { $result = sqlsrv_query($conn, $query); if ($result === FALSE) { get_last_error(); } return $result; } /* Prepare a reusable query (prepare/execute) */ function prepare ( $conn, $query, $params ) { $result = sqlsrv_prepare($conn, $query, $params); if ($result === FALSE) { get_last_error(); } return $result; } /* do the deed. once prepared, execute can be called multiple times getting different values from the variable references. */ function execute ( $stmt ) { $result = sqlsrv_execute($stmt); if ($result === FALSE) { get_last_error(); } return $result; } function fetch_array($query) { $result = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC); if ($result === FALSE) { get_last_error(); } return $result; } $conn = connect(); /* prepare the statement */ $query = "INSERT Records values ( ? , ? , ? )"; $param1 = null; // this will hold col1 from the CSV $param2 = null; // this will hold col2 from the CSV $param3 = null; // this will hold col3 from the CSV $params = array( $param1, $param2, $param3 ); $prep = prepare ( $conn, $query, $params ); $result = execute ( $prep ); //get the csv file $file = $_FILES["csv"]["tmp_name"]; /* Here is where you read in and parse your CSV file into an array. That may get too large, so you would have to read smaller chunks of rows. */ $csv_array = file($file); foreach ($csv_array as $row_num => $row) { $row = trim ($row); $column = explode ( ',' , $row ); $param1 = $column[0]; $param2 = $column[1]; $param3 = $column[2]; // insert the row $result = execute ( $prep ); } /* Free statement and connection resources. */ sqlsrv_close($conn); header( "Location: test.php?success=1" ); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Import a CSV File with PHP & MS SQL Server</title> </head> <body> <?php if (!empty($get_success)) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?> <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> Choose your file: <br /> <input name="csv" type="file" id="csv" /> <input type="submit" name="Submit" value="Submit" /> </form> </body> </html> Now I know it is at this part wiht the $param1, $param2 and $param 3 that it is inserting the NULL values for each column: /* prepare the statement */ $query = "INSERT Records values ( ? , ? , ? )"; $param1 = null; // this will hold col1 from the CSV $param2 = null; // this will hold col2 from the CSV $param3 = null; // this will hold col3 from the CSV $params = array( $param1, $param2, $param3 ); $prep = prepare ( $conn, $query, $params ); $result = execute ( $prep ); But if I take that out them three out, the php errors out then doesn't import the data. Is there a way with what I have to ignore the first row to import? Or am I going about this all wrong?
  10. I am moving my site and database to a new server. The configuration is nearly the same except the new server is 64bit. I can connect to the database remotely with the username and password that I have in my php code to connect. Here is my database connect code: <?php $serverName = 'Nick_C_Desktop\SQLEXPRESS'; $connectionInfo = array('Database'=>'CSLogs', 'UID'=>'cslogslogin', 'PWD'=>'123456'); $connection = sqlsrv_connect($serverName, $connectionInfo); ?> This code worked perfectly fine on my old server, but gives me this error: Fatal error: Call to undefined function sqlsrv_connect() in C:\wamp\www\cslogs\includes\db_connect.php on line 4 I have these two items in my php.ini file enabled: extension=php_pdo_sqlsrv_53_ts.dll extension=php_sqlsrv_53_ts.dll Which is what I had before. Using Wamp Server. PHP 5.3.13 all the same as before. What else am I missing that is not allowing this to connect to the SQL server. Thanks in advance!
  11. Ok I finally found something that worked. Sorry for the premature post, but if there are possibly better ways to do this that would be nice to know. Here is what I got and I will mark as solved: // each array key is the database column name, the corresponding value is the legend/heading to display in the HTML table // the order of the items in this array are the order they will be output in the HTML table $fields = array('RefNumber'=>'Reference Number','Status'=>'Status','Rep'=>'Rep','Disposition'=>'Disposition','appNumber'=>'appNumber', 'Con_Number'=>'Contract Number','Finance_Num'=>'Finance Number','Phone_Num'=>'Phone Number','Disc_Amount'=>'Discount Amount', 'Total_Cost'=>'Total Cost','Total_MP'=>'Total Monthly Payments','New_MP_Amt'=>'New MP Amount','New_DP_Amt'=>'New DP Amount','Notes'=>'Notes'); // start table and produce table heading $color1 = "white"; $color2 = "#A4A4A4"; $i = 1; echo "<table>\n<tr>"; foreach($fields as $legend){ echo "<th>$legend</th>"; } echo "</tr>\n"; // output table data while($row = sqlsrv_fetch_array( $result,SQLSRV_FETCH_ASSOC)) { if ($i % 2 != 0) //An Odd Row {$rowcolor = $color1;} else //An Even Row {$rowcolor = $color2;} echo "<tr bgcolor='$rowcolor'>"; foreach($fields as $key=>$not_used) { echo "<td>$row[$key]</td>"; } $i++; //Increment Row Counter echo "</tr>\n"; } echo "</table>\n";
  12. I am looking to alternate the row color on this loop I have. I have looked up a couple things through google, but none of them seem to fit into what I have. Let me know if this is simple or any suggestions on how to do this. Here is my code: $fields = array('RefNumber'=>'Reference Number','Status'=>'Status','Rep'=>'Rep','Disposition'=>'Disposition','appNumber'=>'appNumber', 'Con_Number'=>'Contract Number','Finance_Num'=>'Finance Number','Phone_Num'=>'Phone Number','Disc_Amount'=>'Discount Amount', 'Total_Cost'=>'Total Cost','Total_MP'=>'Total Monthly Payments','New_MP_Amt'=>'New MP Amount','New_DP_Amt'=>'New DP Amount','Notes'=>'Notes'); // start table and produce table heading echo "<table>\n<tr>"; $color1 = "#EFEFEF"; $color2 = "#FBFBFB"; foreach($fields as $legend){ echo "<th>$legend</th>"; } echo "</tr>\n"; // output table data while($row = sqlsrv_fetch_array( $result,SQLSRV_FETCH_ASSOC)) { echo "<tr>"; foreach($fields as $key=>$not_used) { echo "<td>$row[$key]</td>"; } echo "</tr>\n"; } echo "</table>\n"; $color1 & $color2 are the background colors I want to alternate on each row.
  13. Ok, so now this one pops up: (!preg_match ("#^([[:alnum:]]|~|\!|@|#|\$|%|\^|&|\*|\(|\)|-|\+|`|_|\=|\{|\}|\[|\]|\||\:|\<|\>|\.|,|\?)+$#", $password)) with error: Warning: preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Unknown modifier '|'
  14. Ok, I have never used this function. Is there something you suggest, as I have no clue what this function does.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.