Home > Software engineering >  How do I format my HTML and PHP code in order to send form data to the database?
How do I format my HTML and PHP code in order to send form data to the database?

Time:01-29

I am currently utilizing the website "infinity free" to host my website and SQL database. I am trying to submit form data from this page in my website into mySQL database using PHP, but I am getting the error: "This page isn't working. transcare/infinityfreeapp is currently unable to handle this request. HTTP ERROR 500." I'm new to coding, but I have been trying to get this to connect for weeks now. Any help is appreciated.

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title>transcare</title>
  <link rel="stylesheet" href="./provider.css">
</head>
     <body>
<main>
  <form action="db_connection.php" method="get">
    <div >
    <p><label for="providername">Provider's Name:</label>
      <input type="text" name="providername" id="providername"></p>
    <p><label for="providerpronouns">Provider's Pronouns:</label>
      <input type="text" name="providerpronouns" id="providerpronouns"></p>
    <p><label for="providerphonenumber">Provider's Phone Number:</label>
      <input type="text" name="providerphonenumber" id="providerphonenumber"></p>
    <p><label for="providerzip">Provider's Zip Code:</label>
      <input type="text" name="providerzip" id="providerzip"></p>
  <br>
  <div  style="width:500px">
    <select>
    <option input type="text" name="providertype" id="providertype" >Provider Type:</option>
    <option input type="text" name="providertype" id="providertype" >Mental Health Therapist</option>
      <option input type="text" name="providertype" id="providertype" >Psychiatrist</option>
      <option input type="text" name="providertype" id="providertype" >Primary Care Doctor</option>
      <option input type="text" name="providertype" id="providertype" >Dentist</option>
      <option input type="text" name="providertype" id="providertype" >Massage Therapist</option>
    <option input type="text" name="providertype" id="provider_type" >Dentist</option>
      <option input type="text" name="providertype" id="providertype" >Nutritionist</option>
     <option input type="text" name="providertype" id="providertype" >Psychologist</option>
      <option input type="text" name="providertype" id="providertype" >Chiropractor</option>
      <option input type="text" name="providertype" id="providertype" >Acupuncturist</option>
      <option input type="text" name="providertype" id="providertype" >Optometrist</option>
      <option input type="text" name="providertype" id="providertype" >Other</option>
    </select>
  </div>                                          
      <br>
  
        <div  style="width:500px;">
    <select>
    <option input type="text" name="how" id="how" >How did you hear about this provider?</option>
    <option input type="text" name="how" id="how" >Saw provider myself</option>
    <option input type="text" name="how" id="how" >Heard good things from a trusted friend</option>
    <option input type="text" name="how" id="how" >Saw good reviews from other trans folks online</option>
      </select>
        </div>
  <label for="additional">What would you like us to know about this provider?</label>
    <textarea type="text" value="additional" rows="3" cols="30" name="additional" id="info"></textarea></p>
    <br>
<p>&nbsp;</p>
                                                   <p><input type="submit" name="Submit" id="Submit" value="Submit">                                     </p>
    </div>
    </form>

<?php
    $servername = "sql106.epizy.com";
    $username = "epiz_33436688";
    $password = "8om9bCIpNOMsIU";
    $dbname = "epiz_33436688_transcare";

// Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
} 

    $sql = “INSERT INTO epiz_33436688_transcare (providername, providerpronouns, providerphonenumber, providerzip, providertype, how, additional) VALUES ('$_POST[providername]', '$_POST[providerpronouns]', '$_POST[providerphonenumber]', '$_POST[providerzip]', '$_POST[providertype]', '$_POST[how]', '$_POST[additional]')”; if (!mysql_query($user_info, $connect)) { die('Error: ' . mysql_error()); }


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

    $conn->close();
?>

CodePudding user response:

put the name attribute in select element eg. <select name="ProviderType">

and as far as iknow 'option' doesn't support 'name' attribute eg. <option value="mental_health_therapist">

CodePudding user response:

to insert select option values in the database using PHP & MySQL you must follow the correct syntax of HTML, there is no "input type" in , try to do this:

<select name="providertype" id="providertype" >
<option value="Provider Type">Provider Type:</option>
<option  value="Mental Health Therapist">Mental Health Therapist</option>

... and so on ...

  • Related