Jump to content
SkyAuburn

Pull data to page based on id

Recommended Posts

This is the link to pull the page

http://127.0.0.1/kudos/?viewkudoid=###

This is the kudo-function.php page

function get_kudo_info($viewkudoid){
	global $wpdb;
    $tablename=acikudos_table_name();
    $viewsql = "SELECT * FROM $tablename WHERE kudoid='$viewkudoid'";
	$viewresult = $wpdb->get_results($viewsql);
    #$vewrow = mysql_fetch_assoc($viewresult);

    return $viewresult;
}

This is the kudo-view.php page

function show_kudos($viewkudoid)
{
   global $wpdb;
   
   $viewkudoid = $_GET['viewkudoid'];
   
   $getthekudo = get_kudo_info($viewkudoid);

    if ($viewkudoid == $getthekudo['kudoid']) {
        
		echo 'Congratulations, you have successfully fixed it!';
    } else {
    echo 'Everything is displayed';
}
	} 

 add_shortcode( 'kudosview', 'show_kudos' );

I know the issue is right in front of me but I can not find it.

Share this post


Link to post
Share on other sites
2 minutes ago, SkyAuburn said:

I know the issue is right in front of me but I can not find it.

Then you know a lot more than we do.

What is the issue?

Share this post


Link to post
Share on other sites
Posted (edited)

Correction.  I get only everything is displayed instead of the you fixed it...lol

Edited by SkyAuburn

Share this post


Link to post
Share on other sites

It looks like

$viewkudoid contains an id.
$getthekudo contains a resultset returned by your function, not an array

So if ($viewkudoid == viewkudoid['kudoid'] ) will always return false.

However this is only guesswork as I have no way of knowing what your methods return.

Share this post


Link to post
Share on other sites

P.S. That is if the method returns anything at all as you are using an obsolete mysql library that has been removed from PHP. Change to using PDO.

Share this post


Link to post
Share on other sites

Thanks Barand I will check that out.  What I am trying to do is when somebody wants to view an entry ie:  viewkudoid  it will display the info for that ID.  but if somebody wants to view all of entries it will show all and and not just the single id

Share this post


Link to post
Share on other sites

Barand is right about the equality condition. $wpdb->get_results() returns an array, object, or null, so comparing any of those to an integer (or whatever is passed through $_GET['viewkudoid']) will fail.

Beyond that, you're ... I'm sorry, but you're doing it wrong. If you're not going to use get_posts() or one of the internally-escaped functions to get the data you're looking for, at least use $wpdb->prepare() so you get something that might look like a prepared statement if you squint at it long and hard enough. Beyond that, if the plugin you're using has a function to return the database table name, there's a decent possibility there's a function to directly retrieve records safely through the plugin's API. Use that instead of directly injecting a $_GET variable into an unsanitized query string.

Other than that, WordPress offers the query_vars and rewrite_rules_array hooks to handle routing, instead of using the kludgy '?{var}={val}' $_GET pattern; admittedly I'll cut a good amount of slack here as that is rather advanced and can be a bit touchy in it's own right. However, one thing that I kinda can't cut slack on is that if you're creating a shortcode, use shortcode_atts() - $_GET may not be set at all, and even if it is it shouldn't matter to the shortcode. Shortcode gets parsed inside the user content, regardless of the page the user is on.

Share this post


Link to post
Share on other sites

Hi maxxd, after pulling everything apart and doing a LOT more research after Barands post things are starting to fall in to place. A little messy at the moment but starting to get the results I am looking for.  Been coming to this site since 2005 (SkyRanger account that I am finally getting back) and you guys have been awesome for your support.

Share this post


Link to post
Share on other sites

I hope my post didn't come across too negative or ... jerky. There are so many ways that things can be done poorly in WordPress it's sometimes hard not to do things wrong. As you keep working with the code, post it here and ask any questions you may have. Let us know what plugin you're dealing with - somebody here may have personal experience with it or the time to do some digging into it, thereby offering better or more specific advice.

Share this post


Link to post
Share on other sites

No maxxd not at all.  I am currently creating a kudos plugin for our company this will allow all of the tech agents to see kudos posted on our intranet site that is posted by our supervisors.  I will definitely be posting with problems that I run in to.

Share this post


Link to post
Share on other sites

Not starting off good...lol

global $wpdb;
$tablename = acikudos_table_name();
$sql = $wpdb->prepare( "SELECT * FROM %s ORDER BY kudoentry desc", $tablename);
$results = $wpdb->get_results( $sql , ARRAY_A );

#var_dump($results);
foreach ($results as $result) { 
     echo '<p>' .$result->kudoagent. '</p>';
}

}

