0

I am trying to put data that a user submits in a form onto a MySQL database that I have but I can't seem to figure it out! I've searched everywhere and ended up copying some php code that I found on the internet but it is still not putting the data into the Database!

Here is my HTML form:

<form action="Sendtodatabase.php" method="POST" name="EmailForm">

    First Name:<br>
    <input type="text" size="25" name="firstname"><br><br>
    Last Name:<br>
    <input type="text" size="25" name="lastname"<br><br><br>
    Email:<br>
    <input type="text" size="25" name="email"<br><br><br>
    Telephone Number:<br>
    <input type="text" size="25" name="telephone"<br><br><br>
    <input type="submit" value="Submit">

</form>

and my PHP file:

<?php
$con=mysqli_connect(sql307.byethost33.com, b33_13775589, *********, b33_13775589_murdermystery);
if (mysqli_connect_errno($con)) {
    echo "Failed to connect to MySQL: ".mysqli_conect_error();
}
$sql="INSERT INTO Murder (FirstName, LastName, Email, Telephone) VALUES ('$_POST [firstname]', '$_POST [lastname]',
'$_POST [email]', '$_POST [telephone]')";
if (!mysqli_query($con,$sql)) {
    die('Error: '.mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);

What am I doing wrong here? Thank you!

user2805499
  • 11
  • 1
  • 1

3 Answers3

0

Looks like an issue with your INSERT statement. Switch to PDO and use prepared statements to prevent injection. I added mysqli_real_escape_string to help some.

$sql="INSERT INTO Murder (FirstName, LastName, Email, Telephone) VALUES (
    '" . mysqli_real_escape_string($_POST["firstname"])."', 
    '" . mysqli_real_escape_string($_POST["lastname"]) . "', 
    '" . mysqli_real_escape_string($_POST["email"]) . "', 
    '" . mysqli_real_escape_string($_POST["telephone"]) . "')";
Aaron W.
  • 9,254
  • 2
  • 34
  • 45
  • No, he should use a prepared statement, it provides more reliable way to ensure the data is not evaluated as SQL. mysql_real_escape_string is deprecated and should be avoided. – dead Sep 23 '13 at 02:08
  • Yes I said to use prepared statements - changed to mysqli_real_escape_string – Aaron W. Sep 23 '13 at 02:33
0

Are you sure youre contructing your form correctly?

HTML CODE :

<form action="Sendtodatabase.php" method="POST" name="EmailForm">

        First Name:<br>
        <input type="text" size="25" name="firstname"><br><br>
        Last Name:<br>
        <input type="text" size="25" name="lastname"><br><br><br>
        Email:<br>
        <input type="text" size="25" name="email"><br><br><br>
        Telephone Number:<br>
        <input type="text" size="25" name="telephone"><br><br><br>
        <input type="submit" value="Submit">

</form>

PHP CODE :

    <?php

    // validate to check if $_POST array have datas needed.
    print_r($_POST);
    exit;


    $con=mysqli_connect('sql307.byethost33.com', 'b33_13775589', '*********', 'b33_13775589_murdermystery');
    if (mysqli_connect_errno($con)) {
        echo "Failed to connect to MySQL: ".mysqli_conect_error();
    }
    $sql="INSERT INTO Murder (FirstName, LastName, Email, Telephone) VALUES (".$_POST ['firstname']", ".$_POST ['lastname'].",
    ".$_POST ['email'].", ".$_POST ['telephone'].")";
    if (!mysqli_query($con,$sql)) {
        die('Error: '.mysqli_error($con));
    }
    echo "1 record added";
    mysqli_close($con);
?>
Jhonathan H.
  • 2,734
  • 1
  • 19
  • 28
  • I used your code and the page prints out the array with the correct information but it isn't saved into my database! I feel like I am still doing something wrong! – user2805499 Sep 23 '13 at 23:11
  • @user2805499 what error does it call back. ? or whick condition in your query does it falls? – Jhonathan H. Sep 24 '13 at 01:46
  • i see that your wrong at contructing your query and even getting data from posr variable . Also in connection to the database. you lack some opartors. just check this out.. – Jhonathan H. Sep 24 '13 at 02:00
0

From the code that you post in HTML part, you are lacking to put ">" after the name of each input attribute. Please make the correction and your code will run smoothly already.