Not inserting to table???

I have a simple message form that requires a message in a textarea box and if they are not logged in, must
state who they are in a text box. When it submits to itself, then requires chapterpdo5.php to insert, it
doesnt insert. other connections on my site work but not this one. maybe the code? here it is:

chapter.php:

[php]

if (!empty($userid) && $userid != $_SESSION[“id”])
{

if (isset($_POST[“messagesubmit”]))
{

if (!isset($_SESSION[“login”]))

{

if ($_POST[“credentials”] == null || strlen($_POST[“credentials”]) > 25)
{
$_SESSION[“credentialserror”] = “Please identify yourself”;
$_SESSION[“messagesuccess”] = “”;
}

if ($_POST[“credentials”] != null && strlen($_POST[“credentials”]) < 25)
{
$_SESSION[“credentialserror”] = “”;
}

}

if ($_POST[“message”] == null || strlen($_POST[“message”]) > 500)
{
$_SESSION[“messageerror”] = “Please enter your message”;
$_SESSION[“messagesuccess”] = “”;
}

if ($_POST[“message”] != null && strlen($_POST[“message”]) < 500)
{
$_SESSION[“messageerror”] = “”;
}

if ($_SESSION[“messageerror”] == “” && $_SESSION[“credentialserror”] == “”)
{
require “chapterpdo5.php”;
}

}

echo "Message Me: ";

?><form action = “<?php echo $_SERVER["PHP_SELF"]; echo "?id=" . $userid; ?>” method=“post”>

<?php echo $_POST["message"]; ?>
<?php echo "" . $_SESSION["messageerror"] . ""; echo $_SESSION["messagesuccess"]; if (!isset($_SESSION["login"])) { echo "

From: " . "" . "" . $_SESSION["credentialserror"] . "

"; } ?>

[/php]

and for the included chapterpdo5.php:

[php]

<?php session_start(); $userid = $_GET["id"]; if (isset($_SESSION["login"])) { try { $conn = new PDO("mysql:dbname=xxx;host=xxx.db.1and1.com", "xxx", "xxx" ); $sql = $conn->prepare("INSERT INTO Messages (To, From, Message) VALUES ( ?, ?, ?)"); $sql->execute(array($userid, $_SESSION['id'], $_POST['message'])); $conn = null; $_SESSION["messagesuccess"] = "Message Sent" . $userid . $_SESSION['id'] . $_POST['message']; } catch(PDOException $e) { echo $e->getMessage(); $conn = null; } } if (!isset($_SESSION["login"])) { try { $conn = new PDO("mysql:dbname=xxx;host=xxx.db.1and1.com", "xxxxx7", "dontbackbite" ); $sql = $conn->prepare("INSERT INTO Messages (To, From, Message) VALUES ( ?, ?, ?)"); $sql->execute(array($userid, $_POST['credentials'], $_POST['message'])); $conn = null; $_SESSION["messagesuccess"] = "Message Sent" . $userid . $_POST['credentials'] . $_POST['message']; } catch(PDOException $e) { echo $e->getMessage(); $conn = null; } } ?>

[/php]

What is wrong here? thanks

I am happy to see you are using PDO :slight_smile:

Right here:

[php]INSERT INTO Messages (To, From, Message) VALUES[/php]

The word “From” is a reserved word. See: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

A good practice is to always enclose table and column names with backticks.

[php]INSERT INTO Messages (To, From, Message) VALUES[/php]

That would be good coding practice, but when i try doing my un-ticked way on other connections,
it works. so it must be something else in the code disregarding the insert?

It is more than good coding practice when it comes to using reserved words, at least in MySQL. I can guarantee that insert will not work without backticks around From

Are you not getting any exceptions from PDO?

Well what happens is that if you will look in the try…insert statement you will see a message success
string. even though it doesnt insert, it still executes the rest of the try statement and doesnt go to the
catch. it executes what it can in the try even though it wont insert. i will try the thing with backticks but
when i insert to other tables it works without any backticks. why would this be?

Do your other tables contain reserved words as column names?

yea i use the same mysql–pdo connection for all my connections so I always use reserved words. but it works in all other connections but not this one. so thats why im thinking its something in the code before that.

So are you saying that enclosing your table and column names in backticks did not work? Did you try it?

“To” is also a reserved word.

it still does not work so i really boldly believe it is someithing in the code before it? thanks

You’re not getting any errors from PDO? I really don’t see anything else wrong with this code.

I can only assume you have a problem here:

[php]if ($_SESSION[“messageerror”] == “” && $_SESSION[“credentialserror”] == “”)[/php]

or here:

[php]if (isset($_SESSION[“login”]))[/php]

That may be possible. what do you see wrong with those 2 codes you’re pointing out? thanks again

I’m still scratching my head about this bug. this one i cant seem to solve yet. im still here listening for any answers. thanks

any possible solutions?

is that all the code because you are missing an ending bracket here

[php]

echo "

From: " . “” . “” . $_SESSION[“credentialserror”] . “

”;
}

}// right here
echo "Message Me: ";

