I am trying to insert data from web form to mysql database, Below is the form and input fields html code
<form id="form1" name="form1" method="post" action="insert.php" >
<p>
<lable>ENTER SO NUMBER</lable>
<input type="text" name="soid" id="soid" maxlength="6" required>
<p>
<lable>SELECT DEPARTMENT</lable>
<select type="text" name="dept" id="dept">
<option value="NGS Analysis">NGS Analysis</option>
<option value="E-Array">E-Array</option>
<option value="Micro-Array">Micro-Array</option>
<option value="NGS Data">NGS Data</option>
</select>
</p>
<p>
<lable>SELECT STATUS</lable>
<select>
<option value="Sample Recived" name="samplerecived">Sample Recived</option>
<option value="Mol Bio Extraction" name="molbioextraction">Mol-Bio Extraction</option>
<option value="Extraction QC" name="molbioextractionqc">Extraction QC</option>
<option value="Library Prep" name="libraryprep">Library Prep</option>
<option value="Library QC" name="libraryqc">Library QC</option>
<option value="Sequencing" name="sequencing">Sequencing</option>
<option value="Data check" name="resequencing">Data Check</option>
<option value="Re-Sequencing" name="datacheck">RE-Sequencing</option>
<option value="QC Check" name="qccheck">QC Check</option>
<option value="Analysis Started" name="analysisstarted">Analysis Started</option>
<option value="Analysis Completed" name="analysiscompleted">Analysis Completed</option>
<option value="Report" name="report">Report</option>
<option value="Outbound" name="outbound">Outbound</option>
</select>
</p>
<p><button><img src="http://brandonmadeawebsite.com/images/art/icons/insert_icon.png" height="50" />INSERT</button></p>
</form>
If user selects one of the option from the dropdown options and enters some so-numbers in the field and hit insert button, than in database. selected option column should record the timestamp in particular column.
php script i have tried
<?php
$so = $_POST['soid'];
$dp = $_POST['dept'];
$selected = $_POST['options'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "status";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_errno) {
printf("Connect failed: %s
", $conn->connect_error);
exit();
}
$timestamp = new DateTime();
$result = mysqli_query($conn,"INSERT INTO $dbname.statusinfo (soid, dept, options) VALUES ('$so','$dp','$timestamp->format(NOW())')")or die(mysqli_error($conn));
echo "Inserted sucessfully with So Number <u><b>$so</b></u> Corresponding Status is <u><b>$st</b></u>";
$conn->close();
?>
But i am getting error as "Column count doesn't match value count at row 1".
Error: Column count doesn't match value count at row 1
Means you have more columns in your query than values. You need to set the columns you are actaully updating. you update 3 values so there only need to be set 3 columns.
So match the number of columns in your query, only the columns you are updating:
(soid, dept, samplerecived, molbioextraction, molbioextractionqc, libraryprep, libraryqc, sequencing, resequencing, datacheck, qccheck, analysisstarted, analysiscompleted, report,outbound)
with the number of values in your query:
( '$so','$dp','$se1')
You get this error because the number of column you are updating in the query doesn't match the number of values you are passing:
INSERT INTO table (column1, column2, column3) VALUES (value1, value2, value3);
The number of values must match the number of columns.
To update the correct column with the current timestamp I suggest you to match the values of the option tags with the name of the column you want to update:
<select name="options">
<option value="samplerecived">Sample Recived</option>
<option value="molbioextraction">Mol-Bio Extraction</option>
<option value="molbioextractionqc">Extraction QC</option>
<option value="libraryprep">Library Prep</option>
...
...
</select>
and then get the selected one and update the corresponding column with the current timestamp:
$selected = $_POST['options'];
$timestamp = new DateTime();
INSERT INTO $dbname.statusinfo (soid, dept, $selected) VALUES ('$so','$dp','$timestamp')