猿问

PHP:将值从窗体插入MySQL

我从终端创建了一个users表,mysql然后尝试创建简单的任务:从表单中插入值。这是我的dbConfig file


<?php

$mysqli = new mysqli("localhost", "root", "pass", "testDB");


/* check connection */

if (mysqli_connect_errno()) {

    printf("Connect failed: %s\n", mysqli_connect_error());

    exit();

}

?>

这是我的Index.php。


<!doctype html>

<html>

 <head>

    <meta charset="utf-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

    <meta name="description" content="$1">

    <meta name="viewport" content="width=device-width, initial-scale=1">


    <link rel="stylesheet" type="text/css" href="style.css">


    <title>test</title>


    <?php

    include_once 'dbConfig.php';

    ?>


</head>

<body>

     <?php

    if(isset($_POST['save'])){

        $sql = "INSERT INTO users (username, password, email)

        VALUES ('".$_POST["username"]."','".$_POST["password"]."','".$_POST["email"]."')";

    }


    ?>


    <form method="post"> 

    <label id="first"> First name:</label><br/>

    <input type="text" name="username"><br/>


    <label id="first">Password</label><br/>

    <input type="password" name="password"><br/>


    <label id="first">Email</label><br/>

    <input type="text" name="email"><br/>


    <button type="submit" name="save">save</button>

    <button type="submit" name="get">get</button>

    </form>


</body>

</html>

按下我的保存按钮后,什么也没有发生,数据库仍然为空。我尝试echo'ing了INSERT查询,当它被认为它从形式的所有值。在尝试从终端检查是否可以正常工作后,我登录到sql尝试从用户表返回所有数据的操作,但得到了空集。


GCT1015
浏览 588回答 3
3回答

侃侃无极

您的代码中有两个问题。表单中找不到任何动作。您尚未执行查询mysqli_query()dbConfig.php<?php$conn=mysqli_connect("localhost","root","password","testDB");if(!$conn){die("Connection failed: " . mysqli_connect_error());}?>的index.php&nbsp;include('dbConfig.php');<!Doctype html><html><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta name="description" content="$1"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" type="text/css" href="style.css"><title>test</title></head><body>&nbsp;<?php&nbsp; if(isset($_POST['save'])){&nbsp; &nbsp; $sql = "INSERT INTO users (username, password, email)&nbsp; &nbsp; VALUES ('".$_POST["username"]."','".$_POST["password"]."','".$_POST["email"]."')";&nbsp; &nbsp; $result = mysqli_query($conn,$sql);}?><form action="index.php" method="post">&nbsp;<label id="first"> First name:</label><br/><input type="text" name="username"><br/><label id="first">Password</label><br/><input type="password" name="password"><br/><label id="first">Email</label><br/><input type="text" name="email"><br/><button type="submit" name="save">save</button></form></body></html>

慕尼黑8549860

尝试这个:dbConfig.php<?php$mysqli = new mysqli('localhost', 'root', 'pwd', 'yr db name');&nbsp; &nbsp; if($mysqli->connect_error)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; echo $mysqli->connect_error;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ?>的index.php<html><head><title>Inserting data in database table </title></head><body><form action="control_table.php" method="post"><table border="1" background="red" align="center"><tr><td>Login Name</td><td><input type="text" name="txtname" /></td></tr><br><tr><td>Password</td><td><input type="text" name="txtpwd" /></td></tr><tr><td>&nbsp;</td><td><input type="submit" name="txtbutton" value="SUBMIT" /></td></tr></table>control_table.php<?php include 'config.php'; ?><?php$name=$pwd="";&nbsp; &nbsp; if(isset($_POST['txtbutton']))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $name = $_POST['txtname'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $pwd = $_POST['txtpwd'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mysqli->query("insert into users(name,pwd) values('$name', '$pwd')");&nbsp; &nbsp; &nbsp; &nbsp; if(!$mysqli)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; { echo mysqli_error(); }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; echo "Successfully Inserted <br />";&nbsp; &nbsp; &nbsp; &nbsp; echo "<a href='show.php'>View Result</a>";&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;&nbsp;&nbsp; &nbsp; ?>
随时随地看视频慕课网APP
我要回答