madspof Posted January 13, 2008 Share Posted January 13, 2008 Hi everyone i am trying to use a variable in a string but i cannot escape the string does anyone no why ? echo "<a href='makep.php?p=\$row['Picture']\'>protect from public </a><br><br></div>"; Quote Link to comment Share on other sites More sharing options...
Fyorl Posted January 13, 2008 Share Posted January 13, 2008 You need: echo "<a href='makep.php?p=$row[Picture]'>protect from public </a><br><br></div>"; Quote Link to comment Share on other sites More sharing options...
madspof Posted January 13, 2008 Author Share Posted January 13, 2008 I have already tried this but it does not work Quote Link to comment Share on other sites More sharing options...
trq Posted January 13, 2008 Share Posted January 13, 2008 echo "<a href='makep.php?p={$row['Picture']}'>protect from public</a><br><br></div>"; Quote Link to comment Share on other sites More sharing options...
Fyorl Posted January 13, 2008 Share Posted January 13, 2008 @thorpe: The curly braces around the variable aren't necessary for simple arrays like that. @madspof: You obviously haven't tried my code because it will work. You're escaping the $ character which means PHP treats it as a normal character rather than the start of a variable. Quote Link to comment Share on other sites More sharing options...
trq Posted January 13, 2008 Share Posted January 13, 2008 @thorpe: The curly braces around the variable aren't necessary for simple arrays like that. They are unless you want to generate a notice. Quote Link to comment Share on other sites More sharing options...
madspof Posted January 13, 2008 Author Share Posted January 13, 2008 sorry about that Fyorl i did use your script but there was something wrong with another part of my script which i have now fixed which was causing your script not to work. thrope your script now also works. Thanks to the both of you. Quote Link to comment Share on other sites More sharing options...
Fyorl Posted January 13, 2008 Share Posted January 13, 2008 They are unless you want to generate a notice. Really? I've never encountered one before and my error_reporting level does include E_NOTICE. Quote Link to comment Share on other sites More sharing options...
trq Posted January 13, 2008 Share Posted January 13, 2008 I think it depends. If you use... echo "<a href='makep.php?p=$row[Picture]'>protect from public</a><br><br></div>"; You won't get the notice, but you will get a warning about the undefined constant Picture. If you use... echo "<a href='makep.php?p=$row['Picture']'>protect from public</a><br><br></div>"; You should get some notice about using complex variables without braces. To be honest, Ive never tested it. but it is always best to use braces around complex variables. ie; arrays, object calls. Quote Link to comment Share on other sites More sharing options...
madspof Posted January 13, 2008 Author Share Posted January 13, 2008 Just another quick question would this work: mysql_query ("UPDATE userpic SET public = '1' WHERE userid = '$dir' AND Picture = '$pc'"); Quote Link to comment Share on other sites More sharing options...
trq Posted January 13, 2008 Share Posted January 13, 2008 It looks valid enough providing $dir and $pc are defined. Quote Link to comment Share on other sites More sharing options...
madspof Posted January 13, 2008 Author Share Posted January 13, 2008 I am executing the script but it does not seem to update the database and i know that the variables have been defined as I echo them before the query runs Quote Link to comment Share on other sites More sharing options...
trq Posted January 13, 2008 Share Posted January 13, 2008 Post the relevent code then. Quote Link to comment Share on other sites More sharing options...
madspof Posted January 13, 2008 Author Share Posted January 13, 2008 <?php include("../connect.php"); $cookie_info = explode("-", $_COOKIE['cookie_info']); $dir = $cookie_info[0]; $pc = $_GET["p"]; $query = "SELECT public FROM userpic WHERE userid = '$dir' AND Picture = '$pc' "; $result = mysql_query($query); $row = mysql_fetch_assoc($result); echo "{$row['public']}" . "$pc"; if($row['public'] == 1){ mysql_query ("UPDATE userpic SET public = '1' WHERE userid = '$dir' AND Picture = '$pc'"); }else{ mysql_query ("UPDATE userpic SET public = '0' WHERE userid = '$dir' AND Picture = '$pc''"); } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted January 13, 2008 Share Posted January 13, 2008 The logic of your code says if public equals 1 update it to equal 1. hence, nothing changes. Quote Link to comment Share on other sites More sharing options...
madspof Posted January 13, 2008 Author Share Posted January 13, 2008 SOrry about that the logic is now 0 then set to 1 but it still does not work. Quote Link to comment Share on other sites More sharing options...
Fyorl Posted January 13, 2008 Share Posted January 13, 2008 You won't get the notice, but you will get a warning about the undefined constant Picture. Sorry to go off-topic here but actually, no, you don't get a warning about the undefined constant. That would only happen if you used {$row[Picture]}. In that case, {$row['Picture']} would be needed however if no curly braces are used then using $row[Picture] will have the desired effect without generating any level of errors. I have tested this insofar as I always use it when expanding arrays inside strings. I only ever use the curly brace notation when doing object calls as they don't work without curly braces. Quote Link to comment Share on other sites More sharing options...
madspof Posted January 13, 2008 Author Share Posted January 13, 2008 okay i will try that Quote Link to comment Share on other sites More sharing options...
madspof Posted January 13, 2008 Author Share Posted January 13, 2008 i currently use this script but i am getting no errors but the script is not changing the databse <?php include("../connect.php"); $cookie_info = explode("-", $_COOKIE['cookie_info']); $dir = $cookie_info[0]; $pc = $_GET["p"]; $query = "SELECT public FROM userpic WHERE userid = '$dir' AND Picture = '$pc' "; $result = mysql_query($query); $row = mysql_fetch_assoc($result); echo "{$row['public']}" . "$pc"; if($row['public'] == 0){ mysql_query ("UPDATE userpic SET public = '1' WHERE userid = '$dir' AND Picture = '$pc'"); }else{ mysql_query ("UPDATE userpic SET public = '0' WHERE userid = '$dir' AND Picture = '$pc''"); } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted January 13, 2008 Share Posted January 13, 2008 Change both occurances of mysql_query() to... mysql_query ("UPDATE userpic SET public = '1' WHERE userid = '$dir' AND Picture = '$pc'") || die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
madspof Posted January 13, 2008 Author Share Posted January 13, 2008 Okay well i did that and i didnt get any errors so i tried uplaoding another picture and the script works so am stumped why that has happend but it works nw Quote Link to comment Share on other sites More sharing options...
Fyorl Posted January 13, 2008 Share Posted January 13, 2008 Your last SQL query should generate an error because you've got too many quotes: "UPDATE userpic SET public = '0' WHERE userid = '$dir' AND Picture = '$pc''" should be: "UPDATE userpic SET public = '0' WHERE userid = '$dir' AND Picture = '$pc'" You might want to try putting 'or die(mysql_error())' (without the quotes) after your queries. Also, you should echo the values of $dir and $pc to make sure they're what you're expecting. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.