Form Crashes!!

Permalink 1 user found helpful
I'm lost and really don't know where to turn, I'm trying to make a simple form under a subdirectory and I'm having a massive problem, or at least a massive problem in my eyes...

The reporting system uses PHP and MYSql, it worked fine on my WAMP but since making it live sometimes it wont write to the tables but has no problem connecting to the database, it doesn't allow anyone to insert data.

I think it's because multiple people are trying to enter data at the same time now, although sometimes I'd have the problem while entering info on the WAMP, I'd have to refresh before being able to enter more data.

Is there a way to handle the traffic? I started searching for "deadlock" and found this:

$retry=0;
$notdone=TRUE;
while( $notdone && $retry<3 ) {
try {
$transaction->begin();
$do_all_the_transaction_stuff();
$transaction->commit();
$notdone=FALSE;
} catch( Exception $e ) {
// here we could differentiate basic SQL errors and deadlock/serializable errors
$transaction->rollback();
$retry++;
}
}
if( 3 == $retry ) {
throw new Exception('Try later, sorry, too much guys other there, or it's not your day.');
}

I tried to implement it, but it ended up sending 11,000 + copies to the database without fail.

How do I use this? I have four fields i'm trying to enter data into.

Talk about frustration, I've been reading and testing stuff for two weeks now with no solution, the closest being the 11,000 entries into the database.

Anyway Thanks in advance for any and all help,

To be honest I don't even know if I'm on the proper track or not...

Rob

crob09
 
mnakalay replied on at Permalink Reply
mnakalay
Hello,

I have 2 questions:
1- are you using Concrete5 database helper for saving the data?
2- Why use a transaction? each user represents only one set of data I assume, transactions are really when you are saving several sets of data at once. In this case I don't think it applies.

Even if many users are saving at the same time, they are all in a different session and saving at the same time shouldn't be an issue. Just have a look at the included form add-on, it sends emails and saves data and it doesn't matter if many users use it at the same time.

Maybe you could show us more code?
crob09 replied on at Permalink Reply
crob09
Hi and thanks for getting back to me.

No I'm not using the database helper, I use phpmyadmin in Hostgator to generate the database.

I don't know what the "Transaction" you are speaking of is part of, sorry kinda new to this.

Here is my send page that's throwing the error:

// Get values from form
$email=$_POST['email'];
$address=$_POST['address'];
$report=$_POST['report'];
$robotest = $_POST['robotest'];
$email = htmlspecialchars($_POST['email']);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
die("E-mail address not valid");
}

if($robotest)
{
$error = "You are a gutless robot.";
}
if (strlen($report)<10)
{
die("You must enter ten or more letters into the report section");
}
if (strlen($city)<4)
{
die("You must enter four or more letters for a city or town");
}
if (strlen($company)<4)
{
die("You must enter 4 or more letters for a company name");
}
else
// Insert data into mysql
$sql="INSERT INTO $tbl_name(email, company, city, address, report)VALUES('$email', '$company', '$city', '$address', '$report')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful, thank you for the entry please return to the last page";
echo "<BR>";
echo "<a href='index.php'>Return to the last page</a>";
}

else {
echo "ERROR somethings wrong please refresh and try again soon if this continues contact the editor";
}
?>


Thanks for the help.
crob09 replied on at Permalink Reply
crob09
How do I use the "Concrete5 database helper"?

Do you see anything wrong with the code provided?

Thanks,

Rob
mnakalay replied on at Permalink Best Answer Reply
mnakalay
Hi again,
there are a few things wrong with the code indeed:

You have these 2 lines at the beginning
$email=$_POST['email'];
and
$email = htmlspecialchars($_POST['email']);

delete the first one it's useless because of the second one

Then you have
if (strlen($city)<4)
{
die("You must enter four or more letters for a city or town");
}
if (strlen($company)<4)
{
die("You must enter 4 or more letters for a company name");
}

But neither $city nor $company are defined here. So if they're not defined elsewhere, using strlen to check on the length of their value will always return a 0 (so <4) so an error

Then you try to insert that data in your database
$sql="INSERT INTO $tbl_name(email, company, city, address, report)VALUES('$email', '$company', '$city', '$address', '$report')";
$result=mysql_query($sql);


$tbl_name is also not defined anywhere in the code you provided so nothing will ever get saved and you will get an error since the code has no idea where to save the data.

