0

一直在尝试将复选框的值输入数据库但失败了,有人有关于如何将复选框的值输入数据库的提示吗?

然后是一个问题,我怎样才能在同一个文件中运行 post 操作?

我的代码这么长:

<?php
$con=mysqli_connect("db_info");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO mass (gift, name, epost, interest) 
VALUES 
('$_POST[gift_nor]','$_POST[name_nor]','$_POST[epost_nor]','$_POST[interest_nor]')";


if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);
?>


<form method="post" action="">

    <input type="radio" name="gift_nor" value="present_1">present_1<br>
    <input type="radio" name="gift_nor" value="present_2">present_2<br />
    <input type="radio" name="gift_nor" value="present_3">present_3

    <hr />

    Namn: <input type="text" name="name_nor">
    Epostadress: <input type="text" name="epost_nor">

    <hr />

    <input type="checkbox" name="interest_nor[]" value="int_1">I have a bike<br>
    <input type="checkbox" name="interest_nor[]" value="int_2">I have a car<br />
    <input type="checkbox" name="interest_nor[]" value="int_3">I have a computer

    <hr />        

    <input name="Send" type="submit" id="Skicka">
</form>
4

1 回答 1

3

使用implode,做这样的事情

<?php
if (isset($_POST['Send']))
{
$con=mysqli_connect("db_info");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $check=implode(',', $_POST['interest_nor']); //it convert checkbox array into string

    $sql="INSERT INTO mass (gift, name, epost, interest) 
    VALUES 
    ('$_POST[gift_nor]','$_POST[name_nor]','$_POST[epost_nor]','$check')";
if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);
}
?>
于 2013-10-05T19:08:43.060 回答