result



WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''wpl2_acikudos' ORDER BY kudoentry' at line 1]
SELECT * FROM 'wpl2_acikudos' ORDER BY kudoentry

 

Share this post


Link to post
Share on other sites

keep looking over code but not seeing where the error would be.  The syntax looks correct.

Share this post


Link to post
Share on other sites
1 hour ago, SkyAuburn said:

( "SELECT * FROM %s ORDER BY kudoentry desc", $tablename)

The above is the syntax for sprintf(), not prepare().

Secondly, since the query contains no user provided data parameters, a prepare is not required. (Note you cannot pass sql identifiers as parameters to a prepared statement)

Share this post


Link to post
Share on other sites

Thanks Barand doing a lot of research and self learning this method, thanks for your feedback.

Share this post


Link to post
Share on other sites

Why is your table name variable? Do you have more than one? If so, why?

Share this post


Link to post
Share on other sites

I have 2 tables one called ackikudos  and another ackikudos_setting  I am trying to pull the data from acikudos for now as the settings will be pulled for another script on the same page later (creating a table based on acikudos data and admin will be able to set number of columns through admin panel)

 

 

Share this post


Link to post
Share on other sites

This is still me but thanks to Gizmola I got my account back.

Well on a good note I am a little farther ahead. Thanks Guys got a working part ver of 1/100 of my program...lol

function show_kudos() {

global $wpdb;
$tablename = acikudos_table_name();

$results = $wpdb->get_results( "SELECT * FROM $tablename order by kudoentry desc"); // Query to fetch data from database table and storing in $results
if(!empty($results))           

  {     
    foreach($results as $row){   
  
    echo $row->kudoagent;

    }
  }

}

 

Share this post


Link to post
Share on other sites
Posted (edited)
7 hours ago, Barand said:

The above is the syntax for sprintf(), not prepare().

Secondly, since the query contains no user provided data parameters, a prepare is not required. (Note you cannot pass sql identifiers as parameters to a prepared statement)

Prepared statements in WordPress are sprintf() statements.

@SkyRanger - what exactly does the acikudos_table_name() method return? If it's the name of the table, wouldn't you just use that directly as I assume you know the name of the table you've created. If the database table prefix is dynamic you could always use "{$wpdb->prefix}table_name".

Also, is there a reason not to create the kudos as a custom post type? It seems like it would alleviate a lot of the problems you're running into (you'd be selecting directly from $wpdb->posts, for instance), however as I don't know the business logic I don't want to suggest you burn a bunch of time on something that won't work for you anyway.

Edited by maxxd
forgot words.

Share this post


Link to post
Share on other sites

Hey maxxd.  The plugin is actually going to be used for a couple of companies where they would like to keep the posts separate. Yeah I know it would save a lot of headaches.

The acikudos_table_name() is for now a function:

function acikudos_table_name(){
  global $table_prefix, $wpdb;
  $table_name = $wpdb->prefix . 'acikudos';
  return $table_name;
}

The table name is going to be changed probably within the week (once i get around to change it...lol)  There is also going to be other functions included in the kudos program that is going to be k i s s for the companies that will be using it.

Share this post


Link to post
Share on other sites

New brain headache for myself. 

When I use this: http://127.0.0.1/kudos/?viewkudoid=   or this http://127.0.0.1/kudos/?viewkudoid=xxx The code works.  I know I am missing a piece of code somewhere, done lots of searching to see how to go about doing it and was looking for a push from somebody to point me in the right direction ie with a link to an example if possible.  Problem is when i use this: http://127.0.0.1/kudos/ i get an error.

Working code with first 2 urls:

  $viewkudoid = $_GET['viewkudoid'];

   $tablename=acikudos_table_name();
   
   $showkudos = $wpdb->get_results( "SELECT * FROM $tablename WHERE kudoid = '$viewkudoid'");

    $showkudosall = $wpdb->get_results( "SELECT * FROM $tablename order by kudoentry desc"); // Query to fetch data from database table and storing in $showkudosall

 
   if ( $showkudos )
{
	foreach ( $showkudos as $kudo )
	{
 
		echo $kudo->kudoagent;

	}	
}
elseif ( $showkudosall )
{
    foreach ( $showkudosall as $allkudo)
    {
		echo "<blockquote class=\"otro-blockquote\">";
		echo $allkudo->kudomsg;
		echo "<span>";
		echo "<b>Kudos for:</b> " .$allkudo->kudoagent. ", " .$allkudo->kudoloc;
  		echo "<br>By: ".$allkudo->kudocust. ", " . date("F j, Y g:i a", strtotime($allkudo->kudoentry));;
		echo "</span></blockquote>"; 
        echo "<hr>";

	}
} else {
	  
	    echo "No Kudos In Database";
}

