last day (14 days later) » 

4:56 AM
0
Q: use string for select statement in database pdo php

I- -IThis is just a thought i have for my project is it possible to use a string for example an echo as parameter for select statement in database query? here is the scenario: For example when my page loads i automatically display the latest data in database say last five entry, as echo of course. an...

 
ok sir i will post it please wait for update
 
You could pass some variables to the page as part of the URL (or turn it into a form and use POST instead if more complicated) - e.g. somesite.com/details.php?table=the_table_you_want etc. Then process these variables by first accessing them using $_GET['table'] and check them for expected values using, for example, PHP's filter_var() method. Then use bindParam() to add to your PDO statement and away you go.
 
@adey_888 is i will use POST i need name right?since im using echo which has no name how can i get the value of the parameter?
 
You'd need to code it into your POST form as a hidden field e.g. in your code echo "<input type='hidden' name='table' value='$the_table_you_want'>";. I think a form might be overkill and putting the info in the URL will be fine. You might want to use AJAX and load the page output into a <div> on the first page if you don't want the URL to change/get ugly.
 
@adey_888 actually im using ajax to load pages and the reason why i dont use input is to avoid the ugly look...i will try to use this kind of approach i havent tried it so i might fail to achieve what i want be back on you on this one
 
4:56 AM
You'll then be able to access this on the page crud.php using $_POST['table'] or $_POST['searchwhat'] or whatever you decide to call the variable.
If you want to use a form with AJAX then you'll probably need to use something like onSubmit="yourAJAXfunction(); return false;"
 
i will try to do this please look at this post for update if i made..i may ask for suggestion if i wont make it thank you
@adey_888 i tried your approach i put the hidden input then put a name to it but i always get undefined index do you think it has something to do with the hidden type?maybe it is undefined because it is hidden?
 
Can you update the form HTML in your question and also provide the lines of the code in the page that is being called by the AJAX request? They name of the form input matches what is being asked for in the page that is called, right? You can use if(isset($_POST['searchwhat'])) $searchwhat = $_POST['searchwhat']; to check that POST variable is set/defined.
also, look at php.net/manual/en/function.filter-input.php - it returns FALSE if the filter fails, or NULL if the variable_name variable is not set
 
@adey_888 i did the isset now it is not defined will be back on you for next update
now im getting undefined variable from the variable i got from crud.php
 
hi - we've burned through quite a few comments back and forth so doing the chat thing
have you updated your form HTML in the question and provided a snippet of the crud.php code that tries to get the POST variable?
 
wait i havent i will update it now
 
5:03 AM
thanks - let me know when it's done and I'll take a look
 
its done sir
 
ok - I'm refreshing the page and will take a look
there isn't a hidden input in the form - what's the name of the variable you're trying to access (but throws an error because it's not defined)?
In between your <form ..> and </form> tags you will need some code similar to: echo "<input type='hidden' name='searchwhat' value='$value['searchwhat']'>";
using a hidden input is is how the variable is passed to crud.php using POST
is all this code in one file (crud.php) and the whole page is being reloaded? I thought you said you were using AJAX but I don't see any Javascript anywhere!
Also, how is the form being submitted? there is no submit button and I can't see any anchors or otherwise - unless this is code retrieved from the database?
In order for the page to read the variable it first needs to be declared in the form. You'll need two SQL queries: 1) one to retrieve the names or IDs of all the events for use in the form and 2) another query to fetch all the details relating to an event that is requested via the form
 
5:29 AM
please look again i put wrong code
please look again
 
ok - looking again...
in the first form, what might "$value['searchresultwhat']" look like?
 
5:46 AM
its ok sir its an echo that has anchor when i click it i go to another page there i want to show the other details of that event..
 
i am really struggling to understand how your code works
 
but i cant show the event unless i can properly query the database using that string as parameter\
im sorry it is a bit unclear to you
i will remake the question i will properly order the codes
 
ok, so I do not think that you are actually using the form to submit?
can you give an example of what is causing the new page to load
the anchor href="#" and there is no onClick() so I'm not sure how anything is being loaded from the first page at the moment
 
