<html>
<?php
$con = mysqli_connect("localhost","root","","test1") or die ("Cannot able to connect");
?>
<body bgcolor="cyan">
<form method="POST">
<?php
$query = mysqli_query($con,"SELECT * FROM state");
$rowcount = mysqli_num_rows($query);
?>
<select name="Statename">
<option value=""> --Select One--</option>
<?php
for ($i=1; $i <=$rowcount ; $i ) {
$row = mysqli_fetch_array($query);
?>
<option><?php echo $row['State_Name']?></option>
<?php
}
?>
</select>
State Name:
<input type="text" name="State_Name">
<input type="Submit" name="Submit">
</form>
</body>
<?php
if (isset($_POST['Submit'])) {
$State_Name = $_POST['State_Name'];
$query1 = mysqli_query($con,"INSERT INTO state(State_Name) VALUES('$State_Name')");
if ($query1 === TRUE)
{
echo "Inserted";
}
else
{
echo "Error: ";
}
}
?>
</html>
When I enter state name it is inserting twice in data table [Table in phpmyadmin] I am trying to store state name in table and after inserting that data all that state name will be shown in dropdown list But when i insert data it is storing data twice in my table Please help me for it Entering State name as 'UP' AND Shows state name 'UP' twiceUP]
CodePudding user response:
The problem is that your table column State_name accepts duplicate records, so any time you reach the page with the same option selected your script inserts the duplicity.
Use PHP MyAdmin to add an unique key on the State_name column.
This will protect inserting any duplicity into that column - of course it will raise an sql error.