What I am getting with the last url without the ?viewkudosid-

Notice: Undefined index: viewkudoid in C:\Program Files (x86)\Ampps\www\wp-content\plugins\advantage-kudos\include\acikudos-view.php on line 6

Line 6:     $viewkudoid = $_GET['viewkudoid'];

 

Share this post


Link to post
Share on other sites

Getting closed got everything working but the $viewkudosid=(blank) getting blank page with no errors.  Trying to figure out where to have it echo No Kudos in Database

 $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  
  $url = $actual_link;
if (strpos($url, "?viewkudoid")!==false){
    $viewkudoid = $_GET['viewkudoid'];
     $tablename=acikudos_table_name();
    $showkudos = $wpdb->get_results( "SELECT * FROM $tablename WHERE kudoid = '$viewkudoid'");
    
   foreach ( $showkudos as $kudo )
	{ 
    
    echo "<blockquote class=\"otro-blockquote\">";
		echo $kudo->kudomsg;
		echo "<span>";
		echo "<b>Kudos for:</b> " .$kudo->kudoagent. ", " .$kudo->kudoloc;
  		echo "<br>By: ".$kudo->kudocust. ", " . date("F j, Y g:i a", strtotime($kudo->kudoentry));;
		echo "</span></blockquote>";
		} 
    
}
else {
	 $tablename=acikudos_table_name();
   $showkudosall = $wpdb->get_results( "SELECT * FROM $tablename order by kudoentry desc"); 
   
 foreach ( $showkudosall as $allkudo)
    {
		echo "<blockquote class=\"otro-blockquote\">";
		echo $allkudo->kudomsg;
		echo "<span>";
		echo "<b>Kudos for:</b> " .$allkudo->kudoagent. ", " .$allkudo->kudoloc;
  		echo "<br>By: ".$allkudo->kudocust. ", " . date("F j, Y g:i a", strtotime($allkudo->kudoentry));;
		echo "</span></blockquote>"; 
        echo "<hr>";

	}  
   
}
}

 

Share this post


Link to post
Share on other sites

Here is the issue I am having, as you see with the above code I got part of what I need done. What the problem I am having now is:

if $url with ?viewkudoid=XX - This works to show the kudo with the id of whatever
if $url without the $?viewkudoid - This works to show all the kudos
if $url with ?viewkudoid=  (without the xx) - This shows a blank page without even a page title (wordpress)

I have part 1 and 2 working, now sure how to implement 3. Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
Posted (edited)

It's because you're making things hard on yourself. You're constructing a full URL (for no reason other than to assign it immediately to another variable) only in order to check that a $_GET variable marker exists without actually checking to see if the corresponding $_GET variable value exists. You then try to access the value of the $_GET variable without checking to see if it's actually set, hence the error you would be getting if you had error reporting turned on.

All of this:

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  
$url = $actual_link;
if (strpos($url, "?viewkudoid")!==false){
	$viewkudoid = $_GET['viewkudoid'];
	/*	... stuff ... */
}

should be replaced with this:

if(!empty($_GET['viewkudoid'])){
	/* ... stuff ... */
}

What you're doing now by checking to see if the string `?viewkudoid` is in the URL will technically handle scenario 2 and most of scenario 3 - the problem is that you're assuming there's a value assigned to viewkudoid in the URL. Which you can't. Besides which, everything after the question mark in a URL is the $_GET array. So ditch the homespun variable name existence check and make sure the value exists. Then you'll be taking care of scenarios 2 and 3 - scenario 3 will follow the else clause and print out all kudos records.

And turn on error reporting while you're developing. You should have it turned on in your php.ini on the development/testing server, but you can enable it on a per-site basis. With WordPress you've got 2 ways to turn on error reporting: you can go the traditional PHP way and add this to the top of your script file:

error_reporting(-1);
ini_set('display_errors', true);

or you can go the WordPress way and add this to your wp-config.php file:

define('WP_DEBUG', true);
define('WP_DEBUG_DISPLAY', true);

For more information, check here. I will say that in my experience the WordPress way isn't bulletproof, and I'd highly recommend using the PHP ini_set() and error_reporting() functions if you can't turn on error reporting at the server via the php.ini on your development/testing machine for whatever reason.

Edited by maxxd
wrong word

Share this post


Link to post
Share on other sites