[/php]

I have narrowed down the search for the solution: I know that the reason it wont insert is
in my included query file, chapterpdo5.php…
[php]

<?php session_start(); try { $conn = new PDO("mysql:dbname=xxx;host=xxx", "xxx", "xxx"); $sql = $conn->prepare("INSERT INTO Messages (From, Message) VALUES (?, ?)"); $sql->execute(array($_POST['message'], "Hello")); $conn = null; $_SESSION["messagesuccess"] = "Message Sent"; } catch(PDOException $e) { echo $e->getMessage(); $conn = null; } ?>

[/php]
When I do this, it doesnt insert. But, when I edit my table to drop the ‘From’ column and insert only 1 column:
[php]

<?php session_start(); try { $conn = new PDO("mysql:dbname=xxx;host=xxx", "xxx", "xxx" ); $sql = $conn->prepare("INSERT INTO Messages (Message) VALUES (?)"); $sql->execute(array($_POST['message'])); $conn = null; $_SESSION["messagesuccess"] = "Message Sent"; } catch(PDOException $e) { echo $e->getMessage(); $conn = null; } ?>

[/php]

When I however execute the query that only inserts to the Message column, then it works. but not when i insert to both From and Message. I dont see why inserting to 2 columns doesnt work, but inserting to 1 column does. If it helps, both the Message and From columns are varchar(30) type columns…

Whats the problem here? Thanks again!

well in the first place i dont see a $_POST[‘credentials’] anywhere in the code is it there and i am not seeing it or ?

Well for the purpose of simplifying the code, I removed the credentials form and simplified the code in the previous page to this:

[php]

if (!empty($userid) && $userid != $_SESSION[“id”])
{

if (isset($_POST[“messagesubmit”]))
{
require “chapterpdo5.php”;
}

echo "Message Me: ";

?>

<?php echo $_POST["message"]; ?>
<?php echo "" . $_SESSION["messageerror"] . ""; echo $_SESSION["messagesuccess"]; ?> <?php } [/php] and from there in chapterpdo5.php, I do the 2 queires that i showed you in my last response. The first one doesnt work and the second one does. So the problem is in that query not in the code before it. It would be highly appreciated to point out the flaw in my previous response. thanks so much

well we can see if the actual credentials is making it to the chapterpdo file
i have made a few changes to your file to see if the stuff is going out
http://crap.ezewh.com/messsub.php

[php]<?
session_start();
if (!empty($userid) && $userid != $_SESSION[“id”])
{

if (isset($_POST[“messagesubmit”]))
{

if (!isset($_SESSION[“login”]))

{

if ($_POST[“credentials”] == null || strlen($_POST[“credentials”]) > 25)
{
$_SESSION[“credentialserror”] = “Please identify yourself”;
$_SESSION[“messagesuccess”] = “”;
}

if ($_POST[“credentials”] != null && strlen($_POST[“credentials”]) < 25)
{
$_SESSION[“credentialserror”] = “”;
}

}

if ($_POST[“message”] == null || strlen($_POST[“message”]) > 500)
{
$_SESSION[“messageerror”] = “Please enter your message”;
$_SESSION[“messagesuccess”] = “”;
}

if ($_POST[“message”] != null && strlen($_POST[“message”]) < 500)
{
$_SESSION[“messageerror”] = “”;
}

if ($_SESSION[“messageerror”] == “” && $_SESSION[“credentialserror”] == “”)
{
require “chapterpdo5.php”;
}

}
}
echo "Message Me: ";

?><form action = “<?php echo $_SERVER["PHP_SELF"]; echo "?id=" . $userid; ?>” method=“post”>

<?php echo $_POST["message"]; ?>
<?php echo "" . $_SESSION["messageerror"] . ""; echo $_SESSION["messagesuccess"]; //if (!isset($_SESSION["login"])) //{ //echo "

From: " . "" . "" . $_SESSION["credentialserror"] . "

"; echo "

From: {$_POST['credentials']}{$_POST['message']}

"; //} ?> <? ?>

[/php]

it might be cause it is logging in

if (!isset($_SESSION["login"]))
Sponsor our Newsletter | Privacy Policy | Terms of Service