0

I have setup a form using HTML & PHP. I am sending all of my information submitted from the form to an SQL database. When I submit the form I get the error code below:

ERROR: Could not execute INSERT INTO `timesheets` (`Name`, `weekending`, `monin`, `monout`, `tuein`, `tueout`, `wedin`, `wedout`, `thursin`, `thursout`, `friin`, `friout`, `satin`, `satout`, `sunin`, `sunout`) VALUES ('Nathan Langer','07:30','16:00','18:00','19:00','06:30','15:00','03:00','16:00','05:00','16:00','00:00','00:00','00:00','00:00'). Column count doesn't match value count at row 1

As you can see the date does not get pushed through ( should be in between Nathan Langer & 07:30.

My submission code is below:

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "username", "pass", "dbname");

// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security
$Name = mysqli_real_escape_string($link, $_POST['Name']);
$insertdate = date('Y-m-d', strtotime($_POST['weekending']));
$monin = mysqli_real_escape_string($link, $_POST['monin']);
$monout = mysqli_real_escape_string($link, $_POST['monout']);
$tuein = mysqli_real_escape_string($link, $_POST['tuein']);
$tueout = mysqli_real_escape_string($link, $_POST['tueout']);
$wedin = mysqli_real_escape_string($link, $_POST['wedin']);
$wedout = mysqli_real_escape_string($link, $_POST['wedout']);
$thursin = mysqli_real_escape_string($link, $_POST['thursin']);
$thursout = mysqli_real_escape_string($link, $_POST['thursout']);
$friin = mysqli_real_escape_string($link, $_POST['friin']);
$friout = mysqli_real_escape_string($link, $_POST['friout']);
$satin = mysqli_real_escape_string($link, $_POST['satin']);
$satout = mysqli_real_escape_string($link, $_POST['satout']);
$sunin = mysqli_real_escape_string($link, $_POST['sunin']); 
$sunout = mysqli_real_escape_string($link, $_POST['sunout']);


// attempt insert query execution
$sql = "INSERT INTO `timesheets` (`Name`, `weekending`, `monin`, `monout`,      `tuein`, `tueout`, `wedin`, `wedout`, `thursin`, `thursout`, `friin`, `friout`, `satin`, `satout`, `sunin`, `sunout`) VALUES ('$Name','$monin','$monout','$tuein','$tueout','$wedin','$wedout','$thursin','$thursout','$friin','$friout','$satin','$satout','$sunin','$sunout')";
if(mysqli_query($link, $sql)){
header('location:timesheets.php');
} else{
echo "ERROR: Could not execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link);
?>

Is this because I am doing something wrong with the date in the submission form? the date field in the form is below:

<div class="col-md-6 col-sm-6" >
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-calendar"></i>
</span>
<input type="date" id="weekending" class="form-control " name="weekending" placeholder="Week Ending">
</div>                                              
</div>  

Please can someone help me find where the issue is and why it won't post through to the database?

2
  • Value in query and value in database table is not match...you are missing any value in query Commented Feb 11, 2016 at 12:19
  • @V5Nathan you missed value $insertdate in insert value thats why date is not inserting....check and verify again your query Commented Feb 11, 2016 at 12:26

2 Answers 2

1

change from

$sql = "INSERT INTO `timesheets` (`Name`, `weekending`, `monin`, `monout`,`tuein`, `tueout`, `wedin`, `wedout`, `thursin`, `thursout`, `friin`, `friout`, `satin`, `satout`, `sunin`, `sunout`) VALUES ('$Name','$monin','$monout','$tuein','$tueout','$wedin','$wedout','$thursin','$thursout','$friin','$friout','$satin','$satout','$sunin','$sunout')";

to

$sql = "INSERT INTO `timesheets` (`Name`, `weekending`, `monin`, `monout`,`tuein`, `tueout`, `wedin`, `wedout`, `thursin`, `thursout`, `friin`, `friout`, `satin`, `satout`, `sunin`, `sunout`) VALUES ('$Name','$insertdate','$monin','$monout','$tuein','$tueout','$wedin','$wedout','$thursin','$thursout','$friin','$friout','$satin','$satout','$sunin','$sunout')";
Sign up to request clarification or add additional context in comments.

Comments

1

You are passing more columns than no. of values,

So you need to correct query by adding one more column name or remove one value.

INSERT INTO `timesheets` (`Name`, `weekending`, `monin`, `monout`, `tuein`, 
`tueout`, `wedin`, `wedout`, `thursin`, `thursout`, `friin`, `friout`, 
`satin`, `satout`, `sunin`, `sunout`) VALUES ('Nathan 
Langer','07:30','16:00','18:00','19:00','06:30','15:00','03:00','16:00','05:00',
'16:00','00:00','00:00','00:00','00:00', '00:00')

Above code will work as i added one more value in it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.