开发者

HTML/PHP Survey not passing to MySQL database properly

I'm trying to make a small survey that populates the selections for the dropdown menu from a list of names from a database. The survey does this properly. I want to submit the qu开发者_JAVA技巧ote the user submits with this name into a quote database. The quote text they enter into the field goes in properly, however, the name selected from the menu does not get passed in. Instead I get a blank name field.

I understand some of my code is out of context, but the name is the only thing that does not get passed in properly.

On form submit, I include the php file that submits this data to the database:

<form action="<?php $name = $_POST['name']; include "formsubmit.php";?>" method="post">
    <label> <br />What did they say?: <br />
    <textarea name="quotetext" rows="10" cols="26"></textarea></label>
    <input type="submit" value="Submit!" />
</form>

The variable $name comes from this (which populates my dropdown menu):

echo "<select name='name'>";

    while ($temp = mysql_fetch_assoc($query)) {
    echo "<option>".htmlspecialchars($temp['name'])."</option>";
    }                       
echo "</select>";

And here is my formsubmit.php:

<?php:
    mysql_select_db('quotes');
    if (isset($_POST['quotetext'])) {
            $quotetext = $_POST['quotetext'];                                   
            $ident = 'yankees';
            $sql = "INSERT INTO quote SET 
        quotetext='$quotetext',
        nametext='$name',
        ident='$ident',
        quotedate=CURDATE()";
        header("Location: quotes.php");
    if (@mysql_query($sql)) {
    } else {
        echo '<p> Error adding quote: ' . 
            mysql_error() . '</p>';
    } 
    }
?>


Your form action stuff looks weird, but regardless, I think the problem you're having has to do with not setting $name = $_POST['name'] like you're doing with $quotetext = $_POST['quotetext']. Do that before the sql statement and it should be good to go.

edit to try to help you further, I'll include what the overall structure of your code should be, and you should tweak it to fit your actual code (whatever you're leaving out, such as setting $query for your name options):

file 1:

<form action="formsubmit.php" method="post">
    <label> <br />What did they say?: <br />
    <textarea name="quotetext" rows="10" cols="26"></textarea></label>
    <select name='name'>
    <?php
    while ($temp = mysql_fetch_assoc($query)) {
        echo "<option>".htmlspecialchars($temp['name'])."</option>";
    }
    ?>
    </select>
    <input type="submit" value="Submit!" />
</form>

formsubmit.php:

<?php
    mysql_select_db('quotes');
    if (isset($_POST['quotetext'])) {
        $quotetext = $_POST['quotetext'];
        $name = $_POST['name'];
        $ident = 'yankees';
        $sql = "INSERT INTO quote SET 
                quotetext='$quotetext',
                nametext='$name',
                ident='$ident',
                quotedate=CURDATE()";
        if (@mysql_query($sql)) {
            header("Location: quotes.php");
        } else {
            echo '<p> Error adding quote: ' . 
                mysql_error() . '</p>';
        } 
    }
?>


echo "<select name='name'>";

    while ($temp = mysql_fetch_assoc($query)) {
     $nyme = htmlspecialchars($temp['name']);
    echo "<option value='$nyme'>$nyme</option>";
    }                       
echo "</select>";-

This way you will receive the value of the name in $_POST array

and you have to get that value out of $_POST array as well you need to change the code add the following line to get the name in your script.

$name = $_POST['name'];

you need to change the form action tag

<form action='formsubmit.php' .....>

and in that file after successful insertion you can redirect the user to whereever.php. so it was fun explaining you every thing bit by bit change this now in your code as well.

if (@mysql_query($sql)) {
          header("Location: quotes.php");

} else {
    echo '<p> Error adding quote: ' . 
        mysql_error() . '</p>';
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