MYSQL records not being stored

Sorry if this is not a PHP question directly. As stated in my previous post I have created a form and wish to store the values taken from this and store them in the corresponding sections of a database. The form appears to work and I have created a database called “work_stats” with a single table “daily_stats” using phpmyadmin through MAMP.

Here is the code, the form appears to function correctly but the records do not appear and I receive no error messages. I have confirmed the Mysql server is running in MAMP.

<?php 
include ('Applications/MAMP/htdocs/mysqli_connect.php');
 ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>Work Statistics Form</title>
	<style type="text/css" title="text/css" media="all">
	label {
		font-weight: bold;
		color: #300ACC;
	}
	</style>
</head>
<body>
<!-- Script 2.1 - form.html -->

<form action="index.php" method = "post">
	<fieldset><legend>Please enter information in the form below: </legend>

	<p><label>Date: <input type="date" name="date"/></label></p>

	<p><label>Clinic: <br><input type="radio" name="clinic" value ="Durham"/> Durham <br><input type="radio" name="clinic" value ="Newcastle"/> Newcastle <br><input type="radio" name="clinic" value ="Sunderland"/> Sunderland <br><input type="radio" name="clinic" value ="Ashington"/> Ashington <br><input type="radio" name="clinic" value ="Hartlepool"/> Hartlepool <br><input type="radio" name="clinic" value ="Bishop"/> Bishop <br><input type="radio" name="clinic" value ="Stockton"/> Stockton<br><input type="radio" name="clinic" value ="Middlesborough"/> Middlesborough <br><input type="radio" name="clinic" value ="Other"/> Other</p>

	<p><label>Google Miles: <input type="number" name="c_mileage" min="0" max="500"/></label></p>

	<p><label>Actual Miles: <input type="number" name="a_mileage" min="0" max="500"/></label></p>

	<p><label>Appointments: <input type="number" name="appointments" min="0" max="7" /></label></p>

	<p><label>Comments: <textarea name="comments" rows="3" cols="40"></textarea></label></p>	

	</fieldset>

	<p align="center"><input type="submit" name="submit" value="Submit" /></p>

</form>

<?php

if (isset ($_POST[submit])){

DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'root');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'work_stats');

// Make the connection:
$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die ('Could not connect to MySQL: ' . mysqli_connect_error() );

mysqli_select_db("work_stats", $dbc);

$sql = "INSERT INTO daly_stats (date,clinic,c_mileage,a_mileage,appointments,comments) VALUES ('$_POST[date]','$_POST[clinic]','$_POST[g_mileage]','$_POST[a_mileage]','$_POST[appointments]','$_POST[comments]')";

mysqli_query($sql, $dbc);

mysqli_close($dbc);
}
?>

</body>
</html>

Mysql connection part:

<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'root');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'work_stats');

// Make the connection:
$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die ('Could not connect to MySQL: ' . mysqli_connect_error() );
?>

I tried changing all instances of mysqli to mysql and moved the connection code into the main page to test but this had no effect.

You are trying to insert into daly_stats when your table is named daily_stats. Get rid of the @ symbols. You do not want to suppress errors. Also, if you turned on error reporting you would have known exactly what was wrong.

