Jump to content

Update is not working in an if statement


randall
 Share

Recommended Posts

Believe me!! I have looked and tried so many snippets of code I am exausted!

 

TableName: keywords

Row: id  productid  keywords

 

Good morning...

I have been struggling with update, I dont know why... it seems easy eneough and it dosent look that different than a simple insert. Would someone please take a look at the if statement below, more specificly the update section. I am getting an error (Parse error: syntax error, unexpected T_STRING in...... ).

 

<?
$keywd = substr(implode(",", array_keys($arr_tem)),0,200);
$productid = intval($_GET['product_id']);

// Make a MySQL Connection
mysql_connect("localhost", "xxxxxxxxx", "xxxxxxxxx") or die(mysql_error());
mysql_select_db("xxxxxxxxx") or die(mysql_error());

$get = "SELECT * FROM keywords WHERE productid = $productid";
$SQ_query = mysql_query($get) or die("Query failed: $get\n" . mysql_error());
$fetch = mysql_fetch_array($SQ_query);

$id = $fetch['productid'];
$key = $fetch['keywords'];

// Evaluates to true because $var is empty
if (empty($id)) {
mysql_query("insert into keywords 
(productid, keywords) VALUES('$productid','$keywd')");
}
else{
if (isset($id)) {
    echo '$var is set even though it is empty';	

$mysql_query = mysql_query(UPDATE keywords
SET keywords = '$keywd'
WHERE productid = '$productid');	

// $mysql_query = mysql_query(UPDATE keywords set keywords = "$keywd" WHERE productid = "$productid"); 

}
endif;
?>

Link to comment
Share on other sites

In the future, post the whole error and the specific lines it refers to.

 

You did not enclose the query in quotes, it needs to be a string. This is an example of when a syntax highlighting editor comes in handy.

Link to comment
Share on other sites

You need quotes around the query.

 

Good catch, OP look at the error, as it will give you clues as to what is wrong.

Notice that it states a parse error not a mysql error, this hints that there is an issue with the php syntax.

Also, endif; if for an if(): statement, I don't see that syntax in your code.

Link to comment
Share on other sites

I'd never even seen the syntax, I was just responding to the person who said they needed an endif.

 

It's an alternative to using brackets, common in templates and such. In my opinion it looks a lot cleaner and easier to follow when you are opening/closing PHP a lot.

// this looks clean
<?php if($something === true): ?>
    <some html>
<?php endif; ?>


// this looks ugly
<?php if($something === true) { ?>
    <some html>
<?php } ?>

 

When you have a few nested brackets (like a conditional inside a loop inside a conditional), all the random closed brackets look awkward and hard to follow. When you have explicit "endif", "endforeach", "endwhile" it's easier to see what goes to what.

Link to comment
Share on other sites

OK, this is a big pain in my bum... This is what I have been working on for the last MONTH!...

PLEASE SOMEONE help...

 

My PHP coding skills are very limmited, I have had most of this done piece by piece. I even had a few ppl at fiverr do some for me. It seems like it will never be complete. It also needs to run within the <head> which seems to throw off the entire thing for some reason.

 

It takes description content from the db, strips all html, css, javascript, as well as removing words that are in the swords.txt file.

Then inserts the remaining words into the database in a table called "keywords" but if the productid already exists update it.

 

Would it be too much if I asked someone to clean this big mess up?

 

URL var = ?product_id=XX

 

Table = keywords

fields = (id)unique index

            productid

            keywords

 

<?php
$con = mysql_connect("localhost","XXXXXXXXX","XXXXXXXXX");
mysql_select_db("XXXXXXXXX",$con);

$productid = intval($_GET['product_id']);

$get = "SELECT * FROM product_description WHERE product_id = $productid";
$SQ_query = mysql_query($get) or die("Query failed: $get\n" . mysql_error());
$fetch = mysql_fetch_array($SQ_query);

$longdescription = $fetch['description'];
$raw = $fetch['description'];
#echo $productid;
#echo $longdescription;

$ugly = $longdescription;
$str = $ugly;

#--------------------------------------Strip html tag----------------------------------------------------
function StripHtmlTags( $text )
{
  // PHP's strip_tags() function will remove tags, but it
  // doesn't remove scripts, styles, and other unwanted
  // invisible text between tags.  Also, as a prelude to
  // tokenizing the text, we need to insure that when
  // block-level tags (such as <p> or <div>) are removed,
  // neighboring words aren't joined.
  $text = preg_replace(
    array(
      // Remove invisible content
      '@<head[^>]*?>.*?</head>@siu',
      '@<style[^>]*?>.*?</style>@siu',
      '@<script[^>]*?.*?</script>@siu',
      '@<object[^>]*?.*?</object>@siu',
      '@<embed[^>]*?.*?</embed>@siu',
      '@<applet[^>]*?.*?</applet>@siu',
      '@<noframes[^>]*?.*?</noframes>@siu',
      '@<noscript[^>]*?.*?</noscript>@siu',
      '@<noembed[^>]*?.*?</noembed>@siu',

      // Add line breaks before & after blocks
      '@<((br)|(hr))@iu',
      '@</?((address)|(blockquote)|(center)|(del))@iu',
      '@</?((div)|(h[1-9])|(ins)|(isindex)|(p)|(pre))@iu',
      '@</?((dir)|(dl)|(dt)|(dd)|(li)|(menu)|(ol)|(ul))@iu',
      '@</?((table)|(th)|(td)|(caption))@iu',
      '@</?((form)|(button)|(fieldset)|(legend)|(input))@iu',
      '@</?((label)|(select)|(optgroup)|(option)|(textarea))@iu',
      '@</?((frameset)|(frame)|(iframe))@iu',
    ),
    array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0",),$text );

  // Remove all remaining tags and comments and return.
  return strtolower( $text );
}

function RemoveComments( & $string )
{
  $string = preg_replace("%(#|;|(//)).*%","",$string);
  $string = preg_replace("%/\*(??!\*/).)*\*/%s","",$string); // google for negative lookahead
  return $string;
}


$html = StripHtmlTags($str);

###Remove number in html################
$html  = preg_replace("/[0-9]/", " ", $html);

#replace   by ' '
$html = str_replace(" ", " ", $html);

######remove any words################

$remove_word = file("swords.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($remove_word as $word) {
$html = preg_replace("/\b". $word ."\b/", " ", $html);
}
######remove space
$html =  preg_replace ('/<[^>]*>/', '', $html);

$html =  preg_replace('/\b\s+/', ', ', $html);
$html =  preg_replace('/[\b\W]+/',', ',$html);   // Strip off spaces and non-alpha-numeric 

#remove white space, Keep : . ( ) : &
//$html = preg_replace('/\s+/', ', ', $html);


###process#########################################################################
$array_loop = explode(",", $html);
$array_loop1 = $array_loop;
$arr_tem = array();

foreach($array_loop as $key=>$val) {
if(in_array($val, $array_loop1)) {
	if(!$arr_tem[$val]) $arr_tem[$val] = 0;
	$arr_tem[$val] += 1;

	if ( ($k = array_search($val, $array_loop1) ) !== false )
	unset($array_loop1[$k]);
}
}

arsort($arr_tem);

###echo top 20 words############################################################
//echo "<h3>Top 20 words used most</h3>";
//$i = 1;
//foreach($arr_tem as $key=>$val) {
//	if($i<=20) {
//		echo $i.":  ".$key." (".$val." words)<br />";
//		$i++;
//	}else break;
//}
//echo "<hr />";
//###print array#####################################################################
//echo (implode(", ", array_keys($arr_tem)));
echo substr(implode(",", array_keys($arr_tem)),0,200);
$keywd = substr(implode(",", array_keys($arr_tem)),0,200);

// Make a MySQL Connection
mysql_connect("localhost", "XXXXXXXXX", "XXXXXXXXX") or die(mysql_error());
mysql_select_db("XXXXXXXXX") or die(mysql_error());


$get = "SELECT * FROM keywords WHERE productid = $productid";
$SQ_query = mysql_query($get) or die("Query failed: $get\n" . mysql_error());
$fetch = mysql_fetch_array($SQ_query);

$id = $fetch['productid'];
$key = $fetch['keywords'];

// This insert works

if (empty($id)) {
mysql_query("insert into keywords 
(productid, keywords) VALUES('$productid','$keywd')");
}

//below does not work - A big mess!!!

else 
(isset($id))

mysql_query("UPDATE keywords SET keywords = "$key" WHERE productid = "$id");
printf ("Updated records: %d\n", mysql_affected_rows());
mysql_query("COMMIT");


endif;

?>

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

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