为了账号安全,请及时绑定邮箱和手机立即绑定

PHP:将值从窗体插入MySQL

PHP:将值从窗体插入MySQL

GCT1015 2019-09-24 15:29:03
我从终端创建了一个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尝试从用户表返回所有数据的操作,但得到了空集。
查看完整描述

3 回答

?
侃侃无极

TA贡献2051条经验 获得超10个赞

您的代码中有两个问题。


表单中找不到任何动作。

您尚未执行查询mysqli_query()

dbConfig.php


<?php


$conn=mysqli_connect("localhost","root","password","testDB");


if(!$conn)

{

die("Connection failed: " . mysqli_connect_error());

}


?>

的index.php


 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>


 <?php


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

{

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

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


    $result = mysqli_query($conn,$sql);

}


?>


<form action="index.php" 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>


</form>


</body>

</html>


查看完整回答
反对 回复 2019-09-24
?
慕尼黑8549860

TA贡献1818条经验 获得超11个赞

尝试这个:


dbConfig.php


<?php

$mysqli = new mysqli('localhost', 'root', 'pwd', 'yr db name');

    if($mysqli->connect_error)

        {

        echo $mysqli->connect_error;

        }

    ?>

的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="";

    if(isset($_POST['txtbutton']))

        {

            $name = $_POST['txtname'];

            $pwd = $_POST['txtpwd'];

            $mysqli->query("insert into users(name,pwd) values('$name', '$pwd')");

        if(!$mysqli) 

        { echo mysqli_error(); }

    else

    {

        echo "Successfully Inserted <br />";

        echo "<a href='show.php'>View Result</a>";

    }


         }  


    ?>


查看完整回答
反对 回复 2019-09-24
  • 3 回答
  • 0 关注
  • 587 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信