两个数据库查询

I know there are already a couple of questions on two query commands on one page but I have tried everything and I can't get it to work.

I am trying to create a simple form that adds the inputed value into a database and then outputs the database with a select statement.

What am I doing wrong?

Here is what I got. Thanks so much!

<?php
$dbhost = "localhost";
$dbuser = "widget_cms";
$dbpass = "blah123";
$dbdata = "widget_corp";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbdata);

if(mysqli_connect_errno()) {
    die("database connection failed: " .
        mysqli_connect_error() .
            " (" . mysqli_connect_errno() . ")"
    );
} else {
    echo "success!";
} 

?>


<?php 

if (isset($_POST["submit"])) {
    $menu_name = ($_POST["menuName"]);
    $position = ($_POST["pos"]);
    $visibility = ($_POST["vis"]);

    $query1 = "INSERT INTO subjects (menu_name, position, visibility) VALUES ('{$menu_name}', '{$position}', '{$visibility}'); ";

    $result = mysqli_query($connection, $query1);

    if(!$result) {
        die("Database query failed");
    }

} else {
    echo "something didnt work idiot";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html lang="en">
 <head>
  <title>phpMysql</title>
 </head>
 <body>

    <ul>
     <form action="phpMysql.php" method="post">
     Menu Name: <input type="text" name="menuName" value=""><br>
     Position: <input type="text" name="pos" value=""><br>
     Visibility: <input type="text" name="vis" value=""><br>
     <input type="submit" name="submit" value="Create account"><br>
     </form>
    </ul> 

    <?php print_r($_POST); ?>

    <?php

    $query1 = "SELECT * FROM subjects;" ;

    $result = mysqli_query($connection, $query1);

    if(!$result) {
        die("Database query failed");
    }   
    ?>

    <?php while ($row = mysqli_fetch_assoc($result)) {
        var_dump($row);
        echo "<hr />" ;
    }
    ?>

    <?php mysqli_free_result($result); ?>

 </body>
</html>

<?php mysqli_close($connection); ?>

First, you have several errors in your coding. This line here:

$query1 = "INSERT INTO subjects (menu_name, position, visibility) VALUES ('{$menu_name}', '{$position}', '{$visibility}'); ";

Is wrong. You don't need any {} surrounding your variables. Also, you have an extra ; within your quotes. it won't work with those. Here is the correct code:

$query1 = "INSERT INTO subjects (menu_name, position, visibility) VALUES ('$menu_name', '$position', '$visibility')";

Also this line is bad as well, you included an extra ; within your query.

$query1 = "SELECT * FROM subjects;" ;

It should be this:

$query1 = "SELECT * FROM subjects";

There is also a bit of clean up you can do. A lot of php blocks can be merged into single blocks to make your code look a lot more neat. Here is your edited code with all fixes I noticed.

<?php

$dbhost = "localhost";
$dbuser = "widget_cms";
$dbpass = "blah123";
$dbdata = "widget_corp";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbdata);

if(mysqli_connect_errno()) {
    die("database connection failed: " .
        mysqli_connect_error() .
            " (" . mysqli_connect_errno() . ")"
    );
} else {
    echo "success!";
}

if (isset($_POST["submit"])) {
    $menu_name = ($_POST["menuName"]);
    $position = ($_POST["pos"]);
    $visibility = ($_POST["vis"]);

    $query1 = "INSERT INTO subjects (menu_name, position, visibility) VALUES ('$menu_name', '$position', '$visibility')";

    $result = mysqli_query($connection, $query1);

    if(!$result) {
        die("Database query failed");
    }

} else {
    echo "something didnt work idiot";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html lang="en">
 <head>
  <title>phpMysql</title>
 </head>
 <body>

    <ul>
     <form action="phpMysql.php" method="post">
     Menu Name: <input type="text" name="menuName" value=""><br>
     Position: <input type="text" name="pos" value=""><br>
     Visibility: <input type="text" name="vis" value=""><br>
     <input type="submit" name="submit" value="Create account"><br>
     </form>
    </ul> 

<?php

    print_r($_POST);

    $query1 = "SELECT * FROM subjects";

    $result = mysqli_query($connection, $query1);

    if(!$result) {
        die("Database query failed");
    } 

    while ($row = mysqli_fetch_assoc($result)) {
        var_dump($row);
        echo "<hr />" ;
    }

    mysqli_free_result($result);

?>

 </body>
</html>

<?php mysqli_close($connection); ?>

Refer to the documentation on queries if you are unsure how to code it properly or are coming across issues you don't understand. http://php.net/manual/en/mysqli.query.php I would also suggest looking into prepared statements. They are much better and more secure than a standard mysqli query. http://php.net/manual/en/mysqli.quickstart.prepared-statements.php