Jump to content

mysql_query Question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
17 replies to this topic

#1
SirBee

SirBee

    Newbie

  • Members
  • PipPip
  • 21 posts
Hello all,

I have run into another problem that I just cannot seem to figure out. I would greatly appreciate some guidance on this probably very easy solution.

All I am trying to do is have the user enter a name, and then I search the database to find that name. I have a form set up to get the information, and it is going in properly, but my query is not returning any results. I even tried skipping the user input, and just pulled out a team I knew was in there, still to no avail.

Your suggestions are greatly appreciated, thank you.

SirBee

<?php


include 'connect.PHP';

$oldName=$_POST['oldName'];


$registrationQuery=mysql_query("SELECT `showstandings`.`subcategory` AS `subcategory` AND `showstandings`.`cat_id` AS `catID` FROM showstandings WHERE `showstandings`.`subcategory` = '$oldName'");


echo "$oldName <br><br>";


while ($registration_array=mysql_fetch_array($registrationQuery))

{

	echo "Cat ID = ".$catID." Team Name = ".$subcategory."<br><br>";

}

?>


#2
SirBee

SirBee

    Newbie

  • Members
  • PipPip
  • 21 posts
After another frustrating 20 minutes of looking at this code I found the error. Thanks anyway.

SirBee

#3
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
You should post the solution.

#4
SirBee

SirBee

    Newbie

  • Members
  • PipPip
  • 21 posts
The solution is in the code below. Basically what I had to do was take out the word AND in the query and replace it with just a comma. There is extra code in the second post, but that is just because I needed the final product to do more then my first post.

<?php

include 'connect.PHP';
$oldName=$_POST['oldName'];
$newName=$_POST['newName'];
$test=$_POST['nameSubmit'];

$registrationQuery=mysql_query("SELECT `showstandings`.`subcategory` AS `subcategory`, `showstandings`.`cat_id` AS `catID`, `showstandings`.`identifier` AS `identifier` FROM showstandings WHERE `showstandings`.`subcategory` = '$oldName'");

while ($registration_array=mysql_fetch_array($registrationQuery))
{
	echo "Test = ".$test." Cat ID = ".$registration_array['catID']." Team Name = ".$registration_array['subcategory']."<br><br>";
}
?>

<form action="changeNameFunction.php" method="post">
<div align="center" face="verdana">
Please confirm by entering the cat ID for the team you wish to change: <input type="text" name="catID"><br><br>
<input type="submit" name="submit">
<input type="hidden" name="oldName" value="<?php echo $oldName ?>">
<input type="hidden" name="newName" value="<?php echo $newName ?>">
<input type="hidden" name="ID" value="<?php echo $registration_array['identifier'] ?>">
</div>
</form>


#5
SirBee

SirBee

    Newbie

  • Members
  • PipPip
  • 21 posts
I have run into a problem on the next part of this code. I am trying to use the variable $ID as my test to update the table, the problem comes from the value of $ID. It is of the form
###-#-####-##. I get the impression that the query is reading the dashes as 'minus' signs. How do I enter it properly as to escape the $ID variable and use the whole string as my test.

Thank you in advance for your help.

SirBee

<?php



include 'connect.php';



$oldName=$_POST['oldName'];

$newName=$_POST['newName'];

$ID=$_POST['team'];



echo "Old Name = ".$oldName." New Name = ".$newName." Identifier = ".$ID."<br><br>";



if ($ID && $newName && $oldName)

{

	echo "Success";

	mysql_query("UPDATE showstandings SET subcategory = $newName WHERE identifier = $ID");

}


#6
While1

While1

    Newbie

  • Members
  • Pip
  • 3 posts
Try single quotes around the variable.

#7
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts

SirBee said:

I have run into a problem on the next part of this code. I am trying to use the variable $ID as my test to update the table, the problem comes from the value of $ID. It is of the form
###-#-####-##. I get the impression that the query is reading the dashes as 'minus' signs. How do I enter it properly as to escape the $ID variable and use the whole string as my test.

Thank you in advance for your help.

SirBee

<?php



include 'connect.php';



$oldName=$_POST['oldName'];

$newName=$_POST['newName'];

$ID=$_POST['team'];



echo "Old Name = ".$oldName." New Name = ".$newName." Identifier = ".$ID."<br><br>";



if ($ID && $newName && $oldName)

{

    echo "Success";

    mysql_query("UPDATE showstandings SET subcategory = $newName WHERE identifier = $ID");

}

Since the data type is not an integer it should be enclosed in single quotes.
mysql_query("UPDATE showstandings SET subcategory = $newName WHERE  identifier = '$ID'") or die (mysql_error());

Also trying using mysql_error() it gives you a lot of information about what's wrong with your query.

#8
SirBee

SirBee

    Newbie

  • Members
  • PipPip
  • 21 posts
Thank you for your suggestions, but it still does not work.

I have put the variables in single quotes ('), double quotes ("), and the weird quote that I do not know what to call (`) and it still does not work. The code runs all the way through to the end, and even prints the word success, so it gets into the final if section. The only thing it does not do is change the variable I need it to change. Nothing in my database changes. I have it print out the variables before hand, so I know each one contains the right values.

I am out of ideas.

#9
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Try echoing it out and seeing what it says maybe you can find the error easier or posting it up here. What did the mysql error say?

echo "UPDATE showstandings SET subcategory = $newName WHERE  identifier = '$ID'";


#10
SirBee

SirBee

    Newbie

  • Members
  • PipPip
  • 21 posts
I am not sure what exactly you are suggesting I do. Take out the "mysql_query(" and replace it with echo?

As for the mysql_error. When I run the code that with the following line in it, I get no mysql_error output. Still nothing changes within the database.

mysql_query("UPDATE showstandings SET subcategory = '$newName' WHERE identifier = '$ID'") or die (mysql_error());


#11
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
but then, the query is probably written with syntax but does the wrong thing. Maybe the variables isn't what you expect them to be, that's why the advice is to print the query to see that the query is what you want it to be. this is a very common troubleshooting method!
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#12
SirBee

SirBee

    Newbie

  • Members
  • PipPip
  • 21 posts
It was not that I was unwilling to do it, I was just confused as to what was being asked of me. I would never have thought to do that.

After I ran the line

echo "UPDATE showstandings SET subcategory=$newName WHERE identifier='$ID'"

it output everything properly, but the ID variable had a ' in front of it. I assume that is why it is not matching up. If I remove the ' ' around the ID variable, and run the echo again, it outputs properly. Then when I run the query again, I get the following output:

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 '2 WHERE identifier=202-2-2010-98

I guess the question is, how do I use the right syntax without adding the ' to the ID variable?

SirBee