DATA insert in mysql from form using PHP

Hi Everyone,

I have been busting my brain a few days over this and I can't seem to find the error.
Any help would be appreciated.

I have a form I am getting data from and this is my php file to send the data to the database.

All I get to is hello 2.

Any help would be appreciated please.

//define db
$servername = "localhost";
$username = "root";
$password = "XXXXXX";
$dbname = "self";
echo "hello 1";
//Create Connection
$conn = mysql_connect($servername, $username, $password, $dbname) or trigger_error(mysql_error(),E_USER_ERROR); 
//Check connection

      $date = $_POST['date'];
	  $am_time = $_POST['am_time'];
	  $am = $_POST['am'];
	  $am_comments = $_POST['am_comments'];
	  $noon_time = $_POST['noon_time'];
	  $noon = $_POST['noon'];
	  $noon_comments = $_POST['noon_comments'];
	  $pm_time = $_POST['pm_time'];
	  $pm = $_POST['pm'];
	  $pm_comments = $_POST['pm_comments'];
	  echo " hello 2";
$insert_sql = "INSERT INTO diabetes(date, time_am, am, time_noon, noon, time_pm, pm, am_comments, noon_comments, pm_comments)

Values('$date', '$am_time', '$am', '$am_comments', '$noon_time','$noon','$noon_comments','$pm_time','$pm','$pm_comments')";
echo "$insert_sql";
mysql_query($conn, $insert_sql)

or die(mysql_error());
 echo "One Record Added";


Open in new window

Thanks in advance
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
The term "hello 1" should appear before "hello2" but that does not appear in your output?  Please check - did you post the correct script?

Set aside what you're doing and learn why MySQL is going away and what you must do to keep your scripts running.  Nobody should try to use the MySQL extension any more!

The standard design patterns for MySQL table maintenance are shown in this article.

If you're new to PHP and want to learn the language, this article can help steer you toward trustworthy learning resources.

PHP has some documentation that teaches about this.
nemws1Database AdministratorCommented:
You're not sanitizing your input.  Are there a single quotes in any of your "comments" variables (am/pm/noon)?  If so, then your insert won't work (your single quotes will no longer match).  Although your approach is simple and often works, you have to worry about your input too much.

Check out this article on using placeholders with MySQL/PHP, which will automatically sanitize your inputs and make multiple inserts *much* easier.
Ray PaseurCommented:
We can give you a better answer if you can post the script that contains the HTML form, as well as the CREATE TABLE statement for your diabetes table.  When you post those things, please use the Code snippet feature.  Just click Code in the formatting bar above this comment area.
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

dlojAuthor Commented:
Thanks for your comments.  I have actually used mysqli and mysql both and they both fail.  I have used pretty much the same file for another db I have on another website and it works fine.  

When I say I only get to Hello 2 that means that the code is running fine till after hello 2.  

No there are no quotes on any of my variables.  Most of the variables are numbers, the form calls for date time and blood glucose count, and then comments if there are any.  

At the times I have tested this I have only input date and time and blood glucose info, one of the questions I have is if I do not have inpute in all fields but the fields themselves are set to NULL by default would that not allow the insert into the db?  I would think that would not matter.

I am also attaching a screenshot of the diabetes db from phpmyadmin.

Thanks again.

Form below:
<!doctype html>
<meta charset="utf-8">
<title>Untitled Document</title>

I was diagnosed with type 2 diabetes in March of 2015.  The doctor gave me glipizide and really didn't tell me anything else.
So I started testing my glucose count eventually.
Please input your date, time, numbers, and content below.
<form  action="test_results.php" method="post">
Date:</td><td><input style="text" name="date" id="date" size= "10"></td></tr>
Time:</td><td><input style="text" name="am_time" id="am_time" size="7"></td></tr>
Morning BG:</td><td><input style="text" name="am" id="am" size="5"></td></tr>
Comments:</td><td><input  style="text" name="am_comments" size="256" id="am_comments"></td></tr>
Afternoon Time:</td><td><input style="text" name="noon_time" size="7" id="noon_time"></td></tr>
Afternoon BG:</td><td><input style="text" name="noon" size="5" id="noon"></td></tr>
Afternoon Comments:</td><td><input style="text" name="noon_comments" size="256" id="noon_comments"></td></tr>
Evening Time:</td><td><input style="text" name="pm_time" size="7" id="pm_time"></td></tr>
Evening BG:</td><td><input style="text" name="pm" size="5" id="pm"></td></tr>
Evening Comments:</td><td><input style="text" name="pm_comments" size="256" id="pm_comments"></td></tr>
<input type="submit" value="Submit">


Open in new window

Thanks again
Ray PaseurCommented:
Thanks -- this is helpful.  I'm going to bed, but maybe someone else can help this evening.  If not, we will be back in the daylight.  I'm sure we can get you a good answer soon!
The parameters to mysql_query() are inverted.  The proper order is as follows:
mysql_query($sql_statement, $db_connection);
mysqli_query($db_connection, $sql_statement);

Switching between mysqli_x() and mysql_x() is not always as simple as adding/omitting an "i".  Since you are still learning the api, this is the time where you must consult the manual.  In time it will sink in if you continue using the api.
Ray PaseurCommented:
OK, sorry this fell off the edge of the table.  Here is what I recommend.  First, please read this article about how to handle DATETIME values in PHP and MySQL.  You can convert English-language date/time values to ISO-8601 representations of the date with strtotime() and date('c').  This is all in the article.

Next please read this article about using MySQL, MySQLi and PDO.  The examples can mostly be copied and pasted.  Do not use MySQL any more; it is going away.  No need to waste even a minute of your time on obsolete features!

I think any code you write will be easier if you redefine the date and time columns so that they are DATETIME columns, combining the date and time into one value.

I'm guessing that "am", "noon" and "pm" are blood glucose counts, given as an integer, meaning mg/dL.  Is that right?  Your scripts might want to sanitize the inputs and check for reasonability.  For example, an empty field might be misinterpreted as zero, obviously not a valid measurement.
dlojAuthor Commented:
I changed the int field to varchar and things worked fine.  Thanks anyway guys.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dlojAuthor Commented:
My solution worked, it was simple and straightforward.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.