Jump to content

stupid issue, need help please


leicaphotos

Recommended Posts

I've got code that I believe should work, it displays the variables correctly, just is not inserting anything into mysql --- I had it almost working, but it was inserting each record twice --- here are some code snippets

 

 

My insert statement is as follows:

 

$sql="Insert into recipe (recipe_ID,recipe_title,recipe_author,ingredients,instructions,)";
	$sql.="values ('$counter','$recipe_title','$author','$ingredients', '$instructions')";
	mysql_query($sql);

 

 

My sql structure is as follows:--

 

CREATE TABLE IF NOT EXISTS `recipe` (
  `recipe_ID` int(4) NOT NULL auto_increment,
  `CAT_ID` int(4) NOT NULL,
  `recipe_title` text NOT NULL,
  `recipe_author` text NOT NULL,
  `ingredients` text NOT NULL,
  `instructions` text NOT NULL,
  `recipe_date` varchar(20) NOT NULL,
  `homepage` varchar(50) NOT NULL,
  `link_approved` tinyint(1) NOT NULL,
  `hits` int(4) NOT NULL,
  `rating` varchar(50) NOT NULL,
  `no_rates` int(4) NOT NULL,
  `total_comments` int(4) NOT NULL,
  `hit_date` datetime NOT NULL,
  PRIMARY KEY  (`recipe_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


 

 

Link to comment
https://forums.phpfreaks.com/topic/114837-stupid-issue-need-help-please/
Share on other sites

You have an extra comma in the query:

<?php
$sql="Insert into recipe (recipe_ID,recipe_title,recipe_author,ingredients,instructions,)";
?>

should be

<?php
$sql="Insert into recipe (recipe_ID,recipe_title,recipe_author,ingredients,instructions)";
?>

 

It's always helpful to use an "or die" clause on the mysql_query statement:

<?php
$rs = mysql_query($sql) or die("Problem with the query: $sql<br>" . mysql_error());
?>

 

Ken

echo your SQL string variable and use the die(mysql_error()) function mentioned about, though it sounds like that won't do anything in this case since your query is working, it's just not working how you want it to - so try outputting the SQL to the screen to see what it's actually sending to the database.

anybody see anything stupid I'm doing that's causing the dup entries --- have almost got it doing what I want but there's several thousand recipes and I'm trying to avoid the dupes

 


<?php

$dbserver	="localhost";
$database_connect="memphisb_articles";
$dbuser		="memphisb_admin";
$dbpass		="m3mph1s";

$connect		=	mysql_connect($dbserver,	$dbuser,	$dbpass)
				or die("Couldn't connect to MySQL");
mysql_select_db($database_connect, $connect); 


function ListFiles($dir) {

   if($dh = opendir($dir)) {

       $files = Array();
       $inner_files = Array();

       while($file = readdir($dh)) {
           if($file != "." && $file != ".." && $file[0] != '.') {
               if(is_dir($dir . "/" . $file)) {
                   $inner_files = ListFiles($dir . "/" . $file);
                   if(is_array($inner_files)) $files = array_merge($files, $inner_files); 
               } else {
                   array_push($files, $dir . "/" . $file);
               }
           }
       }

       closedir($dh);
       return $files;
   }
}

$counter = 1;

$website = "<a href=\"http://www.domain.com/\" title=\"Recipe courtesy of domain.com\">Cookbooks.com</a>";

$recipe_date = date("m/d/Y");

$recipe_category = "Appetizers";

$recipe_cat_id = "1";

$author="Blake";

foreach (ListFiles('/home/memphisb/public_html/test') as $key=>$file){

$file2 = file_get_contents ($file); 

//echo "File content: $file2<br><hr>";
$string = $file2;
$regexp = '~<title>(.*)</title>.*<hr>(.*)<br>[\n]<br>(.*)<!--~isU';


preg_match($regexp, $string, $matches);
array_shift($matches);
$hits = rand(1, 10000); 
$rating = rand(1, 10);
$total_rates = rand(1, $hits);
$total_comments = rand(1, 300);

$recipe_title= $matches[0];
$ingredients= $matches[1];
$instructions= $matches[2];


$instructions = strip_tags("$instructions", ENT_QUOTES);

echo $file ."<br />";
echo '<strong>Recipe ID:</strong>  ' . $counter . '<br />
<strong>Title</strong>:  ' . $recipe_title . '<br />
<strong>Ingredients</strong>:  ' . $ingredients . '<br />
<strong>Instructions</strong>:  ' . $instructions . '<br />
<strong>Recipe Date:</strong>  ' . $recipe_date . '<br />
<strong>hits:</strong>  ' . $hits . '<br />
<strong>Website:</strong>  ' . $website . '<br />
<strong>Rating:</strong>  ' . $rating . ' Peppers<br />
<strong>Total Votes:</strong>  ' . $total_rates . '<br />
<strong>Category</strong>:  ' . $recipe_category . '<br /><hr >';


$recipe_title=addslashes($recipe_title);
$ingredients=addslashes($ingredients);
$instructions=addslashes($instructions);
$recipe_category=addslashes($recipe_category);

$sql="Insert into recipe (recipe_ID,recipe_title,recipe_author,ingredients,instructions,recipe_date,hits,homepage,rating,no_rates,recipe_category)";
	$sql.="values ('$counter','$recipe_title','$author','$ingredients', '$instructions', '$recipe_date','$hits','$website','$rating','$total_rates','$recipe_category')";
	mysql_query($sql);



echo "$counter records added";
$counter = $counter + 1;

if (!mysql_query($sql,$connect))
 {
 die('Error: ' . mysql_error());
 }

}

?>


just as revraz predicted, you run mysql_query() twice:

$sql="Insert into recipe (recipe_ID,recipe_title,recipe_author,ingredients,instructions,recipe_date,hits,homepage,rating,no_rates,recipe_category)";
	$sql.="values ('$counter','$recipe_title','$author','$ingredients', '$instructions', '$recipe_date','$hits','$website','$rating','$total_rates','$recipe_category')";
	mysql_query($sql);    //ONCE HERE



echo "$counter records added";
$counter = $counter + 1;

if (!mysql_query($sql,$connect))    //AND ALSO HERE
  {
  die('Error: ' . mysql_error());
  }

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.