Problem ??!!??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Problem ??!!??

Post by leewad »

Can anyone explain why this will not enter into the database ?

Code: Select all

if($submit == "true"){

$price = $_POST['price'];
$location = $_POST['location'];

 foreach($_POST['checkbox'] as $value) {

  $longstring .= $value."' or userID ='"."\r\n";

}
$str = "Select * from boxers where userID = '".$longstring;


$str = substr($str, 0, -14); // takes away the last or userID =

echo $str; // which outputs Select * from boxers where userID = 'haf1' or userID = 'haf2'

// Which it exactly what I want - but when adding to the database

$result=MYSQL_QUERY( "update table SET string='$str'   ");
it results in Select * from boxers wh being added ?? not the whole string

Any ideas ??

basically what I'm doing is having a list of checkboxes which when submitted it creates a string in the database to pull a result in another page.

Select * from boxers where userID = 'haf1' or userID = 'haf2'

haf1 and haf2 being 2 of the checkboxes




Cheers
Last edited by leewad on Tue May 11, 2004 8:55 am, edited 2 times in total.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

check the size of the column in your dbase - i'd say its too small!
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Post by leewad »

no it is set as TEXT so its not that

Thanks anyway
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

not returning any errors?

tag this onto the end and see what happens

Code: Select all

if (!$result) {
   die('Invalid query: ' . mysql_error());
}
mark
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Post by leewad »

Nope no error !

I think it may have something to do with the string being made from an array or am I in the wrong direction ?

If I changed :

Code: Select all

$str = substr($str, 0, -14); // takes away the last or userID =
to:

Code: Select all

$str = substr($str, 0, -13); // takes away the last or userID =
Then is adds another character - so it must be something to do with the string - it echos ok but not formatted somehow ??
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

what happens if you echo it after that substr?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You could just do:

Code: Select all

foreach($_POST['checkbox'] as $value) {
  $longstring[] = "userID='".$value."'";
}
$str = 'Select * from boxers where '.join(' OR ', $longstring);
and avoid having to do all that substr() thing.
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Post by leewad »

Thanks Guys but still having problems! this is giving me a right headache

Yes it echos fine after the substr.

Code: Select all

foreach($_POSTї'checkbox'] as $value) { 
  $longstringї] = "userID='".$value."'"; 
} 
$str = 'Select * from boxers where '.join(' OR ', $longstring);

produces the following error:
Invalid query: 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 'haf2''' at line 1
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

What does echo $str; show? And you shouldn't need any substr's if you're using the join() method.
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Post by leewad »

Code: Select all

echo $str;
Produces the correct string:

Code: Select all

Select * from boxers where userID='haf1' OR userID='haf2'
but it gives the error when trying to update database:

Code: Select all

$result=MYSQL_QUERY( "update boxers SET string='$str'   ");
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Hmm..yeah, UPDATE and SELECT are 2 different things. I'm not sure what you're trying to do, SELECT or UPDATE?
UPDATE syntax should be UPDATE boxers SET string='whatever' WHERE something='blah' ... i'm not sure where your select comes into it. Maybe you want to "UPDATE boxers SET string='whatever' WHERE $longstring"; ? and not even bother with a select *shrug*
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Post by leewad »

What I want to do is:

from check boxes with database references make a select query string which we have made which updates into a field in the database.

On a different page i need to pull the select string we have created from the database to slect from another database.

Code: Select all

$result=MYSQL_QUERY( "update table SET string='$str'   ");
or

Code: Select all

$result=MYSQL_QUERY( "update table SET string='$str'  where 1 ");
should both work - i have other fields updating along side this which is not menioned - but this string is the only problem I have in getting it updated in the database - I can create the string but there is something about it with is stopping it updating maybe its the ' ' in the string ?
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Code: Select all

<?php
//try changing this:
$result=MYSQL_QUERY( "update boxers SET string='$str'   ");

//to this
$result=MYSQL_QUERY( "update boxers SET string="$str"");
?>
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Post by leewad »

Done it at last

Code: Select all

$str = addslashes('Select * from boxers where '.join(' OR ', $longstring));
Thanks for all your help !!
Post Reply