4 Replies - 319 Views - Last Post: 03 April 2012 - 08:48 PM Rate Topic: -----

#1 rosepetalpowder  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 28-February 12

PROBLEM - php MYSQL query

Posted 03 April 2012 - 04:07 PM

I have a sql table which has two colums - page name and page link.

On the homepage.php I have a dropdown which pulls all page names with submit button.

when submit button is pressed, I want the page link corresponding to the selected page name from sql to pop up as a link . This is the code I came up with, BUT nothing is happening. where am i wrong?

  <?php

$conn = mysqli_connect('localhost', 'dc', 'bn',  'dbh');
 
if (!$conn) {
    die('Could not connect: ' . mysqli_error());
}


 $page = $_POST['page_name'];
 // inserts value into the sql table 
 
$sql = "SELECT page_link FROM pages WHERE page_name = '$page'";
 
$result = mysqli_query($conn, $sql);


if ($row = mysqli_fetch_array($result)) {
    
    //drop down options from sql table
    
    echo '<a href = "'. $row['page_link'] . '" > </a> ';

  } 




 mysqli_close($conn);
  

?>
 

This post has been edited by rosepetalpowder: 03 April 2012 - 04:09 PM


Is This A Good Question/Topic? 1
  • +

Replies To: PROBLEM - php MYSQL query

#2 Slice  Icon User is offline

  • sudo pacman -S moneyz


Reputation: 239
  • View blog
  • Posts: 693
  • Joined: 24-November 08

Re: PROBLEM - php MYSQL query

Posted 03 April 2012 - 07:09 PM

You have no text in your link.

echo '<a href = "'. $row['page_link'] . '" > </a> ';



should be something like:

echo '<a href = "'. $row['page_link'] . '" >'. $row['page_link'] .'</a> ';


Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: PROBLEM - php MYSQL query

Posted 03 April 2012 - 07:23 PM

I'd change line 11 so that you had this:
 $page = $_POST['page_name'];
 // inserts value into the sql table by inserting user supplied data directly into the query below, thus begging to be hacked. 
 
$sql = "SELECT page_link FROM pages WHERE page_name = '$page'";



Or, you could use prepared statements. Here's a link to a good PDO tutorial in case you'd like to keep your site from being defaced.
Was This Post Helpful? 1
  • +
  • -

#4 rosepetalpowder  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 28-February 12

Re: PROBLEM - php MYSQL query

Posted 03 April 2012 - 08:39 PM

View PostSlice, on 03 April 2012 - 07:09 PM, said:

You have no text in your link.

echo '<a href = "'. $row['page_link'] . '" > </a> ';



should be something like:

echo '<a href = "'. $row['page_link'] . '" >'. $row['page_link'] .'</a> ';



I tried your advice. it still doesnt display anything. any other suggestions i could try?
Was This Post Helpful? 1
  • +
  • -

#5 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5066
  • View blog
  • Posts: 26,448
  • Joined: 10-May 07

Re: PROBLEM - php MYSQL query

Posted 03 April 2012 - 08:48 PM

Can we try the following?
$sql = "SELECT page_link FROM pages WHERE page_name = '$page'";
 
$result = mysqli_query($conn, $sql);
if(!$result) {
  echo "<p>There was a an error with the following sql</p><p>$sql</p>";
  die();
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1