Home > OS >  Input Values are not going to the database?
Input Values are not going to the database?

Time:05-04

I'm super new to PHP mysql database, can someone help me out how can I make my input appear on the database. This is just a sample I'm testing. I don't know what I'm doing wrong I have all the information from the server inputed it at the right place. I'm getting a messages that it was successful but nothing is going to my database. I have one file called connect.php where the php is and the index.php for the html, table is name cultivar_db. Thanks in advance

table structure cultivar_id type: int auto increment cultivar_name: varchar(20) latin1_swedish_ci

<?php
    $servername = "localhost";
    $username = "myusername";
    $password = "*****";
    $dbname = "mydbname";
    $cultivar_id = $_POST['cultivar_id'];
    $cultivar_name = $_POST['cultivar_name'];
    //Database Connection

    $conn = new mysqli($servername, $username, $password, $dbname);
    if($conn->connect_error){
        die('Connection Failed : ' . $conn->connect_error);
    }else{
        $stmt = $conn->prepare("INSERT INTO cultivar_db(cultivar_id, cultivar_name)
            values(?,?)");
        $stmt->bind_param("is", $cultivar_id, $cultivar_name);
        $stmt->execute();
        echo "successfully input datas..";
        $stmt->close();
        $conn->close();
    }
?>
<div >
    <form action="connect.php" method="post">
    <label>Cultivar ID:</label>
    <input  type="text" name="cultivar_id"><br />
    <label>Cultivar Name:</label>
    <input  type="text" name="cultivar_name"><br />
    <input  type="submit" value="import" >
    </form>
</div>

CodePudding user response:

Three possible issues within

        $stmt = $conn->prepare("INSERT INTO cultivar_db(cuttivar_id, cultivar_name) values(?,?,)");
  1. The second , within the values might causes some trouble
  2. Your ID field is set to AUTO INCREMENT, therefor no reason to insert it
  3. You typed cuttivar_id in your SQL, instead of cultivar_id

If you created your table like this

CREATE TABLE cultivar_db (
     cultivar_id NOT NULL AUTO_INCREMENT,
     cultivar_name VARCHAR(20),
     PRIMARY KEY (cultivar_id)
);

Then all you need for the insert is

$stmt = $conn->prepare("INSERT INTO cultivar_db(cultivar_name)
values(?)");
$stmt->bind_param('s', $cultivar_name);
$stmt->execute();

CodePudding user response:

If your "cultivar_id" is auto-increment, you do not need to add that id from the HTML. You can simply pass the "cultivar_name" from HTML using $_POST.

$stmt = $conn->prepare("INSERT INTO cultivar_db(cultivar_name) values(?)");
$stmt->bind_param('s', $_POST['cultivar_name']);
$stmt->execute();
  • Related