no update made and no table created

Discussion in 'Databases' started by gilgalbiblewheel, Jul 15, 2008.

  1. #1
    I don't understand why no updating is taking effect and no table is created:
    include("../files/constants.php");
    include("../files/dbconnection.php");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db($acronym."_book", $con);
    $sql = "SELECT * FROM ".$acronym."_pages WHERE content LIKE '%nbsp%'";
    $result = mysql_query($sql) OR exit( 'Error: ' . mysql_error() );
        //echo $sql;
    	//to keep record of what changes are made I decided to create a table
    	$sql_record = "CREATE TABLE ".$acronym."_correction_nbsp
    	(
    	cor_ID int(6) NOT NULL AUTO_INCREMENT,
    	PRIMARY KEY(cor_ID),
    	content_before longtext,
    	content_after longtext
    	)";
        while($row = mysql_fetch_array($result)){
    		$new = preg_replace('#&?nbsp;?#i', ' ', $row['content']);
    		if($row['content'] != ""){
    		$sql_record2 = "INSERT INTO correction_nbsp (content_before, content_after)
    		VALUES (".$row['content'].", ".$new.")";
    		echo $sql_record2."<br />\n";
    		}        
    		$sql2 = "UPDATE ".$acronym."_pages SET content = '".$new."' WHERE content = '".$row['content']."'";
    		echo $sql2."<br />\n";
        }
    mysql_close($con); 
    PHP:
    If you want to simply view the statements here are a few examples:
    INSERT INTO correction_nbsp (content_before, content_after) VALUES ( CHAPTER 1  ,  CHAPTER 1  )
    UPDATE bwas_pages SET content = ' CHAPTER 1  ' WHERE content = ' CHAPTER 1  '
    INSERT INTO correction_nbsp (content_before, content_after) VALUES ( , )
    UPDATE bwas_pages SET content = ' ' WHERE content = ' '
    INSERT INTO correction_nbsp (content_before, content_after) VALUES ( , )
    UPDATE bwas_pages SET content = ' ' WHERE content = ' '
    INSERT INTO correction_nbsp (content_before, content_after) VALUES (chapter: , chapter: )
    UPDATE bwas_pages SET content = 'chapter: ' WHERE content = 'chapter: '
    INSERT INTO correction_nbsp (content_before, content_after) VALUES (copies  &nbspfrom  &nbspauthoritative  &nbspsources  &nbspshowing  &nbspthe , copies from authoritative sources showing the )
    Code (markup):

     
    gilgalbiblewheel, Jul 15, 2008 IP
  2. fantozzi

    fantozzi Member

    Messages:
    42
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #2
    If that's all the code, then you don't execute some of the queries, for instance, you just assign the SQL string to the $sql_record, that's why it never creates the table.

    You should use mysql_query($sql_record); if you want the table to be created.

    Also, you don't lack to put the values inside quotes in the INSERT statement.
     
    fantozzi, Jul 16, 2008 IP
  3. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    Well the entire coding is this:
    <?php 
    include("../files/constants.php");
    include("../files/dbconnection.php");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db($acronym."_book", $con);
    $sql = "SELECT * FROM ".$acronym."_pages WHERE content LIKE '%nbsp%'";
    $result = mysql_query($sql) OR exit( 'Error: ' . mysql_error() );
        //echo $sql;
    	
    $constructed = array();
    $sQry = "SHOW TABLES FROM ".$acronym."_book";
    $qry = mysql_query($sQry) or die(mysql_error());
    while ($tables = mysql_fetch_row($qry))
    {
        $constructed[] = $tables[0];
    }
    
    if (!in_array("correction_nbsp", $constructed))
    {
    	//to keep record of what changes are made I decided to create a table
    		$sql_record = "CREATE TABLE correction_nbsp
    		(
    		cor_ID int(6) NOT NULL AUTO_INCREMENT,
    		PRIMARY KEY(cor_ID),
    		".$acronym."_pages_id int(6),
    		content_before LONGBLOB,
    		content_after LONGBLOB
    		)";
    		mysql_query($sql_record,$con) or die(mysql_error());
    }	
    	
    
    	
    	
    	echo "<span style='font-weight: bold;'>".$sql_record."</span><br />\n";
        while($row = mysql_fetch_array($result)){
    		$old = preg_replace('#&?nbsp;?#i', ' ', $row['content']);
    		$new .= preg_replace("/[^ -þ]/", "", $old);
    		//if($row['content'] != ""){
    			//echo $new."<br />\n";
    			$sql_record2 = "INSERT INTO correction_nbsp (".$acronym."_pages_id, content_before, content_after)
    			VALUES (".$row['id'].", '".$row['content']."', '".$new."')";
    			mysql_query($sql_record2,$con) or die(mysql_error());
    			echo $sql_record2."<br />\n";
    		//}
    		//$sql2 = "UPDATE ".$acronym."_pages SET content = '".preg_replace('/&?nbsp;?/', ' ', $row['content'])."' WHERE id = {$row['id']} LIMIT 1"; 
    		//$sql2 = "UPDATE ".$acronym."_pages SET content = '".$new."' WHERE content = '".$row['content']."'";
    		//mysql_query($sql2,$con) or die(mysql_error());
    		//echo $sql2."<br />\n";
        }
    mysql_close($con);
    PHP:
    That was only within the while loop.
     
    gilgalbiblewheel, Jul 16, 2008 IP