Jump to content
MakeWebGames

PHP Functions and Mysqli INSERT INTO problem


Coly010

Recommended Posts

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.

Link to comment
Share on other sites

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 by Coly010
Link to comment
Share on other sites

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);   
}
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);
}
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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....?????

Link to comment
Share on other sites

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.   Paste as plain text instead

  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.

×
×
  • Create New...