Also, do not use if (isset ($_POST[submit])){
It can completely fail in certain instances. I have gone through it in detail recently on this forum. Search my posts.

You should also be using HTML5.

!IMPORTANT - You are vulnerable to SQL Injections. You never ever send user supplied data directly to the database.

Additionally, there is no need to manually close the connection. Php will close it automatically when the script finishes running.

Also, your post code will fail. You are missing quotes. It used to be ok in older php. Not now

$_POST[[size=12pt]’[/size]comments[size=12pt][/size]]

Thank you for your reply. Ok I have added the ‘’ marks inside the [] of $_POST and removed the @ from the connection code. Also removed the close connection code. I now get a server error. I have read your posts about the ‘isset’ problems and will look into this once I am up and running.

Code is now:

 <form action="index.php" method = "post">
 	<fieldset><legend>Please enter information in the form below: </legend>
 
 	<p><label>Date: <input type="date" name="date"/></label></p>
 
 	<p><label>Clinic: <br><input type="radio" name="clinic" value ="Durham"/> Durham <br><input type="radio" name="clinic" value ="Newcastle"/> Newcastle <br><input type="radio" name="clinic" value ="Sunderland"/> Sunderland <br><input type="radio" name="clinic" value ="Ashington"/> Ashington <br><input type="radio" name="clinic" value ="Hartlepool"/> Hartlepool <br><input type="radio" name="clinic" value ="Bishop"/> Bishop <br><input type="radio" name="clinic" value ="Stockton"/> Stockton<br><input type="radio" name="clinic" value ="Middlesborough"/> Middlesborough <br><input type="radio" name="clinic" value ="Other"/> Other</p>
 
 	<p><label>Google Miles: <input type="number" name="c_mileage" min="0" max="500"/></label></p>
 
 	<p><label>Actual Miles: <input type="number" name="a_mileage" min="0" max="500"/></label></p>
 
 	<p><label>Appointments: <input type="number" name="appointments" min="0" max="7" /></label></p>
 
 	<p><label>Comments: <textarea name="comments" rows="3" cols="40"></textarea></label></p>	
 
 	</fieldset>
 
 	<p align="center"><input type="submit" name="submit" value="Submit" /></p>
 
 </form>
 
 <?php
 
 if (isset ($_POST[submit])){
 
 DEFINE ('DB_USER', 'root');
 DEFINE ('DB_PASSWORD', 'root');
 DEFINE ('DB_HOST', 'localhost');
 DEFINE ('DB_NAME', 'work_stats');
 
 // Make the connection:
 $dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die ('Could not connect to MySQL: ' . mysqli_connect_error() );
 
 mysqli_select_db("work_stats", $dbc);
 
 $sql = "INSERT INTO daily_stats (date,clinic,c_mileage,a_mileage,appointments,comments) VALUES ($_POST['date'],$_POST['clinic'],$_POST['g_mileage'],$_POST['a_mileage'],$_POST['appointments'],$_POST['comments'])";
 
 mysqli_query($sql, $dbc);
 
 }
 ?>
 
 </body>
 </html>

Have also seen it done in another example like this:

$table = $_POST["daily_stats"];
 $day = $_POST["date"];
 $clinic = $_POST["clinic"];
 $gm = $_POST["g_mileage"];
 $am = $_POST["a_mileage"];
 $app = $_POST["appointments"];
 $comments = $_POST["comments"];

$sql = "INSERT INTO $table (date,clinic,g_mileage,a_mileage,appointments,comments) VALUES ('$day','$clinic','$g_mileage','$a_mileage','$app','$comments');";
 mysqli_query($sql, $dbc);

However this doesn’t work either. Back to the drawing board! :smiley:

Well, nice you got the error to solve your problem. But, it doesn’t help us without seeing the error message!

Please post it and we will see what we can do to help…

Further, how have you debugged this yet so far? Have you tried to display your query to see if it is well formed
or not? You could just echo it like “die($sql)” just before your line: mysqli_query($sql, $dbc);
That will kill your page and display the query so you can review it and see if it is accurate.

[php]$values = array(
$_POST[“date”],
$_POST[“clinic”],
$_POST[“g_mileage”],
$_POST[“a_mileage”],
$_POST[“appointments”],
$_POST[“comments”],
);

try {
$sql = “INSERT INTO $table (date,clinic,g_mileage,a_mileage,appointments,comments) VALUES (?,?,?,?,?,?);”;
mysqli_prepared_query($dbc, $sql, “ssiiss”, $values); // not sure of the types, I don’t like mysqli
} catch ( Exception $e ) {
echo $e->getMessage();
}[/php]

I am debugging via the log file as I am having issues with errors in the browser despite having all error messages turned on in the php.ini file. When I run my script I get the following:

[22-Feb-2016 18:45:42 Europe/Berlin] PHP Parse error: syntax error, unexpected ‘’ (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /Applications/MAMP/htdocs/WorkStats/index.php on line 60

It looks like " is causing the problem. Can someone give an example of how the query should be formed?

Gonna hit the books again tomorrow, Im getting ahead of myself with the Mysql stuff I think! ::slight_smile:

If I return to my original code before the server error I get the following:

[22-Feb-2016 19:05:46 Europe/Berlin] PHP Warning: include(Applications/MAMP/htdocs/mysqli_connect.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/WorkStats/index.php on line 2
[22-Feb-2016 19:05:46 Europe/Berlin] PHP Warning: include(): Failed opening ‘Applications/MAMP/htdocs/mysqli_connect.php’ for inclusion (include_path=’.:/Applications/MAMP/bin/php/php7.0.0/lib/php’) in /Applications/MAMP/htdocs/WorkStats/index.php on line 2
[22-Feb-2016 19:05:46 Europe/Berlin] PHP Notice: Use of undefined constant submit - assumed ‘submit’ in /Applications/MAMP/htdocs/WorkStats/index.php on line 43
[22-Feb-2016 19:05:46 Europe/Berlin] PHP Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /Applications/MAMP/htdocs/WorkStats/index.php on line 53
[22-Feb-2016 19:05:46 Europe/Berlin] PHP Notice: Undefined index: g_mileage in /Applications/MAMP/htdocs/WorkStats/index.php on line 55
[22-Feb-2016 19:05:46 Europe/Berlin] PHP Warning: mysqli_query() expects parameter 1 to be mysqli, string given in /Applications/MAMP/htdocs/WorkStats/index.php on line 57

Your errors are listed on lines 2, 43, 53, 55 and 57… LOL and you showed 15 lines total? HMMM…

Anyway, your prepare line gives one string, two integers, three strings. ( “siiss” ) BUT, you insert six (6) items.
Bet that might be it… Also, use Astonecipher’s try-catch to get the real error messages…

As I said I am running the original code submitted in the 1st post. The 15 lines above was just an example I saw elsewhere and also did not work. I will try to get better error messages but I think I am out of my depth here and Im not fully understanding what is going on in the mysql code. I will keep at it as this whole project was done as a learning exercise. Thanks again for all the responses so far.

[member=43746]ErnieAlex[/member] the prepared statement was my error. I counted off wrong, of course with PDO, I wouldn’t need to worry about those kinds of things…

Prepared statements should be where you go anyway, but your issue may be, some insertion values need quotes, others don’t. Prepared statements will handle this stuff on your behalf.

Well, LowFlying, we have to go by the last post, not the first post. So, looking at your first post again,
you have the connections to your database at the end of the MySQLi functions. They are supposed to be
the first argument. Also, of course, the $_POST[] names need to be quoted as you were told before…
Good luck…

[php]

<?php include ('Applications/MAMP/htdocs/mysqli_connect.php'); ?> Work Statistics Form label { font-weight: bold; color: #300ACC; } Please enter information in the form below:
<p><label>Date: <input type="date" name="date"/></label></p>

<p><label>Clinic: <br><input type="radio" name="clinic" value ="Durham"/> Durham <br><input type="radio" name="clinic" value ="Newcastle"/> Newcastle <br><input type="radio" name="clinic" value ="Sunderland"/> Sunderland <br><input type="radio" name="clinic" value ="Ashington"/> Ashington <br><input type="radio" name="clinic" value ="Hartlepool"/> Hartlepool <br><input type="radio" name="clinic" value ="Bishop"/> Bishop <br><input type="radio" name="clinic" value ="Stockton"/> Stockton<br><input type="radio" name="clinic" value ="Middlesborough"/> Middlesborough <br><input type="radio" name="clinic" value ="Other"/> Other</p>

<p><label>Google Miles: <input type="number" name="c_mileage" min="0" max="500"/></label></p>

<p><label>Actual Miles: <input type="number" name="a_mileage" min="0" max="500"/></label></p>

<p><label>Appointments: <input type="number" name="appointments" min="0" max="7" /></label></p>

<p><label>Comments: <textarea name="comments" rows="3" cols="40"></textarea></label></p>	

</fieldset>

<p align="center"><input type="submit" name="submit" value="Submit" /></p>
<?php if (isset ($_POST[submit])){ DEFINE ('DB_USER', 'root'); DEFINE ('DB_PASSWORD', 'root'); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', 'work_stats'); // Make the connection: $dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die ('Could not connect to MySQL: ' . mysqli_connect_error() ); mysqli_select_db($dbc, "work_stats"); $sql = "INSERT INTO daily_stats (date,clinic,c_mileage,a_mileage,appointments,comments) VALUES ('$_POST["date"]','$_POST["clinic"]','$_POST["g_mileage"]','$_POST["a_mileage"]','$_POST["appointments"]','$_POST["comments"]')"; mysqli_query($dbc, $sql); mysqli_close($dbc); } ?> [/php]

Yep, I tried the $_POST with quotations but it didnt work. Like I say I intend to read up some more before having another look at it. Thanks again.

Lowflyingsasquatch, nothing will ever work if you don’t fix the DB connection first. I corrected them for you
in your first code post and posted the new version. Did you try it?

Sponsor our Newsletter | Privacy Policy | Terms of Service