Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML form not sending data to MySQL using PHP

I have a form in HTML, but it's not setup like normal I guess. I'll post the code form the HTML (PHP) file and the php to send it to the db.

<form action="upload.php" method="POST">
    <!-- Name input-->
    <div class="form-group">
      <label class="control-label" for="name">Name</label>
      <div class="">
        <input id="name" name="name" placeholder="First and Last Name" class="form-control input-md" type="text" required>
      </div>
    </div>
    <div class="form-group">
        <label class=" control-label" for="supportingDoc">Upload Supporting Documentation</label>
        <div class="">
            <input id="supportingDoc" name="supportingDoc" class="input-file" type="file" style="margin-top: .5em; margin-left: 4em;">
        </div>
    </div>
    <hr>
      <!-- Submit -->
      <div class="form-group">
        <label class="control-label" for="submit"></label>
        <div class="">
          <button value="Submit" type="submit" id="submit" name="submit" class="btn btn-danger" style="border-radius: 25px;">Submit</button>
        </div>
      </div>
</form>

Here is my SQL/PHP

<?php
    $servername = "localhost";
    $username = "xxx";
    $password = "xxx";
    $dbname = "xxx";
    // Create connection
    $con = mysqli_connect("localhost","xxx","xxx","xxx");
    // Check connection
    if (mysqli_connect_errno()){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    if (isset($_REQUEST['name'])){
        // set variables

        $name = mysql_real_escape_string($_POST['name']);
        $supportingDoc = mysql_real_escape_string($_POST['supportingDoc']);

        $sql = "INSERT INTO `tablew` (name,  supportingDoc) VALUES ('$name', '$supportingDoc')";
        $result = mysqli_query($con,$sql);

        if ($con->query($sql) === TRUE) {
            echo "New record created successfully";
            printf("New record created successfully");
        } else {
            echo "Error: " . $sql . "<br>" . $con->error;
            printf("Error: " . $sql . "<br>" . $con->error);
        }

        $con->close();
    }
?>

I've tried all sorts of variations and nothing is showing in phpmyadmin. I've even replicated from a previous site I created and it still didn't work lol.

I see that there are variables for the login info and still put it in the mysqli, but I've been at this one thing for about 8 hours now and am out of juice, so hopefully someone sees what I messed up on.

Thanks in advance for any help everyone.

=========================================================================== UPDATE: I made all the changes mentioned above and now get this:

Warning: mysqli_connect(): (HY000/1049): Unknown database

I can see the database in phpmyadmin and Sequel Pro. I've also made sure to set the password and login to 'root'. My code is as follows for login:

    $con = mysqli_connect("localhost","root","root","epboarding");
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

and this is my POST:

if (isset($_REQUEST['submit'])){
// set variables
$name = mysqli_real_escape_string($con, $_POST['name']);
$sql = "INSERT INTO `wos` (name) VALUES ('$name')";
$result = mysqli_query($con,$sql);

phpmyadmin

mysql error

like image 358
TomG103 Avatar asked Dec 30 '17 04:12

TomG103


2 Answers

Following issues can be there:

  1. Uploading error. Use enctype="multipart/form-data" in form tag.
  2. Correct Mysql_connect details i.e. Username, Password, Dbname.
  3. mysql_real_escape_string is depricated. Use mysqli.
  4. Recheck your column names i.e. name, supportingDoc.
like image 161
TBI Avatar answered Sep 19 '22 19:09

TBI


Your html code wrong. Whenever you want to use input type file, you need to put <form> tag to <form enctype="multipart/form-data">. If you re not declaring the enctype, then the PHP cannot read the file.

like image 31
Mr Hery Avatar answered Sep 20 '22 19:09

Mr Hery