Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP/MySQL Question

    I've begun to develop a script using JavaScript/MySQL/PHP. The basic idea is when a user clicks a link a new window opens with relevant information..

    On "index.php" there are links formatted as so:

    Code:
    <a href="javascript:box('popup.php?id=1');">Link A</a><br>
    The script for this is:

    Code:
    <script>
    var newwindow;
    function box(url)
    {
    	newwindow=window.open(url,'Info Box','height=400,width=200');
    	if (window.focus) {newwindow.focus()}
    }
    </script>
    That works. Now onto what doesn't work..

    "popup.php" would then take the "?id="s part of the link and use the value to select a row from the mysql table...This is the code I have so far (with help from WebmasterWorld.com)

    Code:
    <?php
    
    $dbhost='localhost';
    $dbusername='xxx';
    $dbuserpass='xxx';
    $dbname='info';
    
    mysql_connect ($dbhost, $dbusername, $dbuserpass);
    
    if (!mysql_select_db($dbname)) die(mysql_error());
    
    $sql = "SELECT * FROM info WHERE id=".mysql_escape_string($_GET["id"])";
    $result = mysql_query($query) or die('Error, query failed');
    while ($row = mysql_fetch_array($result)) {
    echo "<table border='5'>
    <tr>
    <td>
    '.$row['name'].'
    </td>
    </tr>
    <tr>
    <td>
    '.$row['description'].'
    </td>
    </tr>
    </table>";
    }
    ?>
    When you click a link, the box pops up...But it displays nothing and when you go to 'View Page Source' nothing is there.

    This is my first script I've tried to create on my own so I'm kind of lost..Here's some info on my server..

    PHP = 4.40
    Apache = 2.0.54
    MySQL = 4.1.14
    OS = Windows Server 2003

    If any other info is needed just ask.

    Regards,
    Ken

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Location
    Brasília, Brazil
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What if you delete the last double quote?
    PHP Code:
    $sql "SELECT * FROM info WHERE id=".mysql_escape_string($_GET["id"]); 
    If it does not work echo the sql string:
    PHP Code:
    $sql "SELECT * FROM info WHERE id=".mysql_escape_string($_GET["id"]);
    echo 
    $sql;
    exit; 

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kakao
    What if you delete the last double quote?
    PHP Code:
    $sql "SELECT * FROM info WHERE id=".mysql_escape_string($_GET["id"]); 
    Change $_GET["id"]); to $_GET['id']); ?

    Quote Originally Posted by Kakao
    If it does not work echo the sql string:
    PHP Code:
    $sql "SELECT * FROM info WHERE id=".mysql_escape_string($_GET["id"]);
    echo 
    $sql;
    exit; 
    The first suggestion did nothing...The second provided this:

    Code:
    SELECT * FROM info WHERE id=1
    It just displayed the sql query...

    Could this be the problem?


    $sql = "SELECT * FROM info WHERE id=".mysql_escape_string($_GET["id"]); - $sql=" starts the query, then you have id="...Wouldn't that end the query... My thought is that it's actually reading it as this:

    $sql = "SELECT * FROM info WHERE id="
    Last edited by saikou; 05-08-2006 at 01:43 AM.

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Location
    Brasília, Brazil
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The code you posted had an extra double quote at the end:
    Code:
    $sql = "SELECT * FROM info WHERE id=".mysql_escape_string($_GET["id"])";
    Quote Originally Posted by saikou
    The first suggestion did nothing...The second provided this:

    Code:

    SELECT * FROM info WHERE id=1
    It means the string is correctly built. Now just take the exit comand out and run. If it returns nothing is because there is no id=1 in the table.

  • #5
    New Coder
    Join Date
    May 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay..Should the code be:
    PHP Code:
    <?php

    $dbhost
    ='localhost';
    $dbusername='xxx';
    $dbuserpass='xxx';
    $dbname='info';

    mysql_connect ($dbhost$dbusername$dbuserpass);

    if (!
    mysql_select_db($dbname)) die(mysql_error());

    $sql "SELECT * FROM info WHERE id=".mysql_escape_string($_GET["id"]);
    echo 
    $sql;
    ?>
    Or...What? Because even after removing 'exit;' it still displays the query...

  • #6
    Regular Coder
    Join Date
    Mar 2006
    Location
    Brasília, Brazil
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can take the echo out. Now use your whole script. The only thing wrong with it was the extra double quote.

  • #7
    New Coder
    Join Date
    May 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    As a precaution I ran this first:

    PHP Code:
    <?php

    $dbhost
    ='localhost';
    $dbusername='xxx';
    $dbuserpass='xxx';
    $dbname='info';

    mysql_connect ($dbhost$dbusername$dbuserpass);

    if (!
    mysql_select_db($dbname)) die(mysql_error());

    $sql "SELECT * FROM info WHERE id=".mysql_escape_string($_GET["id"]);
    $result mysql_query($query) or die('Error, query failed');
    echo 
    $result;
    ?>
    Result:
    Error, query failed
    Then I ran the normal code without the quote and it displayed nothing, with no source as well.

  • #8
    New Coder
    Join Date
    May 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php

    $dbhost
    ='localhost';
    $dbusername='xxx';
    $dbuserpass='xxx';
    $dbname='info';

    mysql_connect ($dbhost$dbusername$dbuserpass);

    if (!
    mysql_select_db($dbname)) die(mysql_error());

    $query "SELECT * FROM info WHERE id=".mysql_escape_string($_GET["id"]);
    $result mysql_query($query) or die('Error, query failed');
    echo 
    $result;
    ?>
    Gives me this when ?id= 1,2, or 3

    Resource id #3
    However if no value is there for ?id= (such as visiting popup.php directly)

    I get the "error, query failed" message.

    Figured it out!

    PHP Code:
    <?php

    $dbhost
    ='localhost';
    $dbusername='xxx';
    $dbuserpass='xxx';
    $dbname='info';

    mysql_connect ($dbhost$dbusername$dbuserpass);

    if (!
    mysql_select_db($dbname)) die(mysql_error());

    $query "SELECT * FROM info WHERE id=".mysql_escape_string($_GET["id"]);
    $result mysql_query($query) or die('Error, query failed');
    while (
    $row mysql_fetch_assoc($result)) {
       echo 
    $row['id'];
       echo 
    $row['name'];
       echo 
    $row['description'];
    }
    ?>
    Returns the values of the table...Now I just have to format them. Thanks for your help Kakao
    Last edited by saikou; 05-08-2006 at 02:43 AM.

  • #9
    New Coder
    Join Date
    May 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can you echo an image? o.o...

    Like...

    Code:
    echo "<img src='path/to/image/$row['image']'>";
    Or how would you do that?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •