我想将无序列表保存到数据库中的列中。我怎样才能做到这一点 ?
- 国际象棋
- 火腿
创建表成分(名称varchar(20),成分varchar(30);
我想把清单放在配料部分。
这是我用来创建列表的 html 和 js 中的代码。
function selectIngredient(select)
{
var $ul = $(select).closest('.ui-select').prev('ul');
console.log($ul[0])
if ($ul.find('input[value=' + $(select).val() + ']').length == 0) {
console.log('s')
$ul.append('<li onclick="$(this).remove();">' +
'<input type="hidden" name="ingredients[]" value="' +
$(select).val() + '" /> ' +
$(select).find('option:selected').text() + '</li>');
}
}
<!DOCTYPE html>
<html>
<head>
<title>Ingredient</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="themes/receta.min.css" />
<link rel="stylesheet" href="themes/receta.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"></script>
<script src="recetas.js"> </script>
</head>
<body>
<form action="insert.php" method="post">
Receta: <input type="text" name="receta">
Ingredientes: <input type="text" name="ingredientes">
<input type="submit">
<label>Ingredientes</label>
<ul>
</ul>
<select onchange="selectIngredient(this);">
<option value="Cheese">Cheese</option>
<option value="Olives">Olives</option>
<option value="Pepperoni">Pepperoni</option>
<option value="Milk">Milk</option>
</select>
</body>
</html>
<?php
$dbhost = 'localhost';
$dbuser = 'alanis_lozano';
$dbpass = '20Anahuac12';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'alanis_recetas';
mysql_select_db($dbname, $conn);
$sql="INSERT INTO Recetas (receta, ingredientes)
VALUES
('$_POST[receta]',$ingredientes = join(',', $_POST['selections'])";
if (!mysql_query($sql,$conn))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($conn);
?>