3

我有问题让我的代码在 php 中工作我想将数据从 html 中的表单移动到我的 mysql 数据库,但它不起作用?

表格部分

 <form action ="Mydbsinsertpersson4.php" method='POST'>
 <table>
 <tr><td width='100'>För namn:<input type='text' name='fnamn'><td></tr>
 <tr><td width='100'>Efternamn:<input type='text' name='enamn'><td></tr>
 </table>
 <tr><td><input type='submit' name='Submit'  value='Submit'><td></tr>
 </form>

php部分

<?php
    if(isset($_POST["submit"])){
require_once 'Mydbconfig.php';
try {
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "INSERT INTO tpersson (Perfnamn, Perenamn)
VALUES ('".$_POST["fnamn"]."','".$_POST["enamn"]."')";
$conn = null;
}
catch(PDOException $e){
echo $e->getMessage();
}
}
?>

与我的 mysql 数据库的连接工作我可以从数据库中获取数据,但不能输入到它。

感谢进阶。

这是我尝试过的许多解决方案中的一个!

4

3 回答 3

3

问题是 :-

<tr><td><input type='submit' name='Submit'  value='Submit'><td></tr>

你写: -

if(isset($_POST["submit"])){

注意:- 要么改变name = submit第一个,要么改变 $_POST["Submit"]第二个。谢谢。

于 2015-07-16T06:39:08.143 回答
0

我喜欢使用两个文件来做到这一点,一个用于处理输入,一个用于实际表单。

表单.html:

<form action ="/assets/post_people.php" method='POST'>
<table>
<tr><td width='100'>Firstname:<input type='text' name='fname'><td></tr>
<tr><td width='100'>Lastname:<input type='text' name='lname'><td></tr>
</table>
<tr><td><input type='submit' name='Submit'  value='Submit'><td></tr>
</form>

现在,对于 post_people.php:

<?php
//db 
require_once 'db_connect.php';


//Get and filter input @ to avoid injections
$Firstname = mysql_real_escape_string($_POST['fname']);
$Lastname = mysql_real_escape_string($_POST['lname']);


//Check if the user has submitted the form
if(isset($_POST['Submit'])){


    //Check if the person already exist in our database
    $Check_people = mysql_query("SELECT * FROM People WHERE Firstname =     '$Firstname' AND Lastname = '$Lastname'");

if(mysql_num_rows($Check_people)==1){
    //The person already exists with exact same data!
}

//The person did not exist in our database, so we'll update.
else{


    $update_query = mysql_query("INSERT INTO People (Firstname, Lastname) VALUES ('$Firstname', '$Lastname')");

    if($update_query){
        //success
    }
    else {
        //Error

    }

}

mysql_close();  
}


//The user didnt submit the form and tried to access the file ?
//Redirect to /form.html & kill the script.
else{
header("Location: /form.html");
die;
}

我不知道这是否对你有用,但它确实对我有用:)

(是的,我很清楚您不应该使用 mysql_ 函数,因为它们已被贬值,但它们仍在工作并且易于使用:))

于 2015-07-16T06:55:28.480 回答
0

错误

  1. 提交标签名称不正确name='Submit'
  2. 改进您的 HTML 编码格式(</table>标签应该在最后一个</tr>标签之后关闭)
  3. 通过使用避免SQL 注入mysql_real_escape_string()

所以你的最终代码是

 <form action ="Mydbsinsertpersson4.php" method='post'>
     <table>
     <tr><td width='100'>För namn:<input type='text' name='fnamn'><td></tr>
     <tr><td width='100'>Efternamn:<input type='text' name='enamn'><td></tr>

     <tr><td><input type='submit' name='submit'  value='Submit'><td></tr>
     </table>
 </form>

Mydbsinsertpersson4.php

 <?php
    if(isset($_POST["submit"])){
        require_once 'Mydbconfig.php';
        try {
            $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
        // set the PDO error mode to exception
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            $fname = $mysqli->real_escape_string($_POST["fnamn"]);
            $ename = $mysqli->real_escape_string($_POST["enamn"]);

            $sql = "INSERT INTO tpersson (Perfnamn, Perenamn) VALUES ('$fname','$ename')";
            $conn = null;
        }
        catch(PDOException $e){
            echo $e->getMessage();
        }
    }
?>
于 2015-07-16T06:59:53.927 回答