SimplePortal
Support => English Support => Topic started by: ag2ieric on February 19, 2010, 10:15:19 AM
-
Hi,
i'm facing a problem that i couldnt succeed in solving by myself nor with google help ;) and i hope someone could help me here.
I want to make a simple mysql insert from a custom php block
i use
- a form
- with an input text
- at submit, i try to store the text in a table
Seems simple, but I face a double problem :
In dev env (WAMP+Win XP) :
when i use a db_query(...) function, it says no db select --> can't find how to select the db
when i use a direct sql insert function (mysql_query), it works
Now In final prod env (server) :
direct sql saying "database error in code block' .
when i use a db_query(...) function, i got the same "database error in code block'.
--> it won't validate the new block.
I would really appreciate to have clues to solve the problem, a sample code or some tip to make this simple insert working ! thank in advance
EDIT:
I use this code in the php block that seems to work fine:
global $boarddir;
require_once($boarddir . '/../my_funcs.php');
mycustomfunc();
My FIRST TRY : not working: (error = no database selected)
mycustomfunc()
{
global $ID_MEMBER, $user_info, $db_connection,$db_prefix,$context;
if (isset($_POST['question']) && !empty($_POST['question']))
{
$question = strip_tags($_POST['question']);
$request = db_query("
INSERT INTO {$db_prefix}faq
VALUES (NULL, '".$question."', $ID_MEMBER),", __FILE__, __LINE__);
if ($request) {
echo "votre question à été posée. Vous recevrez une réponse sous peu.";
echo '<a href="#" onclick="history.go(-1)">Retour</a>';
}
else
{
echo "Erreur de base de données.";
}
}
else{
echo '
Poser votre question:
<form action="" method="post" accept-charset="', $context['character_set'], '">
<input type="text" name="question" id="question" >
<input type="submit" value="Poser" >
</form>';
}
}
My SECOND TRY : working in dev env but refused in final prod env (error = database error in block code)
mycustomfunc()
{
global $db_connection;
global $context, $settings, $scripturl, $ID_MEMBER;
global $db_name, $db_prefix;
if (isset($_POST['question']) && !empty($_POST['question']))
{
$question = strip_tags($_POST['question']);
if (!mysql_select_db ($db_name)) echo "BDD non sélectionnée.";
$sql = "INSERT INTO {db_prefix}faq (faq_id, question, ID_MEMBER)
VALUES (NULL, '".mysql_real_escape_string($question)."', $ID_MEMBER)";
$request = mysql_query($sql) or die('Erreur SQL !'.$sql.'<br>'.mysql_error());
if ($request) {
echo "votre question à été posée. Vous recevrez une réponse sous peu.";
echo '<a href="#" onclick="history.go(-1)">Retour</a>';
}
else
{
echo "Erreur de base de données.";
}
}
else
{
echo '
Poser votre question:
<form action="" method="post" accept-charset="', $context['character_set'], '">
<input type="text" name="question" id="question" >
<input type="submit" value="Poser" >
</form>';
}
}
-
ok guys, seems i solved my problem
simply check "disable php validation" into the admin panel > SimplePortal > general settings