Home > Archive > PHP Language > March 2004 > Function which is not called
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Function which is not called
|
|
| Marcin Zmyslowski 2004-03-26, 11:13 pm |
| I need to put some HTML data into the PHP variable. Here is the code with
error:
----------------------------------------------------------------------------
-----------------
$MIDDLE="<form name=\"formSzukaj\" id=\"formSzukaj\"
ALIGN=\"CENTER\">MENU_L: <input name=\"username\"
type=\"text\" id=\"username\" value=\"$MENU_L\"> <INPUT type=\"button\"
name=\"Submit\" value=\"Zmien\"
OnClick=\"funkcja_update(\"menu\",\"MENU_L\",\"krzeslo\")\"></form>";
----------------------------------------------------------------------------
-----------------
I have one edit field, whose value is put in the function - funkcja_update,
which update me data in SQL database
Function created in PHP shows:
----------------------------------------------------------------------------
-----------------
function funkcja_update($tabela,$pole,$opis)
{
$link_ = mysql_connect("localhost", "database", "password") or die ("Serwer
die");
mysql_select_db ("database") or die ("Baza die");
$query_ = "UPDATE ".$tabela." SET opis='".$opis."' WHERE
pole='".$pole."';";
$result_ = mysql_query ($query_);
}
----------------------------------------------------------------------------
-----------------
How to construct $MIDDLE variable to make it work properly, because now PHP
interpreter doesn`t show any error message,
but update SQL database function is not called???
Please, help me
Thank you in advance
Marcin
| |
| .:Ninja 2004-03-26, 11:13 pm |
| Marcin Zmyslowski wrote:
> I need to put some HTML data into the PHP variable. Here is the code with
> error:
>
>
----------------------------------------------------------------------------
> -----------------
> $MIDDLE="<form name=\"formSzukaj\" id=\"formSzukaj\"
> ALIGN=\"CENTER\">MENU_L: <input name=\"username\"
> type=\"text\" id=\"username\" value=\"$MENU_L\"> <INPUT type=\"button\"
> name=\"Submit\" value=\"Zmien\"
> OnClick=\"funkcja_update(\"menu\",\"MENU_L\",\"krzeslo\")\"></form>";
>
----------------------------------------------------------------------------
> -----------------
> I have one edit field, whose value is put in the function -
> funkcja_update, which update me data in SQL database
>
> Function created in PHP shows:
>
----------------------------------------------------------------------------
> -----------------
> function funkcja_update($tabela,$pole,$opis)
> {
> $link_ = mysql_connect("localhost", "database", "password") or die
> ("Serwer
> die");
> mysql_select_db ("database") or die ("Baza die");
> $query_ = "UPDATE ".$tabela." SET opis='".$opis."' WHERE
> pole='".$pole."';";
> $result_ = mysql_query ($query_);
> }
>
----------------------------------------------------------------------------
> -----------------
>
> How to construct $MIDDLE variable to make it work properly, because now
> PHP interpreter doesn`t show any error message,
> but update SQL database function is not called???
To start off with, the HTML you're trying to put into the variable $MIDDLE
is quite unreadable. Secondly, variables are conventionally only written in
all caps when they refer to globals (and a few other things). So I suggest
using $middle in stead of $MIDDLE. Here's a cleaned up version of your
$middle statement:
$middle = "<form_name='formSzukaj'_id='formSzukaj'
ALIGN='CENTER'>MENU_L:__<input_name='username'
type='text' id='username' value='$MENU_L'> <INPUT type='button'
name='Submit' value='Zmien'
OnClick='funkcja_update('menu','MENU_L',
'krzeslo')'></form>";
OK, now that looks a lot better. However, the execution of your program
suffers from a logistics error. OnClick is a client-side function, while
you're trying to do something server-side i.e. inserting data into a
database. What you need to do is point the form action to a script that
will do the server-side stuff for you. I like to do everything in the same
script, so here's what I think it could look like:
<?
/* I don't know what the variables $opis and $pole are, or where you get
their values from, but here goes... */
function funkcja_update($tabela,$pole,$opis) {
$link__=_mysql_connect("localhost",_"database",_"password")_or_die_("Server
die");
mysql_select_db_("database")_or_die_("Baza_die");
$query_=_"UPDATE_$tabela_SET_opis='$opis'_WHERE
pole='$pole'";
return $result_=_mysql_query_($query);
}
if ($_POST['Submit']) {
# User pressed submit button, update db
funkja_update("var1","var2","var3");
} else {
# We need to display the form
$middle = "<form_action='$_SERVER[PHP_SELF]'
name='formSzukaj'_id='formSzukaj'
ALIGN='CENTER'>MENU_L:__<input_name='username'
type='text' id='username' value='$MENU_L'> <INPUT type='button'
name='Submit' value='Zmien'></form>";
# The form is still not displayed, we need to
# print the value of $middle
print $middle;
}
?>
I have obviously not tested the above. If you still have problems, make sure
that the variable you collect from the form (username) is passed into the
function.
Albe
--
http://www.ninja.up.ac.za
|
|
|
|
|