Here are a few links to help you understand C5 database and form helper.
http://www.concrete5.org/documentation/developers/forms/standard-wi...
http://www.concrete5.org/documentation/introduction/database-access...

But frankly, I think you might want to consider hiring someone to do it for you or stick to using existing concrete5 add-ons. For instance, if you are just trying to save data from a form, why not just use the form add-on that comes with C5? it will do just that in an easy to use way.
crob09 replied on at Permalink Reply
crob09
Thank you for the help, it's always helpful to have a second set of eyes (and a little rest too).

I see the errors you pointed out and made the changes as noted, can't believe I did that!

Anyway: this statement..

$sql="INSERT INTO $tbl_name(email, company, city, address, report)VALUES('$email', '$company', '$city', '$address', '$report')";

$tbl_name is defined in the brackets that follow, the values are defined after that.

So yup some stuff wasn't defined and I had the $email as a variable twice(bad me).

I think for teaching myself this stuff, I'm doing alright, besides I don't have the funds to hire someone and this is a hobby, nothing else.

Thanks again for all the help.
mnakalay replied on at Permalink Reply
mnakalay
Well actually if it's a hobby and you can afford to take your time and do it yourself, it's absolutely great and you should definitely do it. I can't think of a better way to learn.
Good luck with all that.
crob09 replied on at Permalink Reply
crob09
I'm still having trouble and hope you can help a little more, here is my code so far, I included the variables and removed the extra $email.

It still crashes and shows the error message.

$db_name="db name"; // Database name
$tbl_name="my table"; // Table name


// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$city=$_POST['city'];
$address=$_POST['address'];
$report=$_POST['report'];
$company=$_POST['company'];
$robotest = $_POST['robotest'];
$email = htmlspecialchars($_POST['email']);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
die("E-mail address not valid");
}

if($robotest)
{
$error = "You are a gutless robot.";
}
if (strlen($report)<10)
{
die("You must enter ten or more letters into the report section");
}
if (strlen($city)<4)
{
die("You must enter four or more letters for a city or town");
}
if (strlen($company)<4)
{
die("You must enter 4 or more letters for a company name");
}
else
// Insert data into mysql
$sql="INSERT INTO $tbl_name(email, company, city, address, report)VALUES('$email', '$company', '$city', '$address', '$report')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful, thank you for the entry please return to the last page";
echo "<BR>";
echo "<a href='index.php'>Return to the last page</a>";
}

else {
echo "ERROR somethings wrong please refresh and try again soon if this continues contact the editor";
}
?>

<?php
// close connection
mysql_close();
?>

It works for a little while, I managed to enter four things today before this problem.
mnakalay replied on at Permalink Reply
mnakalay
Your problem is that you are not using C5, you are using normal PHP so bypassing the whole C5 environment.

For instance you have the whole piece of code dealing with connecting to the database.
C5 does that, as soon as you navigate to a C5 page, there is a connection to the database. The settings for the database were saved when you installed C5.

Second problem is you are putting your whole logic for dealing with the form data in your page it seems. C5 works following what we call MVC. In short it means that in the file rendering your page (view.php or default.php or...) you only put presentational stuff mostly and all the heavy lifting, processing... is done in a file called controller.php

In your case you need to create a block. The block will have a file db.xml that will define the structure of the database table you want to use to save your data. When you install your block, C5 will read db.xml and automatically create the table in the database. After that your block will always know where to find and save the data.

The block will also have a file view.php in which you will generate the form with all its fields. view.php is what will get inserted in your page when you add the block to it. it is what users will see. The action of the form will call a function which is in the next file you need to know about controller.php

In controller.php you have all the logic. You have the function called when submitting your form and dealing with all the data checking and saving. You also have C5 functions that allow you to do stuff when the block is viewed or things like that.

What I"m trying to say is that if you use C5 you have to learn to use its structure and model, you can't just use PHP the way it's done independently.

