1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how to use on-click

Discussion in 'PHP' started by pshaw, Sep 11, 2022.

  1. pshaw

    pshaw Greenhorn

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #21
    Wow pdo, I must admit the code is impressive. I can't understand the error message with the variable
    expectations?

    <!DOCTYPE><html><head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    SEMrush
    <link rel="stylesheet" href="screen.css" media="screen">

    <title>lookup menu</title>

    </head><body>

    <?php

    try {

    $db = new PDO(
    'mysql:dbname=homedb;host=localhost',
    'root', // username
    '' // password
    );

    } catch (PDOException $e) {
    die('Error!: ' . $e->getMessage());
    }

    echo '
    <form method="post" action="">
    <fieldset>
    <label>
    Account:<br>
    <select>';

    $result = $db->query(`
    SELECT *
    FROM lookuptbl
    ');

    while ($row = $result->fetch()) echo '
    <option>', $row['target'], '</option>'; // line 39

    echo '
    </select>
    </label><br>
    <button>Submit</button>
    </fieldset>
    </form>';

    if (isset($_POST['target'])) {
    $execData = [ ':target' => $_POST['target'] ];
    $result = $db=>prepare(`
    SELECT target
    WHERE target = :target
    `);
    $result->exec($execData);
    if ($target = $result->fetch()) {
    $update = $db->prepare('
    UPDATE lookuptbl
    SET
    lastused = NOW(),
    count = count + 1
    WHERE target = :target
    LIMIT 1
    ');
    $update->exec($execData);
    echo '
    <p>', (
    $update->rowCount() ?
    'Record updated successfully' :
    'Record not found'
    ), '</p>';
    }
    $targetURL = htmlspecialchars($target['target']);
    echo '<a href="', $targetURL, '">Open URL ', $targetURL, '</a>';

    }

    ?>

    </body></html>
    ===========================
    Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE),
    expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or
    number (T_NUM_STRING) in C:\xampp\htdocs\home\lookupselup.php on line 39
     
    pshaw, Sep 20, 2022 at 12:33 PM IP
    SEMrush
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,646
    Likes Received:
    1,976
    Best Answers:
    250
    Trophy Points:
    515
    #22
    Oops, my bad. I've been writing too much node.js / JavaScript templating code and not PHP, I accidentally put in a backtick that should be a normal single quote.

    Line 33 should be:

    $result = $db->query('

    Not

    $result = $db->query(`

    Lines 50-53 should also be:

    
    	$result = $db=>prepare('
    		SELECT target
    		WHERE target = :target
    	');
    
    Code (markup):
    I've edited my original code post to reflect that. Pain in the ass when you switch between languages, muscle memory starts kicking in.

    I'll try to double check the code once I'm at my workstation, was a drive-by on the laptop.
     
    deathshadow, Sep 20, 2022 at 2:28 PM IP
  3. pshaw

    pshaw Greenhorn

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #23
    Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) on line 50?
     
    pshaw, Sep 20, 2022 at 5:03 PM IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,646
    Likes Received:
    1,976
    Best Answers:
    250
    Trophy Points:
    515
    #24
    Oops, wow, I've really been away from PHP for too long. Should be -> not =>

    Total egg on face.
     
    deathshadow, Sep 22, 2022 at 3:16 PM IP
  5. pshaw

    pshaw Greenhorn

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #25
    $result = $db not=>prepare(' ?
     
    pshaw, Sep 22, 2022 at 5:46 PM IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,646
    Likes Received:
    1,976
    Best Answers:
    250
    Trophy Points:
    515
    #26
    $result = $db->prepare('

    "->" not "=>"
     
    deathshadow, Sep 23, 2022 at 8:20 AM IP
  7. pshaw

    pshaw Greenhorn

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #27
    Parse error: syntax error, unexpected '$result' (T_VARIABLE), expecting ']' in C:\xampp\htdocs\home\sellookup.php on line 50
     
    pshaw, Sep 23, 2022 at 9:58 AM IP