0

I have a simple form I used bootstrap to design the form:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Simple Form</title>
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <link href="font-awesome/css/font-awesome.css" rel="stylesheet">
        <link href="css/plugins/iCheck/custom.css" rel="stylesheet">
        <link href="css/animate.css" rel="stylesheet">
        <link href="css/style.css" rel="stylesheet">
    </head>
    <body>
        <div class="ibox-title">
            <h5>Table of All</h5>
        </div>
        <div class="ibox-content">
            <form method="post" class="form-horizontal">
                <div class="form-group">
                    <label class="col-sm-2 control-label">FirstName</label>
                     <div class="col-sm-10">
                    <input type="text" class="form-control" name="fname"></div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">Contact Number</label>
                    <div class="col-sm-10">
                    <input type="text" class="form-control" name="Contact"></div>
                </div>

                <div class="form-group">
                    <div class="col-sm-4 col-sm-offset-2">
                        <button class="btn btn-white" type="submit">Cancel</button>
                        <a href="table_simples.html" class="btn btn-primary" role="button">List</a>
                        <button class="btn btn-primary" name="insert" type="submit">Save</button>
                    </div>
                </div>
            </form>
        </div>
        <!-- Mainly scripts -->
        <script src="js/jquery-2.1.1.js"></script>
        <script src="js/bootstrap.min.js"></script>
        <script src="js/plugins/metisMenu/jquery.metisMenu.js"></script>
        <script src="js/plugins/slimscroll/jquery.slimscroll.min.js"></script>
        <!-- Custom and plugin javascript -->
        <script src="js/inspinia.js"></script>
        <script src="js/plugins/pace/pace.min.js"></script> 
    </body>
</html>

and here is my php code for inserting it to database:

<?php
$servername = "localhost";
$username = "root";
$dbname = "test";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO test (firstname, contact)
VALUES ('John', 'Doe')";

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

$conn->close();
?> 

how I take values from fname and contact textbox to add it in php code.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kho Dam
  • 17
  • 1
  • 7

2 Answers2

0
<?php
if(isset($_POST['insert']){
    $servername = "localhost";
    $username = "root";
    $dbname = "test";
    $password = "root_password_if_applicable";

    $conn = mysqli_connect($servername, $username, $password, $dbname);


    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    else{
        $fname=isset($_POST['fname'])?$_POST['fname']:null;
        $contact=isset($_POST['Contact'])?$_POST['Contact']:null;


    $sql = "INSERT INTO table (firstname, contact) VALUES ('$fname',     '$contact')";

    if (mysqli_query($conn, $sql)) {
        echo $fname." added to table successfully";
    } else {
        echo "Error: ". mysqli_error($conn);
    }

    mysqli_close($conn);
}
?> 

Change table to the table name

Note: You have set the cancel button as type="submit". You might want to change it to type="reset" if you want it clear form.

AtulBhatS
  • 181
  • 2
  • 12
-1

PHP:

<?php
if(isset($_POST['inser'])){
$servername = "localhost";
$username = "root";
$password = "yourpassword";
$dbname = "test";
$conn = new mysqli($servername, $username, $password, $dbname);

$fname = $_POST['fname'];
$contact = $_POST['Contact'];

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO test (firstname, contact)
VALUES ($fname, $contact)";

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

$conn->close();
}
?>

Hope it will work :)

  • *"Hope it will work"* - It won't, believe me and for quite a few reasons. Run that yourself and you will see the syntax errors. – Funk Forty Niner Jun 12 '16 at 15:05
  • sorry, in line 2 use 'insert' instead of 'inser' – Tareque Md Hanif Jun 12 '16 at 15:16
  • You need to modify your answer about that and read up on string literals http://dev.mysql.com/doc/refman/5.7/en/string-literals.html in regards to the values. You're also leaving them open to an SQL injection. http://stackoverflow.com/q/60174/ – Funk Forty Niner Jun 12 '16 at 15:19
  • You have to add the single quote for this value query $sql = "INSERT INTO test (firstname, contact) VALUES ('$fname', '$contact')"; – Peternak Kode Channel Jun 12 '16 at 15:20
  • @FahrudinYuniwinanto True. However, even with the quotes for it, that will fail them if a value such as `Jim's Bar & Grill` is entered, in turn causing a syntax error. http://php.net/manual/en/mysqli.error.php - which is why the values need to be escaped and for a few reasons too. – Funk Forty Niner Jun 12 '16 at 15:21
  • Definitely, for ignore the quote of html we have to catch with real_escape_string() – Peternak Kode Channel Jun 12 '16 at 15:28
  • @Fred-ii- i tried but it gives me error : onnect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "INSERT INTO test (firstname, contact) VALUES ('$fname', '$contact')"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . " " . $conn->error; } $conn->close(); } ?> – Kho Dam Jun 12 '16 at 19:14