Honestly maxxd never even thought of going that route. Thank you that fixed the problem.   ref the error reporting.  the wp_debug is on which is allowing me to catch most errors that are happening (which is lots..lol) but some are still slipping through.  I might just go back to the error_reporting as I am use to that.  This is my first time making a plugin for wordpress so still learning, thank you for your help.

Share this post


Link to post
Share on other sites

This is off the original topic I started but I am running in to a new issue.  "Update"  I can post and delete but having a problem updating. I am getting no errors showing just white screen.  I have verified that everything is posting but the database is not updating for some reason. Not sure if it is late and I am not seeing straight or I actually messed something up that I can not see yes I probably could have put the _POST in the array but they are outside for testing purposes.:

 

if (!empty($_POST)) {
        global $wpdb;
            $table = $tablename;
            
                $kudomsg = $_POST['kudomsg'];
                $kudoagent = $_POST['kudoagent'];
                $kudoagid = $_POST['kudoagentid'];
                $kudocust = $_POST['kudocust'];
                $kudoacct = $_POST['kudoacct'];
                $kudoclient = $_POST['kudoclient'];
                $kudoloc = $_POST['agentloc'];

            $data = array(
                'kudomsg' => $kudomsg,
                'kudoagent' => $kudoagent,
                'kudoagid' => $kudoagid,
                'kudocust' => $kudocust,
                'kudoacct' => $kudoacct,
                'kudoclient' => $kudoclient,
                'kudoloc' => $kudoloc
            );
 
            $where = array('kudoid' => $_POST['kudoid']);             

            $success=$wpdb->update($table , $data, $where);
            if($success){
            
        echo "Kudos has been updated.  Go to Kudos page to view";
        echo "<br /><a href='" .$kudourl."'>Close</a>";   
} } else {
 
  $kudoacct = $_GET['acct'];
     $tablename=acikudos_table_name();
     $editkudos = $wpdb->get_results( "SELECT * FROM $tablename WHERE kudoacct = '$kudoacct'");
    
   foreach ( $editkudos as $editkudo )
	{ 
?> 
<form name="acikudos_form" id="updatekudos" method="post" action="" enctype="multipart/form-data">
	<table>
	<tr>
		<td>Agent Name</td>
		<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
		<td><input name="kudoagent" type="text" value="<?php echo $editkudo->kudoagent; ?>" /></td>
	</tr>
	<tr>
		<td>Agent ID</td>
		<td></td>
		<td><input name="kudoagentid" type="text" value="<?php echo $editkudo->kudoagid; ?>" /></td>
	</tr>
	<tr>
		<td>Agent Location</td>
		<td></td>
		<td><select name="agentloc" size="1">
	<option value="<?php echo $editkudo->kudoloc; ?>"><?php echo $editkudo->kudoloc; ?></option>
	<option value="Amherst Center">Amherst Center</option>
	<option value="Charlottetown Center">Charlottetown Center</option>
	<option value="Kingston Center">Kingston Center</option>
	</select></td>
	</tr>
	<tr>
		<td>Agent Queue</td>
		<td></td>
		<td><select name="kudoclient" size="1">
	<option value="<?php echo $editkudo->kudoclient; ?>"><?php echo $editkudo->kudoclient; ?>"</option>
	<option value="AR">AR</option>
	<option value="Eastlink Tech Support">Eastlink Tech Support</option>
	<option value="PNI">PNI</option>
	</select></td>
	</tr>
	<tr>
		<td></td>
		<td></td>
		<td></td>
	</tr>
		<tr>
		<td>Customer Name</td>
		<td></td>
		<td><input name="kudocust" type="text" value="<?php echo $editkudo->kudocust; ?>"/></td>
	</tr>
		<tr>
		<td>Customer Account #</td>
		<td></td>
		<td><input name="kudoacct" type="text" value=" <?php echo $kudoacct; ?>" /></td>
	</tr>
		<tr>
		<td>Kudos</td>
		<td></td>
		<td><textarea cols="50" name="kudomsg" rows="5"><?php echo $editkudo->kudomsg; ?></textarea></td>
	</tr>
		<tr>
		<td><input type="hidden" value="<?php echo $editkudo->kudoid; ?>" name="kudoid"></td>
		<td></td>
		<td></td>
	</tr>
		<tr>
		<td><button type="submit" value="sendkudos" class="aciformbutton" name="sendkudos" />Update Kudos</button>
		<td></td>
		<td><button type="reset" value="reset" class="aciformbutton">Start Over</button></td>
	</tr>

</table>

</form>

This is a list of my columns

kudoid  kudomsg  kudoagent  kudoagid  kudocust  kudoacct  kudoclient  kudoloc  kudoentry  kudoadmin kudopic kudostatus

 

Share this post


Link to post
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.   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.


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