如何在特定列中插入时间戳值?

时间:2015-07-16 08:18:17

标签: php html mysql

我正在尝试将数据从Web表单插入到mysql数据库中,下面是表单和输入字段html代码

<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>   

如果用户从下拉选项中选择一个选项并在字段中输入一些so-number并按下插入按钮,则在数据库中输入。选定的选项列应记录特定列中的时间戳。

php脚本我试过

   <?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\n", $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();

&GT;

但我收到的错误是“列数与第1行的值计数不匹配”。

2 个答案:

答案 0 :(得分:0)

  

错误:列数与第1行的值计数不匹配

表示查询中的列数多于值。您需要设置您实际更新的列。您更新了3个值,因此只需要设置3列。

因此,匹配查询中的列数,只匹配要更新的列:

(soid, dept, samplerecived, molbioextraction, molbioextractionqc, libraryprep, libraryqc, sequencing, resequencing, datacheck, qccheck, analysisstarted, analysiscompleted, report,outbound)

包含查询中的值数:

( '$so','$dp','$se1')

答案 1 :(得分:0)

您收到此错误,因为您在查询中更新的列数与您传递的值的数量不匹配:

INSERT INTO table (column1, column2, column3) VALUES (value1, value2, value3);

值的数量必须与列数匹配。

要使用当前时间戳更新正确的列,我建议您将选项标记的值与要更新的列的名称相匹配:

<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>

然后获取所选的一个并使用当前时间戳更新相应的列:

$selected = $_POST['options'];

$timestamp = new DateTime();

INSERT INTO $dbname.statusinfo (soid, dept, $selected) VALUES ('$so','$dp','$timestamp')