Jump to content

[SOLVED] So Simple, whats wrong with this?


virtuexru

Recommended Posts

OK. So I have a link with an ?id=$username field on the first page that works fine... Here's the code on the next page to try and get it to work but I get an error:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

Here's the code:

 

include('config/config.php');

include('config/connect.php');

 

$display  = "SELECT displayname, joindate, posts FROM userlist WHERE displayname = $id";

$result  = mysql_query($display) or die(mysql_error());

$userinfo = mysql_fetch_array($result);

Link to comment
https://forums.phpfreaks.com/topic/44636-solved-so-simple-whats-wrong-with-this/
Share on other sites

Register_globals are probably disabled for security reasons. If you are learning from an old book, the examples were written with the assumption that they were enabled. You need to explicitly retrieve the value from the $_GET superglobal array for values passed on the URL. Also, strings in MySQL need to be quoted.

 

<?php
$display  = "SELECT displayname, joindate, posts FROM userlist WHERE displayname = '" . $_GET['id'] . "'";
$result   = mysql_query($display) or die("Problem with the query: <pre>$display</pre><br>" . mysql_error());
$userinfo = mysql_fetch_assoc($result);
?>

 

Ken

<?php

  include('config/config.php');
  include('config/connect.php');

  if (isset($_GET['id'])) {
    $id = $_GET['id'];
    $display  = "SELECT displayname, joindate, posts FROM userlist WHERE displayname = '$id'";
    $result   = mysql_query($display) or die(mysql_error());
    $userinfo = mysql_fetch_array($result);
  }

?>

Along with Beast, do this:

 

include('config/config.php');
include('config/connect.php');

// Note to retrieve post/get should always be done this way.
$display  = "SELECT displayname, joindate, posts FROM userlist WHERE displayname = '".mysql_real_escape_string($_GET['id'])."'"); // the real excape helps prevent sql injection.
$result   = mysql_query($display) or die(mysql_error());
$userinfo = mysql_fetch_array($result);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.