skip to Main Content

Here is my code

if (isset($_GET['ref'])) {
  $ref_id= intval($_GET['ref']); // Sanitize input

  // Query to get the project info
  $projects = $wpdb->get_results($wpdb->prepare("SELECT * FROM table WHERE project_ref = %d", $ref_id));

‘ref’ is passed from previous page
However, the results are from the first line of table – even if I hard code the $ref_id.

Here is code from previous page.

.<a href="../projects?ref=<?php echo $ref; ?>"/><?php echo $_SESSION['project_name'];?>

Each link shows the correct $ref_id

Crazy. I have used similar coding on another page and it works but cannot see why this isn’t.

Hope you can help

2

Answers


  1. Chosen as BEST ANSWER

    Thanks everyone for their assistance. I changed the WHERE query to project id and all is good now.

    if (isset($_GET['project_id'])) {
    $id= intval($_GET['project_id']); // Sanitize input
    
    // Query to get the projects info
    
    $projects = $wpdb->get_results($wpdb->prepare("SELECT * FROM 'table WHERE project_ind  = %d", $id));
    

  2. I reformatted a few things for you and made some adjustments, I’ve commented my changes, haven’t tested this because I don’t have access to your DB.

    if( isset( $_GET['ref'] ) ) :
        //Let's make sure you're getting the right value from the $_GET
        var_dump( $_GET['ref'] );
        //Try using (int) instead of intval().
        $ref_id = (int)$_GET['ref']; // Sanitize input
        //Query to get the project info
        //Put your table into a variable for the prepare statement, I forget why but you then wrap it in curly brackets
        $table = $wpdb->prefix . 'table_name';
        $projects = $wpdb->get_results( $wpdb->prepare(
            "SELECT * FROM {$table} WHERE `project_ref` = %d",
            $ref_id
        ) );
        //Dumping the results to you can see what's being retrieved.
        var_dump( $projects );
    endif;
    

    Also, make sure you’re supposed to use $_GET instead of $_POST for this? If the REF from the URL parameter is wrong, try $_POST instead.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search