Jump to content

lindm

Members
  • Posts

    199
  • Joined

  • Last visited

    Never

Everything posted by lindm

  1. I have a script that erases all fields of a mysql table according to the script below. Some fields need to be left untouched however and I need help with this in my script. Perhaps there is a better solution to my script? <?php include ('mysqlconnnection.php'); //include connection $qColumnNames = mysql_query("SHOW COLUMNS FROM $table") or die("mysql error"); $numColumns = mysql_num_rows($qColumnNames); $x = 0; while ($x < $numColumns) { $colname = mysql_fetch_row($qColumnNames); $col[$colname[0]] = $colname[0]; $x++; } $querycx = 'UPDATE '.$table.' SET '. implode( '=\'\', ', $col ) .'=\'\' WHERE `userName` = \''. $user2 .'\''; mysql_query($querycx); ?>
  2. I receive an error when trying to add a DATE() field like this ALTER TABLE erevisor ADD CYstart DATE() Where is the error?
  3. I store a date in the format 2007-05-04 in a mysql field varchar(10). I then have a script that extracts the two last figures of the year with a decrease: function taxyear($year) { $year1=strtotime($row[$year]); $year2=date('Y', $year1); return substr($year2,2,4); } I get totally wrong years. 2007-12-31 gives for instance '70' instead of 07 and 2006-07-04 gives me '69'. When I manually change to the string in the function like this all is fine...please help. function taxyear($year) { $year1=strtotime('2004-07-04'); $year2=date('Y', $year1); return substr($year2,2,4); }
  4. Solved it now $qColumnNames = mysql_query("SHOW COLUMNS FROM table LIKE 'cx%'") or die("mysql error"); $numColumns = mysql_num_rows($qColumnNames); $x = 0; while ($x < $numColumns) { $colname = mysql_fetch_row($qColumnNames); $col[$colname[0]] = $colname[0]; $x++; } $querycx = 'UPDATE `table` SET '. implode( '=\'\', ', $col ) .'=\'\' WHERE `userName` = \''. $user2 .'\''; mysql_query($querycx);
  5. I have got the following code working that creates an array in $col of all columns beginning with cx in my table: $qColumnNames = mysql_query("SHOW COLUMNS FROM table LIKE 'cx%'") or die("mysql error"); $numColumns = mysql_num_rows($qColumnNames); $x = 0; while ($x < $numColumns) { $colname = mysql_fetch_row($qColumnNames); $col[$colname[0]] = $colname[0]; $x++; } Now I want to create the following mysql query including all columns from the array: mysql_query( "UPDATE table SET cx1 = '', cx2 = '', cx3 = '', cx4 = '', cx5 = '', etc etc "); Need help finishing the code. Many thanks
  6. But isn't that impossible. I want x to refer to all columns that begin with cx. Instead of writing SET cx1 = '', cx2 ='' etc.. I want to write SET all columns that begin with cx to ''. Can't see how your query can work...
  7. Tried the query but get unknown column error. I have say 20 columns all with the name beginning with cx: cxTEST cxTEST2 cxTEST3 etc Is there a simple query to empty all columns? I tried UPDATE table SET x ='' WHERE x LIKE 'cx%' but no go.
  8. Trying to build a tiny but effective script that removes database data for all columns beginning with cx. It is relating to storing checkbox status. Since an unchecked checkbox doesn't send data I need to "reset" all stored checkbox values before sending new checkbox data. Any suggestions?
  9. Hello again, Stumbled across a problem. I have several checkboxes with the value "checked" in my form . If checked the value should be written to the corresponding mysql column. If not checked of course the column should be nulled. Now with the excellent solutions from discomatt the script ignores the $_POST of the checkboxes when they are unchecked...I want the script to post for instance the value 0 to the mysql column for the checkbox if it is unchecked. Hope I am understandable...possible?
  10. Great! Sees to work and I really like the debug feature.
  11. Get Parse error: syntax error, unexpected $end in Which refers to the end tag ?> What is this due to?
  12. I got help some help some while ago with a script to gather all post data and generate a mysql query to update all mysql columns. The script is based on the form field having the same name as the mysql column name. I have however some problems implementing the code into my script. my current code: if(isset($_POST["save"])){ mysql_query("UPDATE table SET FIELD = '$_POST[field]'"); the code supposed to help: function updateTable($data){ $q = "UPDATE erevisor SET "; $num=count($data); $count=0; foreach($data as $key=>$value){ $count++; if($count<$num){ $q .=$key."='".$value."', "; } else{ $q .=$key."='".$value."' "; } $q .="WHERE userName='$user2'"; } Now how to merge these two?
  13. I have 6 checkboxes. Is there an easy way to check if any 2 of them or more are checked? if (2 or more checkboxes are checked) return xxx; Thanks
  14. Hoping there is a simple solutions to this. I have a form with around 400 fields. When the form is submitted a php script handles this and updates/inserts the record into a mysql database. Now...is there a simple script to update all fields or must this be handled for every field like: mysql_query("UPDATE tableX SET field1 = '$var', field2 = '$var2' etc.. Thanks
  15. Probably a simple solution but here is my problem: My script function princ(field,text) { var text=new Array(); text[0]="test1"; text[1]="test2"; text[2]="test3"; var e = document.getElementById(field); e.value=text[text]; } Is called with princ(PRINCint,1) Get en "undefined error". I must try to access the array wrong...but how?
  16. Think I got it faster now. I separated the variable from the function it it is sooo fast now. $contents = file($_FILES['userfile']['tmp_name']); foreach($contents as $line_value) { list($cat, $int, $id, $num) = preg_split("/(\t| )/",$line_value); $cat = str_replace('#', '', $cat); $number[$cat][$int][$id] = trim($num); } } function calculate_range($cat, $bool, $range_min, $range_max) { global $number; $total = 0; for($i = $range_min; $i <= $range_max; $i++) { if(isset($number[$cat][$bool][$i])) { $total += $number[$cat][$bool][$i]; } } return $total; } Still curious about the filter though...
  17. Alright. So I would then need to filter the text file to include only those lines beginning with either #IB #UB #RES Never seen the php function for this...is it http://se.php.net/filter
  18. Thanks for valuable help. Well the files are too different to cut out the data. Yes, it is called 200 times for different calculations. Any help on the way how to avoid the first part being repeated? Thanks
  19. preg_split seems to be working but now to another problem. Sometimes large text files are used and only a part of this file contains the needed data. The text files normally contains loads of this (no interest for the function): { #TRANS 2890 { } -1080.00 20070408 "" #TRANS 7330 { } 1080.00 20070408 "" } #VER A "88" 20070410 "ver 87" 20070410 { #TRANS 2890 { } 1080.00 20070410 "" #TRANS 1930 { } -1080.00 20070410 "" } #VER A "89" 20070410 "Kort Mars" 20070410 { #TRANS 1930 { } -8025.00 20070410 "" and then at the end or beginning follows the interesting part. Furthermore the calculate_range($cat, $bool, $range_min, $range_max) function is called I guess 200 times when executing my whole php page. This seems to be causing an timeout (30 sec) on the server. Is the above code efficient?
  20. Hi all. Got some help here some months ago with a script to import data from a text file according to following: Part of text file: #UB 0 1930 100000 #UB 0 1940 200000 #RES 0 6620 300000 #RES -1 6620 400000 The code extracts the number to the right depending on the three first identifiers: #XX, 0 or -1, four digit number. The final code allows for the sum of a range of numbers to be returned: function calculate_range($cat, $bool, $range_min, $range_max) { $contents = file($_FILES['userfile']['tmp_name']); foreach($contents as $line_value) { list($_cat, $_bool, $_id, $num) = explode("\t", $line_value); $_cat = str_replace('#', '', $_cat); $number[$_cat][$_bool][$_id] = trim($num); } $total = 0; for($i = $range_min; $i <= $range_max; $i++) { if(isset($number[$cat][$bool][$i])) { $total += $number[$cat][$bool][$i]; } } return $total; } Example: calculate_range('UB', 0, 1930, 1940) would return 300000 from the data above. Now my problem. I have now found that the files I try to import come with various delimiters. The code is written for a tab based delimiter I believe but I want it also to work for a single blank space delimiter. Been fiddeling around with the code but a no go so far. Help appreciated. Text file with single space delimiter: #RES 0 3016 -10711941.97 #RES 0 3031 -23125.00 #RES 0 3080 -1685803.07 #RES 0 3740 3.46 #RES 0 4027 1280448.49 #RES 0 4610 2833530.00 #RES 0 4620 197317.72 #RES 0 5010 28944.00
  21. Then I learned something new today too Many thanks!
  22. The original error is: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'xxx ' at line 1 This however seems to work. $var2=mysql_real_escape_string(stripslashes($var), $con); Any ideas why I shouldn't use this? perhaps an other solution? Thanks
  23. Is this perhaps the best solution? $var2=mysql_real_escape_string(stripslashes($var), $con);
×
×
  • 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.