检查表单字段并将值插入数据库

时间:2021-11-12 12:45:05

I have a simple php/html form wich inserts data to my mysql database.

我有一个简单的php / html表单,它将数据插入到我的mysql数据库中。

I have a column on my database called type. When a form is submitted with various values I want to be able to check the value of those submitted fields and add a value to the column type based on those values:
If the submitted form field field1 has the value value1 and field field2 has the value value2 then insert value3 to column type on my database.

我的数据库中有一个名为type的列。当提交具有各种值的表单时,我希望能够检查这些提交字段的值,并根据这些值向列类型添加值:如果提交的表单字段field1的值为value1,则字段field2具有值value2然后将value3插入我的数据库上的列类型。

2 个解决方案

#1


0  

You can manage this with code, if I understand your question right. Remember, it is a good idea to check the post values, before insert or update some values in the database. I am using post for the form method, if you use get, replace $_POST with $_GET

如果我理解你的问题,你可以用代码来管理它。请记住,在插入或更新数据库中的某些值之前,最好检查post值。我使用post作为表单方法,如果你使用get,用$ _GET替换$ _POST

<?php
if ($_POST["field1"] == "value1" AND $_POST["field2"] =="value2") 
{  $type = $value3
}
else
{  /// create some defaultvalue if the condition is not true. 
   $type = "youdefaultvalue" 
   // or you use some other value from your form, for example $_POST["field3"]
}
// here create your sql-action like "insert into ... " or update .... 

#2


0  

I am not sure whether you need this exactly. But after reading your question, i hope you are looking like this only.

我不确定你是否真的需要这个。但在看完你的问题后,我希望你看起来只是这样。

if($_POST['field1'] == "value1" && $_POST['field2'] == 'value2' ){

       // INSERT field3 to type
}

#1


0  

You can manage this with code, if I understand your question right. Remember, it is a good idea to check the post values, before insert or update some values in the database. I am using post for the form method, if you use get, replace $_POST with $_GET

如果我理解你的问题,你可以用代码来管理它。请记住,在插入或更新数据库中的某些值之前,最好检查post值。我使用post作为表单方法,如果你使用get,用$ _GET替换$ _POST

<?php
if ($_POST["field1"] == "value1" AND $_POST["field2"] =="value2") 
{  $type = $value3
}
else
{  /// create some defaultvalue if the condition is not true. 
   $type = "youdefaultvalue" 
   // or you use some other value from your form, for example $_POST["field3"]
}
// here create your sql-action like "insert into ... " or update .... 

#2


0  

I am not sure whether you need this exactly. But after reading your question, i hope you are looking like this only.

我不确定你是否真的需要这个。但在看完你的问题后,我希望你看起来只是这样。

if($_POST['field1'] == "value1" && $_POST['field2'] == 'value2' ){

       // INSERT field3 to type
}