在这里有点生气,尝试了很多事情,但没有任何效果。我想为我的“聚会聚会”魔术师最终项目制作一个表单,您可以在其中选择复选框以在数据库中的一列中填充字母或完全不包含任何字母。
<!Doctype html>
<html>
<head>
</head>
<body>
<?php
if (!empty($_POST)) {
//connect to the database
$mysqli = new mysqli('localhost', 'root', '', 'magic');
//check the connection
if ($mysqli->connect_error) {
die('Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error);
}
//insert the data
$sql = "INSERT INTO decks ( DeckName, Author, CardList ) VALUES ( '{$mysqli->real_escape_string($_POST['DeckName'])}', '{$mysqli->real_escape_string($_POST['Author'])}', '{$mysqli->real_escape_string($_POST['CardList'])}' )";
$insert = $mysqli->query($sql);
//insert the checkbox data
if (isset($_POST['Red'])) {
$sql = "INSERT INTO decks ( Red ) VALUES ( '{$mysqli->real_escape_string('R')}' ) ";
} else {
$sql = "INSERT INTO decks ( Red ) VALUES (' ')";
}
//response from the database
if ($insert) {
echo "Success!";
} else {
die("Error, please try again!");
}
//kill the connection
$mysqli->close();
}
?>
<form method="post" action="">
<input name="DeckName" type="text">
<br>
<br>
<input name="Author" type="text">
<br>
<br>
<input type="checkbox" id="Red" name="Red"> <img src="red.png" alt="Red">
<input type="checkbox" id="Blue" name="Blue"> <img src="blue.png" alt="Blue">
<input type="checkbox" id="Green" name="Green"> <img src="green.png" alt="Green">
<input type="checkbox" id="White" name="White"> <img src="white.png" alt="White">
<input type="checkbox" id="Black" name="Black"> <img src="black.png" alt="Black">
<br>
<br>
<input name="CardList" type="text">
<br>
<br>
<input type="submit" value="Submit Form">
</form>
</body>
</html>
如果要选中此复选框,我希望它在列中放置R,否则请不要。列中什么都没有。
答案 0 :(得分:0)
尝试使用以下代码插入数据:
<?php
if ( !empty( $_POST) ) {
//connect to the database
$mysqli = new mysqli( 'localhost', 'root', '', 'magic' );
//check the connection
if ( $mysqli->connect_error ) {
die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
}
//insert the data
$sql = "INSERT INTO decks ( DeckName, Author, CardList ) VALUES ( '{$mysqli->real_escape_string($_POST['DeckName'])}', '{$mysqli->real_escape_string($_POST['Author'])}', '{$mysqli->real_escape_string($_POST['CardList'])}' )";
//insert the checkbox data
if ( isset($_POST['Red']) ) {
$sql = "INSERT INTO decks ( Red ) VALUES ( '{$mysqli->real_escape_string('R')}' ) ";
} else {
$sql = "INSERT INTO decks ( Red ) VALUES (' ')";
}
//response from the database
if ($mysqli->query($sql) === TRUE) {
echo "Success!";
} else {
die("Error, please try again!");
}
//kill the connection
$mysqli->close();
}
?>
从您的代码中删除此行$insert = $mysqli-> query($sql);
,并将此if ($mysqli->query($sql) === TRUE) {
替换为游览代码if ( $insert ) {
答案 1 :(得分:-1)
In HTML
<label> Language</label>
<input type="checkbox" name="lan[]" value="eng">English
<input type="checkbox" name="lan[]" value="tam">Tamil
<input type="checkbox" name="lan[]" value="tel">Telugu
<input type="checkbox" name="lan[]" value="hin">Hindi
In php
$lan=$_POST["lan"];
$slan=implode[","$lan];
答案 2 :(得分:-1)
if (!empty($_POST)) {
//connect to the database
$mysqli = new mysqli('localhost', 'root', '', 'magic');
//check the connection
if ($mysqli->connect_error) {
die('Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error);
}
//Do NOT insert the data HERE
// the checkbox data
if (isset($_POST['Red'])) {
$red = 'R';
} else {
$red = ' ';
}
$sql = "INSERT INTO decks ( DeckName, Author, CardList, Red ) VALUES ( '{$mysqli->real_escape_string($_POST['DeckName'])}', '{$mysqli->real_escape_string($_POST['Author'])}', '{$mysqli->real_escape_string($_POST['CardList'])}' , '{$mysqli->real_escape_string($red)}' )";
$insert = $mysqli->query($sql);
//response from the database
if ($insert) {
echo "Success!";
} else {
die("Error, please try again!");
}
// I put the value of (red) in the insert sql before executing it so must be done with all check boxes ! ALL MUST BE IN ONE INSERT SENTENCE
//kill the connection
$mysqli->close();
}
?>
<form method="post" action="">
<input name="DeckName" type="text">
<br>
<br>
<input name="Author" type="text">
<br>
<br>
<input type="checkbox" id="Red" name="Red"> <img src="red.png" alt="Red">
<input type="checkbox" id="Blue" name="Blue"> <img src="blue.png" alt="Blue">
<input type="checkbox" id="Green" name="Green"> <img src="green.png" alt="Green">
<input type="checkbox" id="White" name="White"> <img src="white.png" alt="White">
<input type="checkbox" id="Black" name="Black"> <img src="black.png" alt="Black">
<br>
<br>
<input name="CardList" type="text">
<br>
<br>
<input type="submit" value="Submit Form">
</form>
在您的代码中,您只需将sql语句设置为插入红色但不执行它,即使您这样做,它也会位于新行中,而其他列的值为空,
我编写的代码适用于红色复选框