check the INSERT into table name if it works

Discussion in 'PHP' started by gilgalbiblewheel, Jul 22, 2008.

  1. #1
    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.
     
    gilgalbiblewheel, Jul 22, 2008 IP
  2. omgitsfletch

    omgitsfletch Well-Known Member

    Messages:
    1,222
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    145
    #2
    Could you explain what $foo is a bit more? What would $foo hold, and what would an item in $foo, like $foo[1], hold?
     
    omgitsfletch, Jul 22, 2008 IP
  3. Mozzart

    Mozzart Peon

    Messages:
    189
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $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.
     
    Mozzart, Jul 22, 2008 IP
  4. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #4
    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:
     
    gilgalbiblewheel, Jul 22, 2008 IP
  5. Mozzart

    Mozzart Peon

    Messages:
    189
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    you didn't pass $rs through fields();
     
    Mozzart, Jul 22, 2008 IP
  6. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #6
    How? By putting $rs in the function bracket?fields($rs);?
     
    gilgalbiblewheel, Jul 22, 2008 IP
  7. lamborevijay

    lamborevijay Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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";
    }
     
    lamborevijay, Jul 23, 2008 IP
  8. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #8
    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:
     
    gilgalbiblewheel, Jul 23, 2008 IP
  9. CarcaBot

    CarcaBot Active Member

    Messages:
    389
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #9
    Because function fields() is already declared in \new\databasehandling\getFiles\insertindb.php on line 82

    try to remove from there or from here.

    /CarcaBot
     
    CarcaBot, Jul 23, 2008 IP
  10. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #10
    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:
     
    gilgalbiblewheel, Jul 24, 2008 IP
  11. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #11
    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:
     
    gilgalbiblewheel, Jul 24, 2008 IP