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:
    90
    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">

    <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 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    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 IP
  3. pshaw

    pshaw Greenhorn

    Messages:
    90
    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 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    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 IP
  5. pshaw

    pshaw Greenhorn

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

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #26
    $result = $db->prepare('

    "->" not "=>"
     
    deathshadow, Sep 23, 2022 IP
  7. pshaw

    pshaw Greenhorn

    Messages:
    90
    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 IP
  8. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #28
    Don't tell me you talked me into PDO and quit? I've tried everything but can't succeed.
    <!DOCTYPE><html><head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">

    <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['website'], '</option>';

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

    if (isset($_POST['website'])) {
    $execData = [ ':website' => $_POST['website'] ];
    $result = $db=>prepare(' // Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in C:\xampp\htdocs\home\sellookup.php on line 50
    SELECT website
    WHERE website = :website
    ');
    $result->exec($execData);
    if ($website = $result->fetch()) {
    $update = $db->prepare('
    UPDATE lookuptbl
    SET
    lastused = NOW(),
    count = count + 1
    WHERE website = :website
    LIMIT 1
    ');
    $update->exec($execData);
    echo '
    <p>', (
    $update->rowCount() ?
    'Record updated successfully' :
    'Record not found'
    ), '</p>';
    }
    <!-- $targetURL = htmlspecialchars($website['website']);
    echo '<a href="', $websiteURL, '">go to website ', $websiteURL, '</a>';-->

    }

    ?>
    <a href="<?php echo $website;?>">Go to website</a>
    <!-- <a href="<?php echo $website;?>"><button>go to website</button></a>-->
    <!--<button onclick="window.location.href='<?php echo $website;?>';">go to website</button>-->
    <!--<form action="<?php echo $website;?>" method="post">
    <button>go to website</button></form>-->
    <!--<button onclick="myFunction('<?php echo $website;?>')">go to website</button>-->
    </body></html>
     
    pshaw, Sep 27, 2022 IP
  9. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #29
    Ok, I went back and realized what was suggested - I think? There are no errors!


    following are screenshots of what this code produces:
    scr5 b4 selection - note account already displayed
    can't get screenshot of options
    scr6 after selection
    scr7 upon submission
    there is no go to website scr5.png
    <!DOCTYPE><html><head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">

    <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['website'], '</option>';

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

    if (isset($_POST['website'])) {
    $execData = [ ':website' => $_POST['website'] ];
    $result = $db->prepare('
    SELECT target
    WHERE target = :target
    ');
    $result->exec($execData);
    if ($website = $result->fetch()) {
    $update = $db->prepare('

    UPDATE lookuptbl
    SET
    lastused = NOW(),
    count = count + 1
    WHERE website = :website
    LIMIT 1
    ');
    $update->exec($execData);
    echo '
    <p>', (
    $update->rowCount() ?
    'Record updated successfully' :
    'Record not found'
    ), '</p>';
    }
    $targetURL = htmlspecialchars($website['website']);
    echo '<a href="', $websiteURL, '">go to website ', $websiteURL, '</a>';

    }

    ?>

    </body></html>
     

    Attached Files:

    pshaw, Sep 28, 2022 IP