1

データを入力してデータベースに投稿する単純な Web フォームを作成しました。情報を送信すると正しく送信されますが、テーブルで表示するとデータが表示されません。

クエリ

CREATE TABLE `recipe` (
    `id` int(4) NOT NULL auto_increment,
    `recipename` varchar(65) NOT NULL default '',
    `ingredients` varchar(65) NOT NULL default '',
    `instructions` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=0 ; 

phpコード

<?php

$host="localhost"; // Host name
$username="my username"; // Left empty due to privacy
$password="mypassword"; // Left empty due to privacy
$db_name="mydatabase"; // Left empty due to privacy
$tbl_name="recipe"; // Table name


mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$recipe=$_POST['recipename'];
$ingredients=$_POST['ingredients'];
$instructions=$_POST['instructions'];


$sql="INSERT INTO $tbl_name(recipename, ingredients, instructions)VALUES('$recipe', '$ingredients', '$instructions')";
$result=mysql_query($sql);


if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='recipe.php'>Back to main page</a>";
} else {
   echo "ERROR";
}
?>

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

ウェブフォーム

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
  <tr>
    <td>
      <form name="form1" method="post" action="insert_ac.php">
        <table width="100%" border="0" cellspacing="1" cellpadding="3">
          <tr>
            <td colspan="3"><strong>Insert Data Into mySQL Database </strong></td>
          </tr>
          <tr>
            <td width="71">Recipe name</td>
            <td width="6">:</td>
            <td width="301"><input name="name" type="text" id="recipe"></td>
          </tr>
          <tr>
            <td>Ingredients</td>
            <td>:</td>
            <td><input name="lastname" type="text" id="ingredients"></td>
          </tr>
          <tr>
            <td>Instructions</td>
            <td>:</td>
            <td><input name="email" type="text" id="instructions"></td>
          </tr>
          <tr>
            <td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
          </tr>
        </table>
      </form>
    </td>
  </tr>
</table>

データが表示されない理由を誰かが知っているかどうか知りたいです。ここに画像の説明を入力

データのないテーブルのイメージ

4

1 に答える 1

4
$recipe=$_POST['recipename'];
$ingredients=$_POST['ingredients'];
$instructions=$_POST['instructions'];

する必要があります:

$recipe=$_POST['name'];
$ingredients=$_POST['lastname'];
$instructions=$_POST['email'];

フォーム フィールド名が間違っているため、値が取得されず、空のデータが挿入されます。に設定error_reportingするE_ALLと、エラーが発生します。その設定で開発してください。

また、nameフォーム フィールドの属性は、インデックス名の対象となるものです。$_POST['INDEX_NAME']誤ってそのid属性を使用しています。

于 2013-01-17T16:11:58.700 に答える