本文介绍了将单选按钮和复选框的值放入数据库PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试将checkbox和单选按钮的值插入我的数据库...当我选择复选框的选项并选择单选按钮的选项,复选框和单选按钮的值将记录在同一行在数据库...我有3个菜单选择。每个菜单有2个大小选项,这是单选按钮。这意味着每个大小选项应该有不同的名称正确的...名称我的意思是field1 []。 ...

I would like to try insert the value of checkbox and radio button into my database...when I choose the option of checkbox and choose the option of radio button, the value of checkbox and radio button will recorded in the same row in database...I have 3 menu choices..each menu have 2 size options which is radio button..that's mean for each size option there should have different name right??...the name I mean is field1[]....

这是我的代码

resto.php

Menu : 
<input type="checkbox" value="Siomay" name="tile[]">Siomay <br>
<input type="radio" name="field1[]" value="4" />Small
<input type="radio" name="field1[]" value="5" />Large<br>
<input type="checkbox" value="Batagor" name="tile[]">Batagor <br>
<input type="radio" name="field2[]" value="4" />Small
<input type="radio" name="field2[]" value="5" />Large<br>
<input type="checkbox" value="Pempek" name="tile[]">Pempek<br>
<input type="radio" name="field3[]" value="4" />Small
<input type="radio" name="field3[]" value="5" />Large<br>


<input name="confirm" type=submit id="confirm" value='Confirm'>

</form>

menu2.php

<?php
$con = mysql_connect("localhost","root");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("project1", $con);

$Tile = mysql_real_escape_string($_POST['tile']);
$Field = mysql_real_escape_string($_POST['field1']);

if ( !empty($Tile) && !empty($Field) ) {
    $insert="INSERT INTO menu(name, price) VALUES ('$Tile','$Field')";
    if (!mysql_query($insert, $con)) {
        die('Error: ' . mysql_error());
    }
    echo "1 record added";
}
else echo "Please select at least 1 menu.";

mysql_close($con) 
?>


推荐答案

第一件事:为您的单选按钮。使用方式:

1st thing: You don't have to create array for your radio buttons. Use like:

<input type="checkbox" value="Siomay" name="tile[]">Siomay <br>
<input type="radio" name="field1" value="4" />Small
<input type="radio" name="field1" value="5" />Large<br>

第二件事:变更:

  $Tile = mysql_real_escape_string($_POST['tile']);
  $Field = mysql_real_escape_string($_POST['field1']);

      $Tile = mysql_real_escape_string($_POST['tile'])!=""?mysql_real_escape_string($_POST['tile']):0;
      $Field = mysql_real_escape_string($_POST['field1'])!=''?mysql_real_escape_string($_POST['field1']):0;

这篇关于将单选按钮和复选框的值放入数据库PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 23:26