Jump to content

Select Statement with HTML Variables


jt2006

Recommended Posts

Thanks for taking the time to help me out.

I'm trying to do what seems to be a simple select statement.  All I want is for someone to enter in their email address and check the database.  If it's there, retrieve that row.

The problem is that I'm getting all confused as to the proper format of the select statement using the form variables.  I've seen .$variable.  '$variable'  and even '%".$variable."%'.

Can someone take a look and help me out?

Here's my code:


$queryterm=$_POST['updateterm'];

$hostname="server";
$username="DBuser";
$password="DBPassword";
$dbname="DBName";
$usertable="TableName";

mysql_connect($hostname, $username, $password) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

[color=purple]$query = mysql_query("SELECT * FROM '$usertable' WHERE " '$queryterm' "= email");[/color]

if (!$query) {
  die("query Failed. " . mysql_error());
}

While ($row = mysql_fetch_array($result)) {
echo $row['email'];
}

Link to comment
https://forums.phpfreaks.com/topic/13953-select-statement-with-html-variables/
Share on other sites

You need to surround strings with single quotes in MySQL. In your example you have the quotes surrounding the wrong items. Try something like this:
[code]<?php
$query = "SELECT * FROM $usertable WHERE email = '$queryterm'";
$result = mysql_query($query) or die("There was a problem with the query: $query<br>" . mysql_error());
while ($row = mysql_fetch_assoc($result))
    echo $row['email'] . "<br>\n";
?>[/code]

Ken

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.