在这里我要使用更新查询并以相同的形式插入查询。这意味着如果给定数据在数据库中可用,它将执行更新查询,否则将执行插入查询。
这是我的代码。如何将其更改为上述情况?
<?php
include 'database.php';
if(isset($_POST['submit'])) { // Fetching variables of the form which travels in URL
$fname = $_POST['firstName'];
$lname = $_POST['lastName'];
$sname = $_POST['screenName'];
$year = $_POST['year'];
$month = $_POST['month'];
$date = $_POST['date'];
$gender = $_POST['gender'];
$country = $_POST['Country'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$password = $_POST['password'];
$cpassword = $_POST['confirmPassword'];
$dob = $year . "-" . "$month" . "-" . "$date";
$statement=$_POST['statement'];
$query = "INSERT INTO user(firstName,lastName,screenName,dob,gender,country,email,phone,password) VALUES ('$fname','$lname','$sname','$dob','$gender','$country','$email','$phone','$password') ON DUPLICATE KEY UPDATE (firstName='$fname', lastName='$lname', screenName='$sname',dob='$dob'gender='$gender',country='$country',phone='$phone', password ='$password')";
// $queryupdate = "UPDATE user SET firstName='$fname',lastName='$lname',screenName='$sname',dob='$dob',gender='$gender',country='$country',email='$email',phone='$phone',password='$password' WHERE email='$email'";
if ($password == $cpassword && $statement=='accept') {
if (mysqli_query($connection, $query)) {
echo '<script language="javascript">';
echo 'alert("data successfully inserted")';
echo '</script>';
}
} else {
echo '<script language="javascript">';
echo 'alert("Error..")';
echo '</script>';
}
mysqli_close($connection); // Closing Connection with Server
}
现在它没有显示任何错误。.但是,它不会在重复的条目上更新。