-2

フォームからテーブルに値を挿入しようとしています。

ここに私が使用した私のコードがあります

insert.php のコード

<?php
// Make a MySQL Connection
mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
mysql_select_db("theaterdb") or die(mysql_error());
// Insert a row of information into the table "language"
mysql_query("INSERT INTO languages (language) VALUES('$_POST[language]') ")
or die(mysql_error());  
echo "Data Inserted!";
?>

HTMLフォーム

<html>

    <head></head>

    <body>
        <form action="insert.php" method="post">New Language:
            <input type="text" name="language">
            <input type="submit" value="Create Language">
        </form>
    </body>

</html>

しかし、insert.php ファイルを使用したくありません。代わりに、insert.html で php コードを使用したいと考えています。使ってみた

<form action="">

そしてまた

<form action="#">

<form action="<?php echo $PHP_SELF;?>">

しかし、それは機能していません。

4

4 に答える 4

0

PHP コードは .html ファイルでは実行されません。

ただし、html を php ファイルに出力することはできます。

htaccess を使用して html サフィックスを持つように php ページを書き直すこともできます。

于 2013-08-14T08:24:08.833 に答える
0

PHPコードは.htmlファイルでは機能しません.フォームに変更insert.htmlしてinsert.phpアクションを与えない方がよいので、セルフアクションが実行され、フォームがinsert.phpそれ自体に送信されます。

<html>
    <head></head>
    <body>
        <form action="" method="post">New Language:
            <input type="text" name="language">
            <input type="submit" name='save' value="Create Language">
        </form>
    </body>
</html>

そしてphpで

<?php
if(isset($_POST['save'])){
    // Make a MySQL Connection
    mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
    mysql_select_db("theaterdb") or die(mysql_error());
    // Insert a row of information into the table "language"
    mysql_query("INSERT INTO languages (language) VALUES('$_POST[language]') ")
    or die(mysql_error());  
    echo "Data Inserted!";
}
?>
于 2013-08-14T08:24:37.390 に答える
0

2 つのファイルを結合します。

.htaccess で .html のような .php ファイルを取得します

例: form.php

<?php
if (isset($_POST)) {
// Make a MySQL Connection
mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
mysql_select_db("theaterdb") or die(mysql_error());
// Insert a row of information into the table "language"
mysql_query("INSERT INTO languages (language) VALUES('$_POST[language]') ") or die(mysql_error());
echo "Data Inserted!";
}
?>

<html>
<head>
</head>
<body>
<form action="" method="post">
New Language: <input type="text" name="language">
<input type="submit" value="Create Language">
</form>
</body>
</html>
于 2013-08-14T08:25:36.650 に答える