0

I have a form where I enter text, then it goes to the database and after I want it to be immediately displayed on the same page. I enter the info and submit it, page reloads and nothing appears in database or page. Any ideas maybe?

Saving part:

if(isset($_POST['ok'])){
                $Vardas = $_POST['vardas'];
                $Epastas = $_POST['epastas'];
                $Kam = $_POST['kam'];
                $Zinute = $_POST['zinute'];
                $Date = date('Y-m-d H:i:s');
                /*$IP = $_SERVER[REMOTE_ADDR];*/
                }
//else {die ("Neuzpildyta forma");}
$sql = "INSERT INTO table1 (vardas, epastas, kam, data, zinute) 
VALUES ('$Vardas', '$Epastas','$Kam', '$Date', '$Zinute')";
//if (mysqli_query($dbc, $sql)) echo "Įrašyta";
//else die ("Klaida įrašant:" .mysqli_error($dbc));

Form:

    <form method='post' action="">
                            <div class="form-group col-lg-4">
                                <label for="vardas" class="control-label">Siuntėjo vardas:</label>
                                <input name='vardas' type='text' class="form-control input-sm">
                            </div>
                            <div class="form-group col-lg-4">
                                 <label for="epastas" class="control-label">Siuntėjo e-paštas:</label>
                                 <input name='epastas' id="epastas" type='email' class="form-control input-sm">
                            </div>
                            <div class="form-group col-lg-4">
                                 <label for="kam" class="control-label">Kam skirta:</label>
                                 <input name='kam' type='text' class="form-control input-sm">
                            </div>
                            <div class="form-group col-lg-12">
                                 <label for="zinute" class="control-label">Žinutė:</label>
                                 <textarea name='zinute' class="form-control input-sm"></textarea>
                            </div>
                            <div class="form-group col-lg-2">
                                 <input type='submit' name='ok' value='siųsti' class="btnbtn-default">
                            </div>
</form>
4
  • 4
    You have your database query execution commented out. Commented Oct 17, 2018 at 15:23
  • 3
    You are wide open for SQL injection. Since you're using mysqli, take advantage of prepared statements and bind_param. This will take care of any pesky quoting issues that may occur. Commented Oct 17, 2018 at 15:24
  • I presume you know it's commented out? Commented Oct 17, 2018 at 15:25
  • Yes, I am very new to php and copied some of the code from youtube video and am learning this way. Thank You all, now I know what that means! :D Commented Oct 17, 2018 at 15:31

1 Answer 1

1

It looks like you're missing your $mysqli connection. Your code is also very susceptible to mysql injection so here is my recommendation.

$mysqli = new mysqli("localhost", "username", "password", "database_name");

if(isset($_POST['ok'])){
    $Vardas = $mysqli->real_escape_string($_POST['vardas']);
    $Epastas = $mysqli->real_escape_string($_POST['epastas']);
    $Kam = $mysqli->real_escape_string($_POST['kam']);
    $Zinute = $mysqli->real_escape_string($_POST['zinute']);
    $Date = date('Y-m-d H:i:s');

    $my_insert_query = "INSERT INTO table1 (vardas, epastas, kam, data, zinute)
    VALUES ('$Vardas', '$Epastas','$Kam', '$Date', '$Zinute')";

    $insert = $mysqli->query($my_insert_query);
    if($insert){
        echo "Success!";
    }else{
        echo "error" . $mysqli->error;
    }
}

The code above should work and prevent any sql injection.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.