Page 1 of 1

trying to add +1 to a database column

Posted: Tue Mar 06, 2007 2:31 am
by karfeef
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello all. i have written a script that searches for entries in a database column, and then displays all the values from other columns. specifically, it is an error code search, which returns error code meanings and actions and so on.

what i'm trying to do is to make the search results add 1 into another column for the purpose of reporting to see how many times a code has been searched for.

the code i have is below -

Code: Select all

<?

if (isset($_POST['sumbitted'])) {
 if (!empty($_POST['code'])) {
  $M_Code =escape_data($_POST['code']);
  
 	$query2 ="UPDATE error SET search_num = search_num +1 WHERE code LIKE '%$M_Code%'";
  	$result2 = @mysql_query ($query2);

  $query = "SELECT * from error Where code LIKE '%$M_Code%'";
  $result = @mysql_query ($query);
  if ($result) {
   $num_rows = mysql_num_rows($result);
   if ($num_rows >0) {
        ?>
the mysql query works fine when run in phpmyadmin. The error page itself works fine with the update query added, but it seems as if the update query is just being ignored. can anyone help?

many thanks

Karf


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Mar 06, 2007 2:57 am
by mikeq
couple of things to do

after you assign to variable $query2 print it out

Code: Select all

print $query2;
That will show you what is going to be sent, I am assuming that there is a problem with $M_Code as the query itself looks okay. What data type is the code field?

after your $result2 line put an error check

Code: Select all

if (mysql_errno()){
    print "Error Code: ".mysql_errno()." Message: ".mysql_error();
}
if any error occurred this will print it out. You should be doing this kind of error handling anyway, for obvious reasons

Posted: Tue Mar 06, 2007 2:57 am
by volka
try

Code: Select all

error_reporting(E_ALL); ini_set('display_errors', true); ini_set('mysql.trace_mode', true); // for debugging

if (isset($_POST['sumbitted'])) {
  if (!empty($_POST['code'])) {
    $M_Code=escape_data($_POST['code']);

    $query2 ="UPDATE error SET search_num = search_num +1 WHERE code LIKE '%$M_Code%'";
    echo '<fieldset><legend>Debug</legend>', htmlentities($query2);
    $result2 = mysql_query ($query2) or die(mysql_error().'</fieldset>');
    echo 'UPDATE error, affected rows: ', mysql_affected_rows(), '</fieldset>';

    $query = "SELECT * from error Where code LIKE '%$M_Code%'";
    $result = mysql_query ($query) or die(mysql_error());
    if ($result) {
      $num_rows = mysql_num_rows($result);
      if ($num_rows >0) {