gpt4 book ai didi

php - 将单选按钮和复选框的值放入数据库 PHP

转载 作者:行者123 更新时间:2023-11-29 14:22:35 24 4
gpt4 key购买 nike

我想尝试将复选框和单选按钮的值插入到我的数据库中...当我选择复选框选项并选择单选按钮选项时,复选框和单选按钮的值将记录在同一行中在数据库中...我有3个菜单选择..每个菜单有2个尺寸选项,即单选按钮..这意味着每个尺寸选项应该有不同的名称,对吗???...我的意思是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)
?>

最佳答案

第一件事:您不必为单选按钮创建数组。使用如下:

<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 - 将单选按钮和复选框的值放入数据库 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11522106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com