-1
<form action="main.php" method="POST">
    <input type="email" name="email" placeholder="Enter your email">
    <img type="image" id="send" src="bs.png" alt="Submit Form"/>
    <button type="sumbit">Submit</button>
</form>

<?php
$con = 
mysqli_connect("mysql.hostinger.co.uk",
"something","something","something");

$email = $_POST['email'];

$sql = "INSERT INTO emails (email) 
VALUES ('$email')";

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

header("Location: result.htm");    

(three different databases, and it just doesn't get inserted into the table..) I've been trying to fix it for three hours, if not more. Maybe I'm just an idiot, but I don't see what is wrong.

-table name is emails

-there are 2 rows : email varchar(255) and id which is auto incremented

Deathstorm
  • 818
  • 11
  • 36
Maere
  • 39
  • 7
  • Check the value of `$run`. It will return `false` on failure. Call `mysqli_error()` to get a descriptive error. – Halcyon Jun 16 '17 at 14:05
  • 1
    I hope you weren't meaning to post the credentials to your database... – cteski Jun 16 '17 at 14:05
  • 1
    var dump or echo $email and make sure you have data. Second, you should use prepared statements for this, third NEVER POST YOUR PASSWORD AND LOGIN INFORMATION ON STACK OVERFLOW – clearshot66 Jun 16 '17 at 14:05
  • any error throw out? – Felix Jun 16 '17 at 14:05
  • If those are genuine database credentials I'd make sure you change them before someone uses them to gain access to it. Editing your Stack Overflow post won't stop someone being able to view the question's history and viewing them. – drmonkeyninja Jun 16 '17 at 14:07
  • You have a typo; you create a variable called `$con` and then you try to use one called `$conn`. Flagging as such. – cteski Jun 16 '17 at 14:08
  • Yes the typo was the issue :D Thank you all so much! – Maere Jun 16 '17 at 14:23
  • Develop with `error_reporting(E_ALL); ini_set('display_errors', '1');` and to keep your database from being destroyed https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 – AbraCadaver Jun 16 '17 at 15:10

3 Answers3

0

check the name of the variable while executing the query you have written $conn and while initializing the connection you have used $con

Saurabh Gupta
  • 122
  • 2
  • 12
0

Couple of things that could help:

DON'T put personal credentials on stackoverflow!

Variable check!

$con = 
mysqli_connect("mysql.hostinger.co.uk",
"u394e","ere31","u159il");

has $con. Where

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

has $conn

Backticks

Inserting into a Database could work by using backticks. like the example below:

$sql = "INSERT INTO emails (`email`) 
VALUES ('$email')";

Check your Query

Try putting an if statement around your query with an sql_error if it doesn't work. Example below:

if(isset($_POST['submit'])) {
    $email = $_POST['email'];

    $sql = "INSERT INTO emails (email) 
    VALUES ('$email')";

    if ($con->query($sql) === TRUE) {
        echo "<script>alert('Uw reactie is toegevoegd.')</script>";
    } else {
        echo "Error: " . $sql . "<br>" . $con->error;
    }
}

Hope this helps!

Community
  • 1
  • 1
Deathstorm
  • 818
  • 11
  • 36
0

There is an error on your code you are using wrong variable $conn , replace it wit $con , Try This Code. it works.

<form action="main.php" method="POST">
    <input type="email" name="email" placeholder="Enter your email">
    <img type="image" id="send" src="bs.png" alt="Submit Form"/>
    <button type="sumbit">Submit</button>
</form>

<?php
if(isset($_POST['email'])){
$con = mysqli_connect("mysql.hostinger.co.uk","u1e","1","u1il");

$email = $_POST['email'];

$sql = "INSERT INTO emails (email) VALUES ('$email')";

$run = mysqli_query($con, $sql);

header("Location: result.htm");  
}
?> 
Martin
  • 22,212
  • 11
  • 70
  • 132
Sunil Rajput
  • 960
  • 9
  • 19