Page 1 of 2

Problem ??!!??

Posted: Tue May 11, 2004 8:32 am
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

Posted: Tue May 11, 2004 8:38 am
by JayBird

Posted: Tue May 11, 2004 8:38 am
by magicrobotmonkey
check the size of the column in your dbase - i'd say its too small!

Posted: Tue May 11, 2004 9:00 am
by leewad
no it is set as TEXT so its not that

Thanks anyway

Posted: Tue May 11, 2004 9:06 am
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

Posted: Tue May 11, 2004 9:15 am
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 ??

Posted: Tue May 11, 2004 9:19 am
by magicrobotmonkey
what happens if you echo it after that substr?

Posted: Tue May 11, 2004 9:19 am
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.

Posted: Tue May 11, 2004 9:35 am
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

Posted: Tue May 11, 2004 9:37 am
by markl999
What does echo $str; show? And you shouldn't need any substr's if you're using the join() method.

Posted: Tue May 11, 2004 9:45 am
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'   ");

Posted: Tue May 11, 2004 9:51 am
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*

Posted: Tue May 11, 2004 10:10 am
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 ?

Posted: Tue May 11, 2004 10:17 am
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"");
?>

Posted: Tue May 11, 2004 10:21 am
by leewad
Done it at last

Code: Select all

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