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://imgur.com/d4P9gfI
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();
}
?>
3
Answers
and in registration table you write field name with space like item name change them to itemname or item_name
1) first problem:
There is no such method. You have probably meant:
mysqli_connect_errno
2) second potential problem:
you create variables which store your credentials:
but later input them again manually:
change this to:
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:
If you provide these names then they must match – so change the query to:
or remove them from the query (this has consequences: then you need to for example match the order of arguments)
4) debug
Add yourself:
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:
6) aftewards – useless part:
This part is useless I believe:
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 databasec) bind there is invalid: “ssssii” does not match fields` count (4)
in your database fields having a space item name instead of space use underscrore
change in database to Item name -> item_name