user5122241
user5122241

Reputation:

Cant get data from a form to my mysql database

I have problem to get my code working in php I want to move data from a form in html to my mysql database but it dont work??

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

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

the connection to my mysql database works i can get data from the database but not input to it.

Thanks in the advanced.

This is on of many solutions i have tried non have worked!!

Upvotes: 3

Views: 172

Answers (3)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

The problem is :-

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

And you write:-

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

Note:- either change name = submit in first one or change $_POST["Submit"] in second one. Thanks.

Upvotes: 3

Abdulla Nilam
Abdulla Nilam

Reputation: 38672

Errors

  1. Incorrect in submit tag name name='Submit'
  2. improve your HTML codeing format(</table> tag should close after last </tr> tag)
  3. Avoid SQL Injections by using mysql_real_escape_string()

So your final code would 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();
        }
    }
?>

Upvotes: 0

MrK
MrK

Reputation: 1078

I like to do this using two files, one for handling the 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 did work for me :)

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

Upvotes: 0

Related Questions