简体   繁体   中英

php insert data into mysql but load options from a (different) table

I have a simple form and I input the data into a database, that works fine. Now i would like to load one option from a different table, that also works fine. What doesnt work fine is when I put those 2 things in one. Somehow I am able only to do one of those things, the loading options dont work. Could you help me out here please?

Here is the code, I tried to keep it simple.

 <?php    if(!empty($_POST) && $_SERVER['REQUEST_METHOD'] == 'POST'){ /* Attempt MySQL server connection. Assuming you are running MySQL server with default setting (user 'root' with no password) */ $link = mysqli_connect("localhost","root","password","db"); // Check connection if($link === false){    die("ERROR: Chyba připojování k databázi. " . mysqli_connect_error()); } // Escape user inputs for security $name = mysqli_real_escape_string($link, $_REQUEST['name']); $nickname = mysqli_real_escape_string($link, $_REQUEST['nickname']); $phone = mysqli_real_escape_string($link, $_REQUEST['phone']); // attempt insert query execution $sql = "INSERT INTO test (name, nickname, phone) VALUES ('$name', '$nickname', '$username')"; if(mysqli_query($link, $sql)){ header('Location: index.php'); exit; } else{    echo "ERROR " . mysqli_error($link); } } ?> <form method="POST" action=""> <label for="name">Name</label>    <input type="text" name="name" id="name"><br> <label for="nickname">Nickname</label> <select name="nickname" id="nickname"> <?php $res=mysqli_query($link, "select * from wp_is_users"); while($row=mysqli_fetch_array($res)) { ?> <option><?php echo $row["user_login"]; ?></option> <?php } ?> </select> <label for="phone">Phone</label>   <input type="text" name="phone" id="phone"><br> <input type="submit" value="Přidat záznam"><br>

`

Delete this line:

mysqli_close($link);

Firstly, you don't need it. Secondly when you fire a POST method it closes the connection and THEN you are tryingthe second query.

Also, open the connection before you check the request method, so the beginning of your file should go like this:

 $link = mysqli_connect("localhost","root","password","db");
if(!empty($_POST) && $_SERVER['REQUEST_METHOD'] == 'POST'){

This way it will work even if the method is get, which is default.

只是不要关闭数据库上的连接注释这一行 //mysqli_close($link);

Try this code. You were not submitting form properly. I have added some line of code for proper form submission

<?php
if(!empty($_POST) && $_SERVER['REQUEST_METHOD'] == 'POST'){

    /* Attempt MySQL server connection. Assuming you are running MySQL
    server with default setting (user 'root' with no password) */
    $link = mysqli_connect("localhost","root","password","db");
// Check connection
    if($link === false){
        die("ERROR: Chyba připojování k databázi. " . mysqli_connect_error());
    }


// Escape user inputs for security

    if(isset($_POST['submit'])) {//if form submit button is set
        $name = mysqli_real_escape_string($link, $_REQUEST['name']);
        $nickname = mysqli_real_escape_string($link, $_REQUEST['nickname']);
        $phone = mysqli_real_escape_string($link, $_REQUEST['phone']);


// attempt insert query execution
        $sql = "INSERT INTO test (name, nickname, phone) VALUES ('$name', '$nickname', '$username')";
        if (mysqli_query($link, $sql)) {
            header('Location: index.php');
            exit;
        } else {


            echo "ERROR " . mysqli_error($link);
        }

// close connection
       // mysqli_close($link);  skip this code
    }
}
?>

<form method="POST" action="">
    <label for="name">Name</label>
    <input type="text" name="name" id="name"><br>

    <label for="nickname">Nickname</label>
    <select name="nickname" id="nickname">
        <?php
        $res=mysqli_query($link, "select * from wp_is_users");
        while($row=mysqli_fetch_array($res))
        {
            ?>
            <option><?php echo $row["user_login"]; ?></option>
            <?php
        }
        ?>
    </select>

    <label for="phone">Phone</label>
    <input type="text" name="phone" id="phone"><br>
    <input type="submit" name="submit" value="Přidat záznam"><br>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM