user10543225
user10543225

Reputation:

Storing data issue phpmyadmin

I am a beginner in php and using mySQL . I have been trying to store the form data but at the end when i click submit it says "This page isn't working ,if the problem continues, contact the site owner" . I have also created a database and a table on phpmyadmin and added four columns here is a screenshot https://i.sstatic.net/xTlHr.jpg

i have installed xampp and apache and mysql are running.

html
 <form action="insert.php" target="_blank" method="POST">

                        <input id="item-name-id" type="text" name="itemname" value="">

                        <select id="model" name="itemmodel">
                            <option value="Iphone 6/6S">Iphone 6/6S</option>
                            <option value="Iphone 6+/6S+">Iphone 6+/6S+</option>
                            <option value="Iphone 7/8">Iphone 7/8</option>
                            <option value="Iphone 7+/8+">Iphone 7+/8+</option>
                            <option value="Iphone X/XS">Iphone X/XS</option>
                            <option value="Iphone XR">Iphone XR</option>
                            <option value="Iphone XS Max">Iphone XS Max</option>
                            <option value="Iphone 11">Iphone 11</option>
                            <option value="Iphone 11 Pro">Iphone 11 Pro</option>
                            <option value="Iphone 11 Pro">Iphone 11 Max</option>
                            <!--Samsung model list-->
                        </select>

                        <input id="qty" type="number" name="itemquantity" min="0" value="0" required>
                        <select id="clr-list" name="itemcolor"></select>

                        <input type="submit" value="Submit">
                    </form><br>

insert.php
<?php
    $itemname = $_POST['itemname'];
    $itemmodel = $_POST['itemmodel'];
    $itemquantity = $_POST['itemquantity'];
    $itemcolor = $_POST['itemcolor'];

    if(!empty($itemname) || !empty($itemModel) || !empty($itemQuantity) || !empty($itemColor)){

        $host = "localhost";
        $dbUsername = "root";
        $dbPassword = "";
        $dbname = "registration";

        //create connection
        $conn = new mysqli("localhost","root", "pass","registration");

        if(mysqli_connect_error()){
            die('Connect Error('. mysqli_connect_errorno().')'. mysqli_connect_error());

        }
        else{
            $INSERT = "INSERT Into registration (itemname, itemmodel, itemquantity, itemcolor) values(?, ?, ?, ?)";

            //Prepare statement
            $stmt = $conn->prepare($INSERT);
            $stmt->bind_param("ssss", $_POST['itemname'], $_POST['itemmodel'], $_POST['itemquantity'], $_POST['itemcolor']);
            $stmt->execute();


            if($rnum==0){
                $stmt->close();
                $stmt = $conn->prepare($INSERT);
                $stmt->bind_param("ssssii", $itemname, $itemmodel,$itemquantity,$itemcolor);
                $stmt->execute();
                echo "New record inserted successfully";
            }

            $stmt->close();
            $conn->close();
        }
    }
    else{
        echo "All fields are required";
        die();
    }



?>

Upvotes: 0

Views: 289

Answers (3)

jithin
jithin

Reputation: 27

in your database fields having a space item name instead of space use underscrore

 $INSERT = "INSERT Into registration (item_name, item_model, item_quantity, item_color) values($_POST['item_name'], $_POST['item_model'], $_POST['item_quantity'], $_POST['item_color'])";

change in database to Item name -> item_name

Upvotes: 0

Bartosz Pachołek
Bartosz Pachołek

Reputation: 1308

1) first problem:

mysqli_connect_errorno

There is no such method. You have probably meant: mysqli_connect_errno

2) second potential problem:

you create variables which store your credentials:

$host = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbname = "registration";

but later input them again manually:

//create connection
$conn = new mysqli("localhost","root", "pass","registration");

change this to:

//create connection
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);

and be sure to set correct values in these credentials.

3) invalid column names

You have (based on the screenshot) set column names as follows:

"Item name" "Item model" "Item Quantity" "Item Color"

while later in the query you use:

(itemname, itemmodel, itemquantity, itemcolor)

If you provide these names then they must match - so change the query to:

$INSERT = "INSERT into registration (`Item name`, `Item model`, `Item Quantity`, `Item Color`) values(?, ?, ?, ?)";

or remove them from the query (this has consequences: then you need to for example match the order of arguments)

$INSERT = "INSERT Into registration VALUES (?, ?, ?, ?)";

4) debug

Add yourself:

            if (mysqli_errno($conn)) {
                die(mysqli_error($conn));
            }

after the prepare statement to see errors.

5) no color

In your db Item color is marked as NOT NULL therefore must have a value while your field for color is empty.

Add there some values - for example:

<option value="red">red</option>

6) aftewards - useless part:

This part is useless I believe:

            if($rnum==0){
                $stmt->close();
                $stmt = $conn->prepare($INSERT);
                $stmt->bind_param("ssssii", $itemname, $itemmodel,$itemquantity,$itemcolor);
                $stmt->execute();
                echo "New record inserted successfully";
            }

a) you do not have a variable rnum b) if you would have one with value 0 it would cause a second attempt to add the data to the database c) bind there is invalid: "ssssii" does not match fields` count (4)

Upvotes: 1

Ankit mehra
Ankit mehra

Reputation: 87

$host = "localhost";
    $dbUsername = "root";
    $dbPassword = "";
    $dbname = "registration";                                                                                                     your db name is cart in screenshort so write $dbname="cart" ;

and in registration table you write field name with space like item name change them to itemname or item_name

Upvotes: 1

Related Questions