I've been teaching myself php & mysql & have found this site invaluable. Thanks to all who answer questions.
I have found several q&a's that are close to this & they HAVE helped to point me in the right direction but I can't quite get this to work.
Here's what I'm trying to do: Pass a constructed SQL query to a second php script so that the results can be displayed in a new window. I can't quite seem to get the syntax right to pass the sql
query ($sql) to open.window correctly. A matter of quotes I
think. I have tried echo instead of print, single quotes with double quotes inside, using ('$sql'), but nothing seems to work.
I do need this to be called by a variable, so that it is after the submit is given. I would prefer not to have a second submit
Help?
if (isset($_POST['NEWWINDOW'])) {
echo "<script>window.open("results.php?sql=$sql)</script>";
}
This will concatinate your $sql-variable with the rest of the string, however doing this you should keep in mind to sanitize your sql in result.php before submiting it to your database.
if (isset($_POST['NEWWINDOW'])) {
echo "<script>window.open('results.php?sql=".$sql."')</script>";
}
Try this
<?php
if (isset($_POST['NEWWINDOW'])) {
echo "<script>window.open('results.php?sql=$sql')</script>";
}
?>
Thank you each for your responses. This query form is private, & I will be the only one using it... (behind firewall), so the risk is minimal. However, the point is well made & I would certainly rather learn to do it the right way than the wrong way. The database is important. Recommendations?
Neither of the two offered suggestions as to syntax work for me.
I made sure & copy the syntax exactly. In both (all three cases) the window.open fails to open a window. I finally cut & pasted to be sure, but the behavior is the same.. the window does not even open.
Related
I am a beginner in PHP, so I need a little help.
I have stored some PHP code in a MySQL database (in a table), and now I'm trying to echo in another PHP page (<?PHP echo $result['code'];?>) after making a successful connection with the database and selecting the right table where I stored the code in the code column. But it is not working.
If I store HTML or JavaScript or any code in the database and try to echo, the content is displayed as it was programmed, as you can see in this image:
.
But it doesn't work with PHP. How can I fix it?
I am going to have to do some guessing, and show a very simple example, but something like the following should work.
Add a code_type column to your table with values of html, php, etc.
Make sure that any php code stored in the database has been tested and returns some output (without using <?php tags). For example:
$var1 = 100;
$var2 = 10;
$var3 = 1;
echo "<h1> The equation of ($var1*$var2+$var3) equals</h1>";
echo "<p>".($var1*$var2+$var3)."</p>";
Use the code_type in your data-output script to either evaluate the PHP code or just echo it in all other cases:
<?php
if ($result['code_type'] == 'php') {
eval($result['code']);
} else {
echo $result['code'];
}
?>
I cannot say that I have ever used this, so I can't guarantee anything, but hopefully points you in the right direction.
Important:
It is worth noting that this is not considered good practice, as noted multiple times in the PHP Manual page. The manual itself notes it in several places, but the first comment is on-key:
If eval() is the answer, you're almost certainly asking the wrong
question. -- Rasmus Lerdorf, BDFL of PHP
... You are probably a lot better off parsing the php code before you save it to the database, then you won't have to worry about any of this!
php5, Microsoft SQL Server Management Studio, OS 6.3.15063. i get $row data via mssql_fetch_assoc and stuff a PHP array with the $row data just fine when the first record does not have double quotes. subsequent row data can have double quotes, just not the first row. Error: "SyntaxError: missing ) after argument list" in jquery.min - so i cannot peruse and get helpful info from that. code:
$History_SQL = "
DECLARE #AID int;
SET #AID = $Activity_ID;
SELECT activityid, lpid, abstract, changeid, changestamp FROM cpy_activity_history
WHERE activityid = #AID ORDER BY changestamp DESC;
";
$History_results = mssql_query($History_SQL);
while ($row = mssql_fetch_array($History_results)) {
$HistoryRecords[] = $row;
}
This works when record index[0] does not have double quotes, but when double quotes, errors out.
Interestingly, if i use:
$HistoryRecords[] = json_encode($row);
RESULT>>SyntaxError: missing ) after argument list
then it gets past the error; but I am having trouble usind json_decode to get any useful information out of the array [not even getting js braces or anything, getting another record from another table, possibly because this value is not what it thought it was going to be/either way, not sure i am even starting down the right road with json_endode/decode.]
it could be i simply do need help learning how to decode each record coming out of the json encoded var. either way, does this problem look familiar, and any ideas for how to stop getting the error? NOTE: ultimately, i need to display the array information via javascript. other related posts do not seem to cover this scenario. these double quotes are proving maddening. thanks in advance!
thx, All for helping!
the SYMPTOMS included the [a]browser crashing with console/js error: "missing ) after arg list", [b]a var in the code to display an erroneous field, unrelated to the sql used to create info for that var.
the REASON this occurred was that i had within PHP, a console.log that contained a PHP variable. so i was mixing js and PHP. the PHP was firing late and so at the time, the js console.log had nothing to display.
the CLUE which led to fixing this was noticing that the js msg was crashing in the browser, YET the php LOG_IT stmts below the js were working. the js was trying to console.log a msg that had a null value, and somehow a js var was getting loaded with wrong information. sheeze.
the CODE - before the fix the php echo from within the console.log was the culprit. i tossed the original offending stmt, but it was something like this:
<?php here in php land....., now i want to do a console.log....
?><script>console.log("show me info!: " <?php echo $info; ?></script><?php
...back in php land...
i commented out the "echo $info;" and things worked. of course i ripped it out altogether. funny, it would've cleared itself up when i stopped banging my head on the wall and simply decided to clean up and remove all the troubleshooting console.logs.
the CONCLUSION - an old lesson, "don't mix js and php", even in error logging!
thx again for all your help!
I'm trying to make a dynamic menu in my web, in which only some pages from each section will appear.
The code I wrote was:
$menulist=array();
$menulist[1]='file1%#16';
$menulist[2]='file2%#9';
$menulist[3]='file3%#19';
$menulist[4]='file4%#8';
$menulist[5]='file5%#13';
$menulist[6]='file6%#14';
$menulist[7]='file7%#10';
$menulist[8]='file8%#23';
$menulist[9]='file9%#19';
$menulist[10]='file10%#18';
$menulist[11]='file11%#12';
function actualizaciones($matriz)
{
$linea=explode("%#",$matriz);
echo '<li><a href="first_chunk_of_URL'.$linea[0].'middle_chunk_of_url'.$linea[1].'last_chunk_of_URL">'.${$linea[0]}[$linea[1]].'</li>;
}
echo '<ul>';
array_walk($menulist,'actualizaciones');
echo '</ul>';
Every $linea[0] string is the name of another array (not shown in this code) which contains the text that should be in every possible link corresponding to every key passed by $linea[1].
I must have done something wrong, because the hyperlinks work fine but there's no text showing on them.
use the simple character like below
echo '<li><a href="first_chunk_of_URL'.$linea[0].'middle_chunk_of_url'.$linea[1].'last_chunk_of_URL">'.${$linea[0]}[$linea[1]].'<li>';
and the problem in your code is
.'</li>;
^^^^^
here is the problem it should be
.'</li>';
If I'm reading the question right, you're asking how to use variable variables in PHP.
This can be done using the double-dollar syntax - ie $$linea[0]. See the PHP manual for more info: http://uk.php.net/manual/en/language.variables.variable.php
But if that is what you're doing, I would say you're not writing good code: if variable variables are involved, there's almost always a better way of doing it.
Can't really offer much better assistance here without understanding more about what you're trying to do, but it sounds like you should be using subarrays rather than separate named variables for everything.
Hope that helps.
So I'm trying to do something extremely simple, and after reading through forums, and researching on google I still can't figure out why this is not working. But this is mostly like because I'm still a very much noobie programmer. I'm trying to send information through a url, and having a script pick it up using the $_GET super global.
Here's the link code, in a file called TESTFORM.php:
<p>
Here's a link:
ID
</p>
This is the TESTGET.php script:
<?php
if (isset($_GET['id']))
echo 'it is set<br />';
else
echo 'it is not set<br />';
?>
This yields in a "It is not set" appearing on the page every time. Any thoughts? Are there ghosts in my computer ruining my code? Thanks for taking the time to read through this! Happy coding!
I'm no PHP programmer, but I do know from HTML that computers (especially file names) don't "like" spaces. Try removing the spaces in the id = 5 code.
Your problem is the extraneous space here around the URL parameters:
ID
That will result in PHP seeing the parameter as $_GET["id_"]. The space gets converted into an underscore.
It's always best to use var_dump($_GET); or var_dump($_REQUEST) when you run into such problems. Secondarily it is sometimes helpful to get rid of isset in such cases. Albeit you have a custom error message in place of the language notices intended just for that.
Have you tried to remove spaces in your link?
ID
Code seems fine at a glance, have you tried removing the spaces in
?id = 5 to ?id=5
I'm probably blind to my own mistake again, but why doesn't 2011 become the default selected year? (The -- are for debugging, they show up that the if statement works correctly.)
for($year=1900;$year<=2050;$year++) {
if ($year==date("Y"))
echo "<option value='".$year."' selected='selected'>--".$year."</option>";
else
echo "<option value='".$year."'>".$year."</option>";
}
The selected='selected' should just work. (link) So it is either a stupid mistake(i really don't see one) or some effect from php..
UPDATE:
found the problem.. the ftp didn't overwrite the file properly. Now it works. Thanks for the fast help, when these things happen I always start doubting my own sanity. (cant send this as answer because i have a low rep)
PHP has no effect on the browser behaviour, so if it doesn't work, it is a stupid mistake in the HTML output. ;)
Best thing is checking if the selected variant is actually outputted. Could be that the comparison evaluates to false, so it's always the second line. A little debugging should point out fast what is wrong. Check the output in the browser or var_dump the values of $year and date('Y') to see if they return what you expect.
At first sight I see no error here. Do you have any Javascript that might influence the selection?
CTRL + SHIFT + R if you use firefox.
I had that problem very often too ..
It should be just the cache.
Btw.: You should rather use double quotes for html attributes. That's more "usual". :)
Are you checking the generated HTML? Maybe this code works well (it seems it does), but the problem is in select, or in another place.