Can't get data from form to my mysql database

I have a problem to get my code to work in php I want to move data from form in html to my mysql database but that doesn't work.

The formal part

 <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 part

<?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();
}
}
?>

      

connecting to my mysql database, i can get data from the database but not enter into it.

Thanks in advanced.

These are many solutions I've tried but doesn't work!

+3


source to share


3 answers


The problem is this: -

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

      

And you write: -



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

      

Note: - either change name = submit

in the first or change $_POST["Submit"]

in the second. Thank.

+3


source


I like to do this using two files, one for handling inputs and one for the actual form.

form.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>

      

Now, for 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;
}

      

I don't know if this will work for you, but it worked for me :)

(Yes, they are well aware that you shouldn't use the mysql_ functions as they are depreciated, but they still work and are easy to use :))

0


source


Errors

  • Invalid submit tag name name='Submit'

  • improve your HTML code format (the tag </table>

    should close after the last tag </tr>

    )
  • Avoid SQL Injections withmysql_real_escape_string()

So your final code will be

 <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>

      

IN 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();
        }
    }
?>

      

0


source







All Articles