$(document).on('click','#searchresultwhat',function() {
$('#middlecolumncontainer').load('pages/upcomingeventscontent.php');
$('#leftcolumncontainer').load('pages/upcomingeventsmenu.php');
alert($(this).text());
});
this is the one that loads the page sir
when i run it the result is an image with no image then the character even
 
so you're using jQuery? It would've been useful to know that earlier
 
5:53 AM
yes sir..sorry if i didnt mention it before
 
so if you just pass string as the URL to $.load() it will assume that GET is being used to pass data to the page
not POST
which is why your variable is not being passed through
and is not set when you checking with isset()
 
what is the best approach for this problem sir?do i need to just use input?i dont want to use input because it does not look good
i change it to this `LoadSpecificEvent();
echo '<div id="upcomingeventcontentimage">';
echo "<img src=\"admin/".$events[4]."\">";
echo $events[0];
echo $events[1];
echo $events[2];
echo $searchresultwho;
echo '</div>';
echo "<br/>\n";
echo '</div>';`
but i get undefined variable in $searchresultwho;
 
there are lots of ways to do this
one way might be to not use $(document).on('click','#searchresultwhat',function() { ...
 
how then?
i mean what will i use to load the page?
 
but instead use a function like:
function getEventInfo(eventID) {
$('#middlecolumncontainer').load('pages/upcomingeventscontent.php?eventID='+even‌​tID);
...
}
then in upcomingeventscontent.php
some code like:
$eventID = filter_input(INPUT_GET, 'eventID', FILTER_SANITIZE_EMAIL);
or
if(isset($_GET['eventID ']) {
$eventID = filter_var($_GET['eventID', FILTER_SANITIZE_EMAIL]);
then use $eventID in the SQL statement using $stmt->bindParam();
you can call getEventInfo() from the anchors:
<a href="#" onClick="getEventInfo(<?php echo $eventID; ?>); return false;">click me for eventID: <?php echo $eventID; ?></a>
I've made $eventID up, but you should get the idea
 
6:09 AM
i see i will change the code where i would load the page by the code above?let me try it i will work on it now and be back on you asap
 
that's ok - take your time. i am going to be unavailable for maybe an hour or two
 
ok sir but please take a look at this when you are available i will wait for you
 
if you want to use POST because you have more than a simple string to pass to the page, you should look at api.jquery.com/jquery.post
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
where data contains your POST data
but it's probably not necessary for what I think you are trying to do
 
will try that as well sir
 
 
2 hours later…
7:55 AM
hi, how's it going?
have you managed to make progress?
 
unfortunate no..my got all messed up i have to redo from the start i keep having error in the function
so i had to do it from start again
$('#middlecolumncontainer').load('pages/upcomingeventscontent.php?eventID='+even‌​tID);

...
} this part is confusing me
 
it's using a function instead of $(document).on('click'...
the function is called from the anchor: <a href="#" onClick="getEventInfo(<?php echo $eventID; ?>); return false;">click me for eventID: <?php echo $eventID; ?></a>
 
i see that is why i cant run it i did do that on anchor
let me try i again
where eventID is id of anchor right?
 
where eventID is the id of the event you're going to get the details for
SQL example:
SELECT eventID FROM events WHERE ...
[create HTML code using PHP for results]
[user clicks link/anchor/URL]
[Javascript is called and passes eventID to page as variable which then runs another SQL to get the details and returns via AJAX]:
SELECT * FROM events WHERE eventID = $event; // don't really use $eventID, use prepared statement and bindParam()
"eventID" is just a made-up example I've used as a demonstration
 
8:11 AM
yes im working on it now i hope i can make it
please bear with me
im sorry i can make your approach run i think i will just stick with my old code and jjust find a work around i am not really familiar with the way you code..i can make it
no matter how hard i think about i cant make it run when i put alert nothing is happening
 
ok no worries, i guess it just wasn't meant to be today!
best of luck with the project
 
thank you for your support and i am so sorry i have wasted so much of your time with out a good outcome again i am so sorry and thank you so much
 

last day (14 days later) »