Jump to content

What is wrong with this query?


ballhogjoni

Recommended Posts

Try this:

<?php
$Tittle = $_POST['title'];
$Discription = $_POST['description'];
$Price = $_POST['price'];

$result = mysql_query("SELECT Username, title, description, price FROM products WHERE Username='$Username' AND title='$Tittle' AND description='$Discription' AND price='$Price'", $connection) or die(mysql_error());
$Row = mysql_num_rows($result);

echo $Rows;
?>

 

If it returns a number, then the query will work.

Link to comment
Share on other sites

$Title = trim($_POST[title]);

$Discription = trim($_POST[description]);

$Price = $_POST['price'];

 

$query = "SELECT Username, title, description, price FROM products WHERE Username = '$Username' AND title =' $Title' AND description = '$Discription' AND price = '$Price'";

$result = mysql_query($query) or die(mysql_error() . " Line: " .__LINE__);

 

//then call your row values later

Link to comment
Share on other sites

@ballhogjoni

 

"what is wrong with this query" would have been more easily answered if you had explained just what it is or isn't doing that you expected, or citing the error returned if there was one.

 

And "that didn't work" hasn't explained what "didn't work".

 

You can try the code posted by Neptunus Maris and/or add something to echo the actual query string (since it might not be what you think it ought to be).

 

If you want help, you need to provide better information. 

Link to comment
Share on other sites

Sorry bout that andy,

What doesn't work is having multiple AND's in the where clause. Basically it is not echoing any info whn I do that. I don't' know if mySQL can handle that or if there is something wrong with my syntax. i tried both Neptunus Maris and NArc0t1c ideas but they both came up with no records, or in other words did not echo anything.

Link to comment
Share on other sites

There's nothing wrong with the query. It's returning no results because no records match ALL of those conditions exactly.  Check carefully what's in the database table that you're querying. You may need to trim POSTed data.  Test it using phpMyAdmin, etc.

Link to comment
Share on other sites

Sorry bout that andy,

What doesn't work is having multiple AND's in the where clause. Basically it is not echoing any info whn I do that. I don't' know if mySQL can handle that or if there is something wrong with my syntax. i tried both Neptunus Maris and NArc0t1c ideas but they both came up with no records, or in other words did not echo anything.

 

You can use multiple ANDs

Link to comment
Share on other sites

There's nothing wrong with the query. It's returning no results because no records match ALL of those conditions exactly.  Check carefully what's in the database table that you're querying. You may need to trim POSTed data.  Test it using phpMyAdmin, etc.

 

I have to believe that there is somethign wrong with my logic because when I comment this code out the script works fine, but when I leave it in the script nothing is showing on the page. No errors, no html, nothing.

 

heres is my cod eof the whole page. I hope this helps.

 

<?php 
session_start(); 
include('db.php'); 
include('functions.php'); 
checkLogin('1 2');
$query = mysql_query("SELECT * FROM users WHERE ID = '{$_SESSION['user_id']}'");
$row = mysql_fetch_array($query) or die(mysql_error());
$Username = $row['Username'];
if (isset($_POST['submit'])) {
$result = mysql_query("SELECT * FROM products WHERE Username = '$Username' AND title = '{$_POST['title']}' AND description = '{$_POST['description']}' AND price = '{$_POST['price']}'") or die(mysql_error());
$productNum = mysql_num_rows($result);
echo $productNum'<br>';
/*if ($productNum < 0) {
$productCheck = mysql_fetch_array($result);
if ($Username == $productCheck['Username'] && $productCheck['title'] == $title) {
echo 'Hello';
} else {
echo 'dint work'
}
}*/
$title = $_POST['title'];
if (isset($_POST['title']) && isset($_POST['desc']) && isset($_POST['price'])) {
$title_checked = strip_tags($_POST['title']);
$desc_checked = strip_tags($_POST['desc']);
$price = $_POST['price'];
if (!empty($title_checked) && !empty($desc_checked) && !empty($price)) {
	if (strlen($title_checked) > 20 || strlen($desc_checked) > 30) {
		$leng = '<span class="problemMessage">Please check the length of your Title and Description. Title is 20 Characters Max and Description is 30 Characters Max.</span>';
	} elseif (strlen($title_checked) <= 20 && strlen($desc_checked) <= 30) {
    		mysql_query("INSERT INTO products (Username, title, description, price) VALUES ('$Username','$title_checked','$desc_checked','$price')") or die(mysql_error());
		$verbage = '<span class="goodMessage">Your product has been saved.</span>';
	}
} else {
	$verbage = '<span class="problemMessage">Product has NOT been Saved. Please enable Javascript & be sure to fill in all fields</span>';
}
}
}
?>
<html>
<head>
<title>Welcome to the Members Area</title>
<link type="text/css" rel="stylesheet" href="style_sheet.css" media="screen">
<script type="text/javascript">
function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateTitle(theForm.title);
  reason += validateDesc(theForm.desc);
  reason += validatePrice(theForm.price);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}
