Coly010 Posted August 23, 2013 Share Posted August 23, 2013 I've created a couple of functions in php which are: function new_notification($u_id, $sid, $t, $ot) { $uid = abs((int) $u_id); $s_id = abs((int) $sid); $type = abs((int) $t); $other = abs((int) $ot); switch ($type) { case 1: friend_request($uid, $s_id, $type, $other); break; case 2: new_pm($uid, $s_id, $type, $other); break; case 3: new_chat($uid, $s_id, $type, $other); break; } } function new_pm($u_id, $sid, $t, $ot) { include "mysqli.php"; global $db; $uid = $u_id; $s_id = $sid; $type = $t; $other = $ot; $s_details_sql = $db->query("SELECT * FROM users WHERE userid = '$s_id'"); $s_details = mysqli_fetch_array($s_details_sql); $s_dname = $s_details['display_name']; $s_username = $s_details['username']; $s_dpic = $s_details['display_pic']; $notification_message = "" . $s_dname . " (" . $s_username . ") has sent you a private message!"; $notifcation_nmessage = "<a href='#' onClick='showPMC(". $s_id .",". $other .")'><table class='notification'><tr><td><img src='" . $s_dpic . "' /></td><td>" . $notification_message . "</td></tr></table></a>"; $db->query("INSERT INTO notifications(userid, s_id, type, n_message, n_time) VALUES('$uid', '$s_id', '$type', '$notifcation_nmessage', NOW()"); } as you can probably see once I call the function and and if $type is 2 then it'll cal the new_pm() function. The problem i am having is that $db->query("INSERT INTO notifications(userid, s_id, type, n_message, n_time) VALUES('$uid', '$s_id', '$type', '$notifcation_nmessage', NOW()"); does not insert a row into the database. Anyone have any ideas. Quote Link to comment Share on other sites More sharing options...
Razor42 Posted August 23, 2013 Share Posted August 23, 2013 Your missing a close bracket just after now() Quote Link to comment Share on other sites More sharing options...
Coly010 Posted August 23, 2013 Author Share Posted August 23, 2013 (edited) Although that too was a problem it still will not insert it, I'm calling the new_notification() function here: $pmessage = $db->real_escape_string($pmessage); $fr_id = $db->real_escape_string($fr_id); $c_id = $db->real_escape_string($c_id); if( $pmessage == " ") { } else { if ( $pmessage == "") { } else { $db->query("INSERT INTO p_messages(pmc_id, s_id, r_id, message, time) VALUES('$c_id', '$userid', '$fr_id', '$pmessage', NOW())"); new_notification($fr_id, $userid, 2, $c_id); } } Edited August 23, 2013 by Coly010 Quote Link to comment Share on other sites More sharing options...
Razor42 Posted August 23, 2013 Share Posted August 23, 2013 Try surrounding the now() in quotes like 'now()' Quote Link to comment Share on other sites More sharing options...
Coly010 Posted August 23, 2013 Author Share Posted August 23, 2013 still no joy Quote Link to comment Share on other sites More sharing options...
sniko Posted August 23, 2013 Share Posted August 23, 2013 Run the query in your database interface; i.e phpmyadmin, and see if that gives an error. (Don't put a function in quotes, it will be treated as a string) Quote Link to comment Share on other sites More sharing options...
SRB Posted August 23, 2013 Share Posted August 23, 2013 if( $pmessage == " ") { } else { if ( $pmessage == "") { } else { $db->query("INSERT INTO p_messages(pmc_id, s_id, r_id, message, time) VALUES('$c_id', '$userid', '$fr_id', '$pmessage', NOW())"); new_notification($fr_id, $userid, 2, $c_id); } } if (strlen(str_replace(" ", "", $pmessage)) > 0) { $db->query("INSERT INTO p_messages(pmc_id, s_id, r_id, message, time) VALUES('$c_id', '$userid', '$fr_id', '$pmessage', NOW())"); new_notification($fr_id, $userid, 2, $c_id); } Quote Link to comment Share on other sites More sharing options...
rockwood Posted August 24, 2013 Share Posted August 24, 2013 Try surrounding the now() in quotes like 'now()' try this $db->query("INSERT INTO p_messages(pmc_id, s_id, r_id, message, time) VALUES($c_id,$userid,$fr_id,$pmessage, NOW())"); now() should without quotes bcz it is predefined function for mysql current date & time Quote Link to comment Share on other sites More sharing options...
Dayo Posted August 24, 2013 Share Posted August 24, 2013 how comes you are using a $db class to call the query and then mysqli_fetch_array() to proccess the results? kind of nulls the need for a db class if you are going to mix the two methods. a small function i find usefull is sessionLog() function sessionLog($var) { $_SESSION['log'][] = $var; } //then make a file session.log.php <?php foreach ($_SESSION['log'] as $temp) { echo '<pre>'.print_r($temp, true).'</pre><br />'; } ?> then use the above code to check the query out ie like this function new_pm($u_id, $sid, $t, $ot) { include "mysqli.php"; global $db; $uid = $u_id; $s_id = $sid; $type = $t; $other = $ot; $s_details_sql = $db->query("SELECT * FROM users WHERE userid = '$s_id'"); // i would change this so it only selects the fields you need $s_details = mysqli_fetch_array($s_details_sql); sessionLog($s_details); // see if we are getting details $s_dname = $s_details['display_name']; $s_username = $s_details['username']; $s_dpic = $s_details['display_pic']; $notification_message = "" . $s_dname . " (" . $s_username . ") has sent you a private message!"; $notifcation_nmessage = "<a href='#' onClick='showPMC(". $s_id .",". $other .")'><table class='notification'><tr><td><img src='" . $s_dpic . "' /></td><td>" . $notification_message . "</td></tr></table></a>"; $sql = "INSERT INTO notifications(userid, s_id, type, n_message, n_time) VALUES('$uid', '$s_id', '$type', '$notifcation_nmessage', NOW()"; sessionLog($sql); // now you can run the sql query in phpmyadmin to see whats wrong with it $db->query($sql); } Quote Link to comment Share on other sites More sharing options...
Hedge Posted August 24, 2013 Share Posted August 24, 2013 just a thought, i had a similar problem today. Made a function to to give items to players. Code seemed to be working fine, not throwing any errors, but not inserting new rows. Finally found that at some point id had made the userid column a 'unique index' so of course it wouldn't enter a new row if the userid was previously used. If your code seems to be working fine, maybe have a look at it from the database end and work your way forwards. Quote Link to comment Share on other sites More sharing options...
Alan Posted August 24, 2013 Share Posted August 24, 2013 It is mind-blowing that people cannot debug the most simplest of problems; worse that they write non-defensive code which makes debugging hard. Consider: mysql_query('INSERT INTO table VALUES (1, 2, "three")') vs $sql = 'INSERT INTO TABLE (field1, field2, field3) VALUES (1, 2, "three")'; if (array_key_exists('debug', $_GET)) { echo $sql . "<br>"; } $rs = mysql_query($sql); if ($rs === false) { echo __FILE__ . ':' . __LINE__ . ' -- ' . mysql_error() . '<br>' . $sql; } Not an elegant way of displaying error messages I admit, but it does: a) Define the SQL statement prior to using it - helpful during debugging, especially if you use something like PHP Storm b) Define each field - In case you change the table structure at a later date. c) Displays the SQL statement if debugging - No elegantly, but this is more about defensive programming than style. d) Checks the result of the query e) Displays an error message if the query failed - Again, not pretty, but you get the point. Yes, there is more code to type; but in its defence, if a problem does arise, you will be able to see the exact error message, where it is being output and the actual query that caused the problem. Obviously you would need something more secure than simply passing ?debug to the script, but again that's not the point here. Write your code so that you can easily debug it; any complex variable that gets generated over several lines of code and/or is populated with data from a variety of insecure sources (ie user input) should have something that enables you to quickly show its value *PRIOR* to using it. Quote Link to comment Share on other sites More sharing options...
Coly010 Posted August 24, 2013 Author Share Posted August 24, 2013 Using dayo's sessionLog() function, i was able to view the sql query that is supposed to be inserted into the database. Now following sniko's advice i ran it in the database interface, it works. no problem at all. so i'm assuming the $db isnt working for some reason. because the query is correct and should be inserting the row. Hedge i've checked the indexes and there is only a primary index on notify_id. Quote Link to comment Share on other sites More sharing options...
Coly010 Posted August 24, 2013 Author Share Posted August 24, 2013 I've tried connecting to the database using the procedural style aka: $c = mysqli_connect(); and it still is not working, its like the line mysqli_query($c, $sql); / $db->query($sql); is being ignored completely Quote Link to comment Share on other sites More sharing options...
Coly010 Posted August 24, 2013 Author Share Posted August 24, 2013 With help from what alan has posted i've concluded that it's to do with: $notifcation_nmessage = '<a href="#" onClick="showPMC('. $s_id .','. $other .')"><table class="notification"><tr><td>' . $s_dpic . '</td><td>' . $notification_message . '</td></tr></table></a>'; as when i try: $notifcation_nmessage = "hi there"; it works perfectly, so canyone see what the problem is with my variable? I've tried changing quotes but that hasn't worked, im think perhaps the parentheses could be the problem, but how do i have them ignored as part of a string? Quote Link to comment Share on other sites More sharing options...
Coly010 Posted August 24, 2013 Author Share Posted August 24, 2013 After futtering around with the string value of $notification_nmessage i have found the actual error, by having . $s_dpic . in the query it is causing it to fail. So i'll try changing how that field works throughout the site and in the database and see if i can come up with a conclusion Quote Link to comment Share on other sites More sharing options...
Coly010 Posted August 24, 2013 Author Share Posted August 24, 2013 Problem solved, thanks everyone for all the help, and the debugging tips, they will come in very handy in the very near future more than likely Quote Link to comment Share on other sites More sharing options...
Dayo Posted August 24, 2013 Share Posted August 24, 2013 Problem solved, thanks everyone for all the help, and the debugging tips, they will come in very handy in the very near future more than likely what was the error? may become helpful for others who may have a similar problem down the line Quote Link to comment Share on other sites More sharing options...
Coly010 Posted August 24, 2013 Author Share Posted August 24, 2013 my 'display_pic' column in my database contained the full <img src='' /> tag for the display picture. when i took this out and changed it to just the string of the location of the image and surrounded the ". $s_dpic ." with the image tag, it worked perfectly, on countless tests. Quote Link to comment Share on other sites More sharing options...
rockwood Posted August 24, 2013 Share Posted August 24, 2013 (edited) MCC mysqli db class is very old and not enough so if possible than use AB's mysqli db class (NWE). Edited August 25, 2013 by rockwood Quote Link to comment Share on other sites More sharing options...
SRB Posted August 24, 2013 Share Posted August 24, 2013 it is incomplete waste coded db classes please AB's mysqli Anybody able to translate from idiot to English? Quote Link to comment Share on other sites More sharing options...
Dominion Posted August 24, 2013 Share Posted August 24, 2013 Anybody able to translate from idiot to English? Sure, but like any translations it's probably slightly out. :p The database class used in MCC is rather poor, and I would recommend looking at A-B's mysqli class (used in NWE) if you wish to use one. Quote Link to comment Share on other sites More sharing options...
Guest Posted August 24, 2013 Share Posted August 24, 2013 Anybody able to translate from idiot to English? Google translate didn't work for that :( Quote Link to comment Share on other sites More sharing options...
SRB Posted August 24, 2013 Share Posted August 24, 2013 From: it is incomplete waste coded db classes please AB's mysqli To The database class used in MCC is rather poor, and I would recommend looking at A-B's mysqli class (used in NWE) if you wish to use one. I wonder if you're right. I'm sure we'll see when the reply comes, although I feel like we'll need another translation at that point too, so stay close by Dom :D Google translate didn't work for that :( Shocking, thought they could translate anything! Sure they have wookie too :D Quote Link to comment Share on other sites More sharing options...
rockwood Posted August 25, 2013 Share Posted August 25, 2013 Sure, but like any translations it's probably slightly out. :p yes, i was mean it and sorry bcz after drunk i typed those words . The database class used in MCC is rather poor, and I would recommend looking at A-B's mysqli class (used in NWE) if you wish to use one. Quote Link to comment Share on other sites More sharing options...
Guest Posted August 25, 2013 Share Posted August 25, 2013 yes, i was mean it and sorry bcz after drunk i typed those words . Non-English + "Apparently drunk", you think that posting a post would help huh? That's what I don't get, why after getting drunk do you even have time or even THINK about coming onto a computer, I get drunk and pass out, is that just me or....????? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.