You should download an add-on called Designer Content. it allows you to create blocks in an easy way. Create a few blocks and have a look at the code generated, you will see how easy it is to follow.
crob09 replied on at Permalink Reply
crob09
Sorry I did miss this response, and I'll look at the meta data file you mentioned and how to create a block.
I wasn't sure if that was the avenue I wanted to take because I wasn't sure if I wanted this to be a separable thing.
That's why it's under another directory, I was even thinking about creating an Add0on domain for it.
Compaired to people like you I'm a little dumb, but I'm trying, hope I don't annoy you too much with my ignorance and hope you can help me find a solution.
I do think this would be better if I could just move it wherever I want and just have to change some database values.
Should I be making an error handle and DB connect file outside of the for processing page, just use PHP include?
Also is the "If" statement I'm using the right way to go opposed to a While loop?

Thanks,

Rob
crob09 replied on at Permalink Reply
crob09
Should I be using a While loop or something? I just don't see what I'm doing wrong... maybe I need to change a value using PHPMyadmin?
mnakalay replied on at Permalink Reply
mnakalay
Hi again,

what happens exactly now? What kind of error messages are you getting?

Did you read my previous answer?
crob09 replied on at Permalink Reply
crob09
sure did read your last response and solution, hence why I posted the code again with the changes I made.
For now I'm trying to get it working the hardway with the tables and DB I made using Hostgator tools, just wondering if I need to set the fields as a Blob or something other than Text or Varchar.

The error is the one I defined here:

else {
echo "ERROR somethings wrong please refresh and try again soon if this continues contact the editor";
}

So it's skipping the insert part but still connecting to the DB, I can insert some things but when I cut and past data I get the error.

Thanks for the time and help,

Rob
mnakalay replied on at Permalink Reply
mnakalay
You don't need to use while, the if here are just fine.
There can be a number of errors and we don't know which because it gives us a formatted error message.

Instead of
$result=mysl_query($sql);

write
try{
mysl_query($sql);
} catch (Exception $e) {
return false;
}


and then see what error message it is giving you.

But once again I have to emphasize that this is not the right way to do it and that you are going to run in a lot of trouble including security problems.
crob09 replied on at Permalink Reply
crob09
Thank you for the help I'll try that now and let you know what the
outcome is soon.
Also thanks for warning me about the potential problems I may run into
doing things this way, I'll probably end up doing it the proper way
later.
To be honest I'm a little stubborn and will never rest until I know
what I've done wrong here, I know that's dangerous because it just
leads me further down the Rabbit Hole, one problem always leads to
another.
Yet here people have worked hard to make a CMS that's easy to use and
secure, and here I am re-inventing the wheel, anyway truly thanks for
the patients and guidance.

Rob


Quoting concrete5 Community <discussions@concretecms.com>:
mnakalay replied on at Permalink Reply
mnakalay
You're welcome. Good learning and good luck.
crob09 replied on at Permalink Reply
crob09
So I made those changes and did encounter a new error message so I
googled it, it pointed toward an PHP configuration problem. So I
contacted Hostgator and they returned saying the error was caused from
a missing letter. I missed the letter "Y" in this "mysl_query($sql);",
meaning it was written like this "msl_query($sql);".

So I fixed that and now still receive the exact same error "ERROR
somethings wrong please refresh and try again soon if this continues
contact the editor".

Stumped, I've been searching and trying things but nothings working,
any other suggestions?

Thanks again for all the time and help,

Rob

Quoting concrete5 Community <discussions@concretecms.com>:
crob09 replied on at Permalink Reply
crob09
Here is the code so far...


<?php

$host="isthehost"; // Host name
$username="theuser"; // Mysql username
$password="mypass"; // Mysql password
$db_name="Report"; // Database name
$tbl_name="canadabad"; // Table name


// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$city=$_POST['city'];
$address=$_POST['address'];
$report=$_POST['report'];
$company=$_POST['company'];
$robotest = $_POST['robotest'];
$email = htmlspecialchars($_POST['email']);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
die("E-mail address not valid");
}

if($robotest)
{
$error = "You are a gutless robot.";
} else

try{
mysql_query($sql);
} catch (Exception $e) {
return false;
}

// Insert data into mysql
$sql="INSERT INTO $tbl_name(email, company, city, address, report)VALUES('$email', '$company', '$city', '$address', '$report')";
$result=mysql_query($sql);



if($result){
echo "Successful, thank you for the entry please return to the last page";
echo "<BR>";
echo "<a href='index.php'>Return to the last page</a>";
}


else {
echo "ERROR somethings wrong please refresh and try again soon if this continues contact the editor";
}
?>

<?php
// close connection
mysql_close();
?>