0

順序付けられていないリストをデータベースの列に保存したいと考えています。これどうやってするの ?

  • チェス
  • ハム

CREATE TABLE INGREDIENTE (名前 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);
?>

ここに画像の説明を入力

4

2 に答える 2

0

ここで本当にやりたいのは、材料を区切り文字で保存し、出力時にリストにフォーマットすることです。

次のようなものを試してください。

<form action="save.php">
    <input type="text" name="name" />
    <textarea name="ingredients"></textarea>
    <button type="submit">Save</button>
</form>

save.php-保存する前に、区切り文字を改行(\ n)文字に置き換えます

<?php

$name = $_GET["name"];
$name = mysqli_real_escape_string($name);

$ingredients = $_GET["ingredients"];
$ingredients = mysqli_real_escape_string($ingredients);
$ingredients = preg_replace("/[,\.\n\t]+/", "\n", $ingredients);

display.php-表示する前に、成分を改行文字で分割し、ULでレンダリングします

echo "<ul>";
$ingredients = explode('\n", $ingredients);
for ($i = 0; $i < count($ingredients); $i++) {
    echo "<li>{$ingredients[$i]}</li>";
}
echo "</ul>";
于 2012-12-02T02:51:08.323 に答える
0

リストをカンマ区切りの文字列に変換できます。

$ingredients = join(',', $_POST['selections']);

dom では、選択ボックスの選択に名前を付けることができます。

于 2012-12-02T02:44:49.417 に答える