Jump to content

Unknown column in where clause.


HogDog

Recommended Posts

Hello all first time poster here, I'm new to PHP and have run into some trouble.

What I want to do is when a user logs in a session is created using a company name (a username basically). Then once the user has logged in they are redirected to account.php which takes the session created when the user logged in and sees what the companies corresponding url in the mysql database is then redirects the user to that url. Now everything works fine until the user gets to the accounts page and the output is the following:

 

logged in

Unknown column 'company_name' in 'where clause'

 

Heres the php for account.php

<?php

$query = mysql_connect("*********", "******", "***********") or die(mysql_error());

mysql_select_db('******', $query) or die(mysql_error());

 

session_start();

 

if(isset($_SESSION['Company'])) {

        echo "logged in <br/>" ;

        $query = mysql_query("SELECT url FROM links WHERE company = " . $_SESSION['Company'] . " LIMIT 1") or die(mysql_error());

 

        list($url) = mysql_fetch_row($query);

                header('Location: . $url .'); die('<a href=". $url .">Contiune</a>');

}

    else {

   

    header('location: login.php');

    }

?>

 

Thanks for the help, -HogDog

Link to comment
Share on other sites

Well, seeing as your query doesn't "appear" to have company_name in it, I would have to assume either 1) That is not the query creating the error or 2) The variable you are using has more information in it that it should.

 

Also, you are not enclosing the value of $_SESSION['Company'] within single quotes in the query. Unless the value is a number, the query will also fail.

 

You should create your queries as a variable so you can echo them to the page when an error occurs for debugging purposes.

 

<?php

$query = mysql_connect("*********", "******", "***********") or die(mysql_error());
mysql_select_db('******', $query) or die(mysql_error());

session_start();
  
if(isset($_SESSION['Company']))
{
     echo "logged in <br>" ;
     $query = "SELECT url FROM links WHERE company = '" . $_SESSION['Company'] . "' LIMIT 1";
     $result = mysql_query($query) or die("Query:<br>$query<br>Error:<br>".mysql_error());
  
     list($url) = mysql_fetch_row($result);
     header('Location: . $url .'); die('<a href=". $url .">Contiune[/url]');
}
else
{
     header('location: login.php');
}

?>

Link to comment
Share on other sites

I used to use this lol Its from ages ago so it's a bit crap lol

<?php

function sqlErr($line = NULL,$file = NULL,$err = NULL){
echo '<link rel="stylesheet" type="text/css" href="style.css" />';

$l = "<font color='red'><strong>A SYNTAX error has occured, please read on...<br /><br />";

if ($err != NULL){
$l1 = "Error: ".$err."<br /><br />";
}else{
$l1 = "";
}

if ($file != NULL){
$l2 = "File: ".$file."<br /><br />";
}else{
$l1 = "";
}

if ($line != NULL){
$l3 = "Line: ".$line."<br /><br />";
}else{
$l3 = "";
}

$l4 = "Please report this to an administrator.</strong></font>";

$fullLine = $l.$l1.$l2.$l3.$l4;

$return = '<table width="500" height="300" border="1" cellspacing="0" cellpadding="0" valign="middle" align="center" bordercolor="red">
<tr><td align="center"><h3><font color="red"><strong>[ ERROR ]</strong></font></h3></td></tr>
<tr><td align="center">'.$fullLine.'</td></tr>
</table>';

return $return;

}


function displayError($errormsg = NULL){

if ( (!empty($errormsg)) && ($errormsg != NULL) ){
echo '<center><fieldset class="fieldset" width="500"><legend><strong><font color="red">Error:</strong></legend>'.$errormsg.'</font></fieldset></center>';
}

}

function displayUpdate($successMsg = NULL){

if ( (!empty($successMsg)) && ($successMsg != NULL) ){
echo '<center><fieldset class="fieldset" width="500"><legend><strong><font color="green">Error:</strong></legend>'.$successMsg.'</font></fieldset></center>';
}

}

mysql_query($query)or die(sqlErr(__LINE__.__FILE__.mysql_error()));

?>

Link to comment
Share on other sites

Thanks guys that solved one problem but now I have another, when the page redirects the URL reads:

 

 

Heres my updated PHP:

<?php
ob_start();
$query = mysql_connect("*************", "******", "********") or die(mysql_error());
mysql_select_db('*******', $query) or die(mysql_error());
session_start(); 
if(isset($_SESSION['Company']))
{
     echo "logged in <br>" ;
     $query = "SELECT url FROM links WHERE company = '" . $_SESSION['Company'] . "' LIMIT 1";
     $result = mysql_query($query) or die("Query:<br>$query<br>Error:<br>".mysql_error());
  
     list($url) = mysql_fetch_row($result);
     header('Location: . $url .'); die('<a href=". $url .">Contiune</a>');
}
else
{
     header('location: login.php');
}
?>

 

thanks again -HogDog

Link to comment
Share on other sites

Thanks guys that solved one problem but now I have another, when the page redirects the URL reads:

 

it's because you're using variables within single quotes. PHP wont parse variables within single quotes.

header('Location: . $url .'); die('<a href=". $url .">Contiune</a>');

The above line should be

header("Location: $url"); die('<a href="'. $url .'">Contiune</a>');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.