user3717206
user3717206

Reputation: 75

Connecting PHP source code and submit form to MySQL Database

I'm trying to learn PHP and I'm trying to connect a MySQL database with my PHP code to make a submit form that lets me input data into the database. My problem is that the source code is connecting but the HTML isn't posting the variables to the PHP file. I could really use some help.

This is my HTML source code

<html>

<head>
<title>Form Input Data</title>
</head>

<body>

<table border="1">
  <tr>
    <td align="center">Form Input Employees Data</td>
  </tr>
  <tr>
  <td>
  <table>
    <form action="input.php" method="POST">
    <tr>
      <td>Name</td>
      <td><input type="text" name="name" size="20">
      </td>
    </tr>
    <tr>
      <td>Address</td>
      <td><input type="text" name="address" size="40">
      </td>
    </tr>
    <tr>
      <td></td>
      <td align="right"><input type="submit" 
      name="submit" value="Sent"></td>
    </tr>
    </table>
  </td>
</tr>
</table>

And this is my PHP source code

<?php

$user_name = "fees0_14446440";
$password = "********";
$database = "fees0_14446440_addressbook";
$server = "sql107.0fees.net";

mysql_connect("$server","$user_name","$password");

mysql_select_db("$database");


$order = "INSERT INTO Trial

        (name, address)

        VALUES

        ('$name',

        '$address')";


$result = mysql_query($order);

if($result){

    echo("<br>Input data is succeed");

} else{

    echo("<br>Input data is fail");

}
?>

Upvotes: 4

Views: 57587

Answers (4)

JustStarting
JustStarting

Reputation: 9

Try the following:

<?php

$user_name = "fees0_14446440";
$password = "********";
$database = "fees0_14446440_addressbook";
$server = "sql107.0fees.net";

mysql_connect("$server","$user_name","$password");

mysql_select_db("$database");

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

$order = mysql_query("INSERT INTO Trial (name, address) VALUES ('$name', '$address')");

if ($order) {
    echo '<br>Input data is successful';
} else {
    echo '<br>Input data is not valid';
}
}
?>

Upvotes: 0

Bibek Jana
Bibek Jana

Reputation: 104

put POST values into variable

$name=$_POST['name'];
$address=$_POST['address'];

before sql query and write sql query as

$order = "INSERT INTO Trial

    (name, address)

    VALUES

    ('".$name."',

    '".$address."')";

Upvotes: 2

aplewandowski
aplewandowski

Reputation: 60

You can find the POST data in PHP's $_POST variable. It should hold all the values that were passed via the POST method.

$name = $_POST["name"];

Upvotes: 3

Khushboo
Khushboo

Reputation: 1817

Try this

<html>

<head>
<title>Form Input Data</title>
</head>

<body>
<form action="input.php" method="POST">
<table border="1">
  <tr>
    <td align="center">Form Input Employees Data</td>
  </tr>
  <tr>
  <td>
  <table>

    <tr>
      <td>Name</td>
      <td><input type="text" name="name" size="20">
      </td>
    </tr>
    <tr>
      <td>Address</td>
      <td><input type="text" name="address" size="40">
      </td>
    </tr>
    <tr>
      <td></td>
      <td align="right"><input type="submit" 
      name="submit" value="Sent"></td>
    </tr>
    </table>
  </td>
</tr>
</table>
</form>

In PHP code

$order = "INSERT INTO Trial

        (name, address)

        VALUES

        ('$_POST["name"]',

        '$_POST["address"]')";

Upvotes: -1

Related Questions