I want to insert into the proper fields in order. Is this the right syntax? mysql_select_db("kjv"); $rs = mysql_query("SHOW FIELDS FROM bible"); while ($row = mysql_fetch_array($rs)) { $sql = "INSERT INTO bible (".$row['Field'].") VALUES ('".$foo[1][9]."')"; mysql_query($sql) or die(mysql_error()); //echo "{$row['Field']}<br />\n"; echo $sql."<br />\n"; } PHP: I know I have to make corrections. But the problem is that I don't see inserting into the db table.
Could you explain what $foo is a bit more? What would $foo hold, and what would an item in $foo, like $foo[1], hold?
$foo is what holds the preg_match_all with contents The only thing that bugs me is this part mysql_select_db("kjv"); $rs = mysql_query("SHOW FIELDS FROM bible"); while ($row = mysql_fetch_array($rs)) { $sql = "INSERT INTO bible (".$row['Field'].") VALUES ('".$foo[1][9]."')"; mysql_query($sql) or die(mysql_error()); //echo "{$row['Field']}<br />\n"; echo $sql."<br />\n"; } PHP: $sql = "INSERT INTO bible (".$row['Field'].") VALUES ('".$foo[1][9]."')"; PHP: You are looping through, meaning that its not doing id, book, book_spoke. You need to structure $row to be INSERT INTO bible (id, book, book_spoke, etc etc) VALUES ('','Genesis',$foo[1][9], etc etc); PHP: Instead you are telling the loop to insert INSERT INTO bible (id) VALUES ($foo[1][9]); INSERT INTO bible (book) VALUES ($foo[1][9]); The thing is that, if you have structured your columns to be, id = bigint,int,smallint it will become a 0 if bible column is varchar(40), it will limit your text And so on.
Ok soinstead I'm thinking of placing 2 function within the brackets: $sql = "INSERT INTO bible (".$row['Field'].") VALUES ('".$foo[1][9]."')"; PHP: This would turn out something like this (correct me if I'm doing it wrong): $i = 0; mysql_select_db("kjv"); $rs = mysql_query("SHOW FIELDS FROM bible"); function fields(){ $fields = array(); $fields = ""; while ($row = mysql_fetch_array($rs)) {//line 86 if($row['Field']!="id"){ $fields .= $row['Field'].", "; } $i++; } return $fields; } function rowValues(){ $rowValues = ""; for($i = 0; $i < 10; $i++){ if($i % 10 != 0){ $rowValues .= $foo[1][$i].", "; } } return $rowValues; } while ($row = mysql_fetch_array($rs)) { if($row['Field']!="id"){ $sql = "INSERT INTO bible (".fields().") VALUES ('".rowValues()."')"; mysql_query($sql) or die(mysql_error()); //echo "{$row['Field']}<br />\n"; echo $sql."<br />\n"; } $i++; } PHP: But I get this error:
The query shld be like this. Replace "etc" with the values of fields tht u want to insert. mysql_select_db("kjv"); $rs = mysql_query("SHOW FIELDS FROM bible"); $strfields = ""; while ($row = mysql_fetch_array($rs)) { // create a string of fileds $strfields .= $row['Field'] . ","; } if(isset($strfields) && $strfields != "") { $strfields = substr($strfields,0,strlen($strfields)-1); // etc should be the value of the each filed that you want to insert. $sql = "INSERT INTO bible (".$strfields.") VALUES ('".$foo[1][9]."', 'etc','etc',.........)"; mysql_query($sql) or die(mysql_error()); //echo "{$row['Field']}<br />\n"; echo $sql."<br />\n"; }
I don't understand why it's saying that I'm redeclaring fields(): $i = 0; mysql_select_db("kjv"); $rs = mysql_query("SHOW FIELDS FROM bible"); function fields(){//line 82 $strFields = array(); $strFields = ""; while ($row = mysql_fetch_array($rs)) { if($row['Field']!="id"){ $strFields .= $row['Field'].", "; } $i++; } return $strFields; } function rowValues(){ $rowValues = ""; for($i = 0; $i < 10; $i++){ if($i % 10 != 0){ $rowValues .= $foo[1][$i].", "; } } return $rowValues; } while ($row = mysql_fetch_array($rs)) { if($row['Field']!="id"){ $sql = "INSERT INTO bible (".fields().") VALUES ('".rowValues()."')"; //mysql_query($sql) or die(mysql_error()); //echo "{$row['Field']}<br />\n"; echo $sql."<br />\n"; } //$i++; } PHP:
Because function fields() is already declared in \new\databasehandling\getFiles\insertindb.php on line 82 try to remove from there or from here. /CarcaBot
I decided to rework the function from scratch. THis is what I came up with: function recValues(){ $contents_of_page = file_get_contents('../bible1.html'); preg_match_all("#<td.*>(.+)</td#Ui", $contents_of_page, $tdInnerHTML); $totaltds = count($tdInnerHTML[1]); $line = 0; $strLine = array(); for($k=0; $k < $totaltds; $k++){ if($k % 10 == 0){ print("line "); $strLine[$line] = } if($k % 10 != 0){ $j = 0; $tdValues = array(); $tdValues[$j] = $tdInnerHTML[1][$k]; print("'"); print("<span style='font-weight: bold; color: red'>".$tdValues[$j]."</span>"); //print($tdInnerHTML[1][$k]); if($k % 10 == 9){ print("'<br />\n"); }else{ print("', "); } //return $tdValues[$j]; $j++; $line++; } } PHP: But I'm stuck. I need $strLine[$line] = to be equal to all this: if($k % 10 != 0){ $j = 0; $tdValues = array(); $tdValues[$j] = $tdInnerHTML[1][$k]; print("'"); print("<span style='font-weight: bold; color: red'>".$tdValues[$j]."</span>"); //print($tdInnerHTML[1][$k]); if($k % 10 == 9){ print("'<br />\n"); }else{ print("', "); } PHP:
I almost succeeded into building my sql statement. But here is the result: As you see there is a repetition. Instead it should be: function recValues(){ $contents_of_page = file_get_contents('../bible1.html'); preg_match_all("#<td.*>(.+)</td#Ui", $contents_of_page, $tdInnerHTML); $totaltds = count($tdInnerHTML[1]); $line = 0; $strLine = array(); for($k=0; $k < $totaltds; $k++){ if($k % 10 == 0){ print("line "); $strLine[$line] = ""; } if($k % 10 != 0){ $j = 0; $tdValues = array(); $tdValues[$j] = $tdInnerHTML[1][$k]; $strLine[$line] .= "'"; //print("'"); $strLine[$line] .= $tdValues[$j]; //print("<span style='font-weight: bold; color: red'>".$tdValues[$j]."</span>"); //print($tdInnerHTML[1][$k]); if($k % 10 == 9){ $strLine[$line] .= "'"; //print("'<br />\n"); }else{ $strLine[$line] .= "', "; //print("', "); } } print($strLine[$line]."<br />\n"); //return $strLine[$line]; $j++; if($k % 9 == 0){ $sql = "INSERT INTO bible (".fields().") VALUES (".$strLine[$line].")"; print("<span style='color: red;'>".$k." ".$sql."</span><br />\n"); $line++; } } } PHP: