Forum Moderators: coopster

Message Too Old, No Replies

question from others who understand php and databases

         

Simone100

4:59 am on Dec 31, 2006 (gmt 0)

10+ Year Member



Hello, can actual php functions be pulled from a database and work?

I tried pulling out a small one inside other php that pulls items out of a database but pulling a php function out instead of just text didn't work. Can it be done? For instance how do people fill out new forms and such if a certain something is triggered and so on?

Thank you very much.

eelixduppy

5:05 am on Dec 31, 2006 (gmt 0)



>>>if a certain something is triggered

Are you talking about conditional control structures [us3.php.net]? Something like this:


if($name == "Bob") {
//do bob-related task
}
else if($name == "Smith") {
//do smith=related task
}

[edited by: eelixduppy at 5:28 am (utc) on Dec. 31, 2006]

Simone100

5:19 am on Dec 31, 2006 (gmt 0)

10+ Year Member



Thats exactly what I mean. And if its bob how to send a special small form just for bob to fill out and so on.

Simone100

5:27 am on Dec 31, 2006 (gmt 0)

10+ Year Member



Heres the idea. I have items pulled from a database and put into a form field in the php. {
print "<input type='text' value='$row[rowforfield1]' size=12/>
}

So on the web page there is the form field with text from the database already in it. How can I change that form field to something else and then add it back into the database. I don't know how to
take the print command above and make it into a query insert.

eelixduppy

5:36 am on Dec 31, 2006 (gmt 0)



Oh, ok. I see now :) Something like this should work:

//connect to database
if(empty($_POST['new_value'])) {
//get info from database: ie SELECT * FROM table
echo "<input type='text' value='".$row[col_name]."' size=12 name='new_value' />";
exit();
}
$new_value = $_POST['new_value'];
$query = "UPDATE table_name SET col_name = '".mysql_real_escape_string($new_value)."' WHERE some_col = 'something'";
//run query

Now you must look at the WHERE clause in the above query. It should only match ONE row so you have to decide how you are going to do that. I'm not sure if you have a unique id for each row, or you want to base it on something else? But whatever you choose, make sure that a user cannot change who the field changes for, otherwise you are creating a security issue.

I hope this explains something :)

Good luck!

Simone100

7:16 am on Dec 31, 2006 (gmt 0)

10+ Year Member



Your a pal! Thanks! :) It even worked including a form field in the print command with the database fields in the form and then resending it with an form action. I'll keep your stuff in case I need that approach later instead.

eelixduppy

2:08 pm on Dec 31, 2006 (gmt 0)



>>>resending it with an form action

That's pretty much what I had in mind, just didn't write it all out

Glad you got it! :)

Simone100

8:54 pm on Jan 2, 2007 (gmt 0)

10+ Year Member



Hey Elix, please look at my newest question on PHP self commands in this forum. Hoping you can help. Simone.