I am trying to insert the input received from a text box in a table. The result I am getting in php page are below: Before entering the input, on page load i get Connected successfullyError: INSERT INTO tested (itemno, item, quantity) VALUES ('1', , )
On entering the input and submitting, I get the result , connected successfully and item is not empty msg but the query is not executed and a row is not getting inserted. Can someone help me?
<!DOCTYPE html>
<html>
<body>
<?php
$servername = "localhost";
$username = "learnphp@localhost";
$password = "password";
$dbname = "my_learnphp";
$result="";
$result1="";
$item = $_POST["item"];
$quantity = $_POST["quantity"];
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method = "POST">
Item name: <input type="text" name="item"><br>
Quantity : <input type="number" name="quantity"><br>
<input type="submit" name="upd" value="Insert"/>
<input type="submit" name="item_update" value="Update"/>
</form>
<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);}
echo "Connected successfully";
$sql = "INSERT INTO tested (itemno, item, quantity) VALUES ('1', $item, $quantity)";
if (!empty($item)) {
echo "item is not empty";
$result = $conn->query($sql);
if($result){
echo $_POST["item"];
echo $_POST["quantity"];
echo "New record created successfully";
}
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
</body>
</html>