function validateEmpty(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}
function validateTitle(fld) {
    var error = "";
    var illegalChars = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|\(\)\<\>\,\;\:\\\"\\\/[\]]/;

    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a Title.\n";
    } else if ((fld.value.length < 4) || (fld.value.length > 20)) {
        fld.style.background = 'Yellow'; 
        error = "The title is the wrong length. The title can only have one Capital letter for each word. 20 Characters Max.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The title contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}
function validateDesc(fld) {
    var error = "";
    var illegalChars = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|\(\)\<\>\,\;\:\\\"\\\/[\]]/;

    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a Description.\n";
    } else if ((fld.value.length < 1) || (fld.value.length > 30)) {
        fld.style.background = 'Yellow'; 
        error = "The Description is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The Description contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}
function validatePrice(fld) {
    var error = "";
    var illegalChars= /[abcdefghijklmnopqrstuvwxyz\$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|\(\)\<\>\,\;\:\\\"\\\/[\]]/;

    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a Price.\n";
    } else if ((fld.value.length < 1) || (fld.value.length > 9)) {
        fld.style.background = 'Yellow'; 
        error = "The Price is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The Price contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}
</script>
</head>
<body>
<table width="100%" CELLSPACING="0" CELLPADDING="0" BORDER="0">
<tr>
	<td WIDTH="275" HEIGHT="64" valign="bottom">
	<p><a href="index.php"><img BORDER="0" src="http://" width="275" height="47" alt="My Logo that hasn't been created" /></a></p>
	</td>
	<td WIDTH="100%" HEIGHT="64" VALIGN="TOP">
	<DIV ALIGN="RIGHT"></DIV>
	</td>
</tr>
<tr>
	<td width="100%" height="11" colspan="2">
	<p><img width="100%" height="11" src="http://img.auctiva.com/imgdata/2/5/2/2/9/4/webimg/63422197_o.gif" alt="FreeCart's Free Shopping Cart Hosted Online with Our Servers."></p>
	</td>
</tr>
</table>
<table cellpadding="1" cellspacing="0" border="0" bgcolor="#6699CC" width="100%">
<tr>
	<td height="21" class="navigationtext">
	<?php include('member_top_nav.php'); ?>
	</td>
</tr>
</table>
<table class="standard">
<tr>
	<td valign="top" class="leftPanel">
	<table class="sideMenu">
		<tr>
			<td class="sideNavHeading">
			Products
			</td>
		</tr>				
		<?php
		$count = "SELECT COUNT(*) FROM products WHERE Username = '$Username' ";
		$result = mysql_query($count) or die(mysql_error());
		$product_count = mysql_result($result, 0, 0); 
		if ($product_count <= 24) {
		$query = "SELECT * FROM products WHERE Username = '{$Username}'"; 
			$result = mysql_query($query) or die(mysql_error());
				while($row = mysql_fetch_array($result)) {
				echo "<tr><td class=\"sideNav\"><a href=\"edit-product.php?ID=".$row['ID']."\">".$row['title']."</a></td></tr>";
				}
			if (mysql_num_rows($result) == 0) {
				echo "<tr><td class=\"sideNav\">You have no products!</td></tr>";
			}
		}
		?>
		<tr>
			<td class="sideNavHeading">RECOMMENDED</td>
		</tr>
		<tr>
			<td class="sideNav">
			<a href="http://ballhogjon.bryxen1.hop.clickbank.net/?tid=FC" target="_blank" class="sideNavLink">Website SEO</a>
			</td>
		</tr>
	</table>
	</td>
	<td class="mainPanel">
	<table>
		<tr>
			<td width="350" valign="top" style="border-right:1px solid #D2DBE7">
			<span class="titleText">Add a Product</span><br>
			<div style="border-top:1px solid #D2DBE7"></div>
			<table cellpadding="5" align="center">
				<tr>
					<td style="font-family:verdana;font-size:75%">
					<?php
					if ($product_count <= 24) {
						if (isset($verbage)) { echo '<center>'.$verbage.'</center>'; } if (isset($leng)) { echo '<center>'.$leng.'</center>'; } unset($verbage,$leng);?>
					<form action="" onSubmit="return validateFormOnSubmit(this)" method="POST">
					<!--<div align="center" class="formText">This form requires javascript to be enabled</div>-->
					<table class="standard">
						<tr>
							<td class="formTableLeft">Title:</td>
							<td class="formTableRight"><input type="text" name="title" /></td>
						</tr>
						<tr>
							<td class="formTableLeft">Description:</td>
							<td class="formTableRight"><input type="text" name="desc" /></td>
						</tr>
						<tr>
							<td class="formTableLeft">Price:</td>
							<td class="formTableRight"><input type="text" name="price" /></td>
						</tr>
						<tr>
							<td colspan="2" align="center"><br /><input type="hidden" name="submit" /><input class="formText" type="submit" value="Add Product" /></td>
						</tr>
					</table>
					</form>
					<?php
					} else {
						echo '<span class="problemMessage"><center>You must purchase more product space. Due to the expense we receive for offering this free service, we can only allow 25 free products. <a href="">Click here</a> to purchase more product space.</center></span>';
					}
					?>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>
</body>
</html>

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.