gpt4 book ai didi

php - 在 MySQL 中存储复选框值

转载 作者:行者123 更新时间:2023-11-30 22:12:13 24 4
gpt4 key购买 nike

我有一个 html 测试,其中有几个关于复选框的问题。我的目标是将选定的值存储在数据库中。

这是我的代码:

insert.php

    <?php
$host="***"; // Host name
$username="***"; // Mysql username
$password="***"; // Mysql password
$db_name="***"; // Database name
$tbl_name="test_submissions"; // Table name

// Connect to server and select database.
$conn = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$question1=implode(',',$_POST["radio-choice-h-2"]);

//Instant search results
$perfume1=implode(',', $_POST["product1"]);
$perfume2=implode(',', $_POST["product2"]);
$perfume3=implode(',', $_POST["product3"]);
$noperfume4=implode(',', $_POST["product4"]);

//Rest of questions
$question2=implode(',', $_POST["radio-choice-h-4"]);
$question3=implode(',', $_POST["radio-choice-h-5"]);
$question4=implode(',', $_POST["radio-choice-h-6"]);
$question5=implode(',', $_POST["radio-choice-h-7"]);
$question6=implode(',', $_POST["radio-choice-h-8"]);
$question7=implode(',', $_POST["radio-choice-h-9"]);
$question8=implode(',', $_POST["radio-choice-h-10"]);
$question9=implode(',', $_POST["radio-choice-h-11"]);
$question10=implode(',', $_POST["radio-choice-h-12"]);
$question11=implode(',', $_POST["radio-choice-h-13"]);
$question12=implode(',', $_POST["radio-choice-h-14"]);
$question13=implode(',', $_POST["radio-choice-h-15"]);
$question14=implode(',', $_POST["radio-choice-h-16"]);

//Inserting email
$email=implode(',', $_POST["email"]);

if(isset($_POST['submit']))
{
$sql ="INSERT INTO test_submissions (question1, perfume1, perfume2, perfume3, noperfume5, question2, question3,";
$sql +="question4, question5, question6, question7, question8, question9, question10, question11, question12, question13$
$sql +="question14, question15, mail) VALUES('$question1','$perfume1','$perfume2','$perfume3','$noperfume4','$question2'$
$sql +="'$question5','$question6','$question7','$question8','$question9','$question10','$question11','$question12','$que$
$result=mysql_query($conn, $sql) or die("Error en l'SQL");
}

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}

else {
echo "ERROR";

}
<?php
// close connection
mysql_close();
?>

index.php

<form action="insert.php" method="post">
<fieldset data-role="controlgroup" data-type="horizontal">
<input name="radio-choice-h-2[]" id="radio-choice-h-2a" value="woman" checked="checked" type="radio">
<label for="radio-choice-h-2a">Mujer</label>
<input name="radio-choice-h-2[]" id="radio-choice-h-2b" value="man" type="radio">
<label for="radio-choice-h-2b">Hombre</label>
<input name="radio-choice-h-2[]" id="radio-choice-h-2c" value="baby" type="radio">
<label for="radio-choice-h-2c">Bebé</label>
<input name="radio-choice-h-2[]" id="radio-choice-h-2d" value="pet" type="radio">
<label for="radio-choice-h-2d">Mascota</label>
<input name="radio-choice-h-2[]" id="radio-choice-h-2e" value="environtment" type="radio">
<label for="radio-choice-h-2e">Ambiental</label>
</fieldset>
<input type="submit" name="submit" data-inline="true" value="Submit"></p>

而且我无法将选定的复选框存储在数据库中。任何人都知道什么是错的?非常感谢您的帮助!

最佳答案

//use type="checkbox"
<input name="radio-choice-h-2[]" id="radio-choice-h-2b" value="man" type="checkbox">

关于php - 在 MySQL 中存储复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39712762/

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