INSERT and UPDATE differences

Discussion in 'PHP' started by tyler_durden, Apr 3, 2007.

  1. #1
    Again, i'm new to php and modifying some open source scripts, and I have another issue. I can't figure out why this code below that originally posted a record, doesn't work when modified to edit the record instead.

    The INSERT version that works
    @mysql_query("	INSERT INTO reminders
    				(date_field,email,comment,recur_num,
    				 recur_val,adv_notice,notify_num,notify_val,
    				 recurring,remid,subject,usrname)
    			VALUES
    				('$query','$my->email','$comment','$recur_num',
    				 '$recur_val','$adv_notice',
    				 '$notify_num','$notify_val','$recurring','$remid','$subject',
                                     '$my->username')
    				");
    Code (markup):
    The update version that doesn't work - I do want to edit by the different field remid.
    @mysql_query(" UPDATE reminders SET date_field='$query',email='$my->email',comment='$comment',recur_num='$recur_num',recur_val='$recur_val',adv_notice='$adv_notice',
    notify_num='$notify_num',notify_val='$notify_val',recurring='$recurring',remid='$remid',subject='$subject',
    usrname='$my->username' WHERE remid='$remid'");
    Code (markup):

    The whole page if the above code is not the issue
    extract($_POST);
    extract($_GET);
    
    $remid = $_POST['remid'];
    
    
    
    
    $result=mysql_query("	SELECT *
    			FROM passwd
    			WHERE email='$email'");
    
    while ($row=mysql_fetch_array($result)){
    	$ckpasswd=$row['epasswd'];
    	$blocked=$row['eblocked'];
    }
    
    $edate=date("M d Y, h:ia ").$dbtimezone;
    
    
    if (  ( 
           ((USE_EREMINDERS_AUTHENTICATION) && ($ckpasswd=="")) 
           || ( (USE_APACHE_AUTHENTICATION) && ($result==0)) 
          )
        && ($email != ''))
    {
    	
    
    	// Seed random generator
    	srand((double)microtime()*1000000);
    
    	for ($i=1; $i<=$passlength; $i++){
    		$epasswd=$epasswd.chr(rand(97,122));
    	}
    
    if (RESTRICT_NEW_USER_REGISTRATION) {
    } else if (PERMIT_NEW_USER_REGISTRATION) {
    	@mysql_query("	INSERT INTO passwd (email,epasswd,eip,edate)
    			VALUES ('$email','$epasswd','$REMOTE_ADDR','$edate')");
    }
    	$ckpasswd=$epasswd;
    
    	$message = $ins_req. " ".$REMOTE_ADDR." ".$ins_made."\n";
    	$message .= $ins_add."\n";
    	$message .= $ins_at." ".$site.".  \n\n";
    	$message .= $ins_log." ".$epasswd;
    	$message .= "\n\n".$ins_msg1.": \n".$site.".  \n\n";
    	$message .= "\n".$ins_msg2."\n";
    	$message .= $ins_msg3."\n";
    	$message .= $ins_msg4."\n";
    
    if (RESTRICT_NEW_USER_REGISTRATION) {
    } else if (PERMIT_NEW_USER_REGISTRATION) {
    	mail ($email, $ins_rem, $message,"From: $from");
    }
    
    	msg_box($ins_new,
    	$ins_set."<P>".$ins_sent." ".$email.$ins_back1." <b>".$ins_back2."</b> ".$ins_back3."<P>".$ins_tnx,"black");
    
    	// Yes, this exit in the middle is not optimal, but it works.
    	echo "</BODY></HTML>";
    	exit;
    }
    
    
    if ($ckpasswd==$epasswd)
    {
    		@mysql_query("	UPDATE passwd
    			SET edate='$edate'
    			WHERE email='$email'");
    	
    	@mysql_query("	UPDATE passwd
    			SET eip='$REMOTE_ADDR'
    			WHERE email='$email'");
    
    	
            if ($hour == "12") {$hour="00";}
            if ($ampm == "pm") {$hour=$hour+12;}
    
    	$query=$year.$month.$cal_day."03"."00"."00";
    	$remid=uniqid("");
    
    	$shortcomment=$comment;
    
    	/* Show the time THEY set the event for, in THEIR timezone */
    	if ($adv_notice == "yes"){
    		$comment=$month."-".$cal_day."-".$year." at ".$hour.":".$minute." GMT".($localzone>=0?'+':'').$localzone."...\n\n".$comment;
    	}
    
    
    @mysql_query(" UPDATE reminders SET date_field='$query',email='$my->email',comment='$comment',recur_num='$recur_num',recur_val='$recur_val',adv_notice='$adv_notice',notify_num='$notify_num',notify_val='$notify_val',recurring='$recurring',remid='$remid',subject='$subject',usrname='$my->username' WHERE remid='$remid'");
    
    
    
    		if ($adv_notice=="yes") {
    			$result=mysql_query("	SELECT *
                   		                   	FROM reminders
                                      		WHERE remid='$remid'");
     			$row=mysql_fetch_array($result);
    			$interval=$row['notify_num'];
    
    	                switch ($row['notify_val']) {
                            case "day"      :
                                    $interval=$interval*86400;
                                    break;
                            case "minute"      :
                                    $interval=$interval*60;
                                    break;
                            case "hour"     :
                                    $interval=$interval*3600;
                                    break;
                            case "month"    :
    				$interval=$interval*2592000;
                                    break;
                            case "year"     :
                                    $interval=$interval*31536000;
                                    break;
    					}
    			$result=mysql_query("
    					SELECT unix_timestamp(date_field)
                            	        FROM reminders
                                    	WHERE remid='$remid'
    			");
    
                    	$row=mysql_fetch_array($result);
    			$new_time=$row['unix_timestamp(date_field)']-$interval;
    
                    	@mysql_query("  UPDATE reminders
    					SET date_field=FROM_UNIXTIME($new_time)
                                            WHERE remid='$remid'");
    	}
    
    	
    	$result=mysql_query("
    		SELECT unix_timestamp(date_field)
                    FROM reminders
                    WHERE remid='$remid'
    	");
    
            $row=mysql_fetch_array($result);
    	$new_time=$row['unix_timestamp(date_field)']-(($localzone-$dbtzoffset)*3600);
    
            @mysql_query("  UPDATE reminders
    			SET date_field=FROM_UNIXTIME($new_time)
                            WHERE remid='$remid'");
    Code (markup):

     
    tyler_durden, Apr 3, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    You're not letting MySQL tell you what the problem is. Take the @ out infront of your query, and add OR die( mysql_error() ); at the end of your query.

    
    
    mysql_query("UPDATE [....]") OR die( mysql_error() );
    
    PHP:
     
    nico_swd, Apr 4, 2007 IP
  3. ZYSeo

    ZYSeo Peon

    Messages:
    164
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Take away @ from mysql_query() - is hides all error messages.

    Try this code:

    
    $result = mysql_query(...);
    
    if ($result == false) {
    	echo mysql_error();
    }
    
    PHP:
    it will prints you complete error message, even pointing part of query where error is.
     
    ZYSeo, Apr 4, 2007 IP
  4. Rasczak

    Rasczak Peon

    Messages:
    131
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Also ECHOing the query before executing it helps a lot!

    Try:

    $query="INSERT INTO ......";
    echo $query;
    $result=mysql_query($query);

    etc...
     
    Rasczak, Apr 4, 2007 IP
  5. tyler_durden

    tyler_durden Peon

    Messages:
    340
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Interesting - I made the changes (I haven't tried Rasczak's yet), and I have just a black screen with nothing displayed.
     
    tyler_durden, Apr 4, 2007 IP
  6. kashem

    kashem Banned

    Messages:
    1,250
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Check the where clause , whethe the right value you are passing to compare
     
    kashem, Apr 4, 2007 IP
  7. tyler_durden

    tyler_durden Peon

    Messages:
    340
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I can echo the value that I am using for the where clause correctly.

    I am now getting this error. Shouldn't the function name be mentioned in that error line? The function is $mysql_query, correct?
    Fatal error: Call to undefined function: () in (filename) on line <b>105

    Here is the code for that line.
    $mysql_query(" UPDATE reminders SET date_field='$query',email='$my->email',comment='$comment',recur_num='$recur_num',recur_val='$recur_val',adv_notice='$adv_notice',notify_num='$notify_num',notify_val='$notify_val',recurring='$recurring',remid='$remid',subject='$subject',usrname='$my->username' WHERE remid='$remid'") OR die( mysql_error() );
    Code (markup):
     
    tyler_durden, Apr 4, 2007 IP
  8. Rasczak

    Rasczak Peon

    Messages:
    131
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    the sql function can NOT start with a "$" - its a function, not a variable! :)
     
    Rasczak, Apr 10, 2007 IP
  9. jrrl

    jrrl Peon

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Well, technically, you COULD have a variable called $mysql_query that is bound to the function of the same name and then it would work.

    But that would be gross.

    <?php
    
    $sin = sin;
    echo "the sin of 45 is " . $sin(45);
    
    // and to prove it isn't a weird trick due to the same name...
    
    $foo = sin;
    echo "the sin of 45 is (still) " . $foo(45);
    
    ?>
    
    Code (markup):
    -John.
     
    jrrl, Apr 10, 2007 IP
  10. Rasczak

    Rasczak Peon

    Messages:
    131
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    i got your point... anyway (for tyler) - i'm talking about something like this:

    $query = "SELECT foo FROM table WHERE 1 ORDER BY foo";
    echo $query;
    $result = mysql_query($query);


    if you would operate with

    mysql_query("SELECT foo FROM table WHERE 1 ORDER BY foo");

    you wouldn't be able to debug anything, is that right?
     
    Rasczak, Apr 11, 2007 IP