UDDATE查询只能运行一次

时间:2016-05-27 18:17:22

标签: php mysql

有人可以向我解释为什么此查询UPDATE仅在第一次有效。如果您更新页面并尝试进行第二次录制,则会记录除latlng值以外的所有内容。

<?php
include("database.php");

$error = $lat = $lng = '';;
if(isset($_POST['submit']))
{
  $username = mysql_real_escape_string($_POST['username']);
  $name = mysql_real_escape_string($_POST['name']);
  $surname = mysql_real_escape_string($_POST['surname']);
  $affiliation = mysql_real_escape_string($_POST['affiliation']);
  $department = mysql_real_escape_string($_POST['department']);
  $address = mysql_real_escape_string($_POST['address']);
  $position = mysql_real_escape_string($_POST['position']);
  $email = mysql_real_escape_string($_POST['email']);
  $web = mysql_real_escape_string($_POST['web']);
  $telephone = mysql_real_escape_string($_POST['telephone']);
  $mobile = mysql_real_escape_string($_POST['mobile']);
  $password = $_POST['password'];
  $passwordConfirm = $_POST['passwordConfirm'];
  $privacy = $_POST['privacy'];

  //validare i valori inseriti dall'utente
  if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  {
    $error = "Inserisci una email valida ";
  }

  else if (strlen($password < 8)) {
    $error = "La password deve contenere almeni 8 caratteri";
  }

  else if ($password != $passwordConfirm)
  {
    $error = "Le password devono coincidere!";
  }

  else {
    $error = "Ti sei appena registrato su";
  }


  $sql = "INSERT INTO users(username, name, surname, affiliation, department,address,position,email,web,telephone,mobile,password,privacy) VALUES('$username','$name','$surname','$affiliation','$department','$address','$position','$email','$web','$telephone','$mobile','$password','$privacy')";
  mysqli_query($database,$sql) or die(mysqli_error($database));

  if($address !=''){
    $request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$address."&sensor=true";
    $xml = simplexml_load_file($request_url) or die("url not loading");
    $status = $xml->status;
    if ($status=="OK"){
      $lat = $xml->result->geometry->location->lat;
      $lng = $xml->result->geometry->location->lng;

    }
    $sql1 = "UPDATE users SET lat='$lat',lng='$lng'  WHERE username='$username'";
    mysqli_query($database,$sql1) or die(mysqli_error($database));
  }
}
?>

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" href="css/reset.css">
  <link rel="stylesheet" href="css/style.css">

</head>

<body>
  <div class="pen-title">
    <h1>B-Where</h1>
  </div>
  <!-- Form Module-->
  <div class="form-module">
    <div class="registration">
    </div>
    <div class="form">
      <h2>Account Information</h2>
      <form role="form" action="registration.php" method="post">
        <input  type="text" name="username" placeholder="Username"/>
        <input  type="text" name="name" placeholder="Name"/>
        <input  type="text" name="surname" placeholder="Surame"/>
        <input  type="text" name="affiliation" placeholder="Affiliation"/>
        <input  type="text" name="department" placeholder="Department"/>
        <input  type="text" name="address" placeholder="Address"/>
        <input  type="text" name="position" placeholder="Position"/>
        <input  type="text" name="email" placeholder="Email"/>
        <input  type="text" name="web" placeholder="Web"/>
        <input  type="text" name="telephone" placeholder="Telephone"/>
        <input  type="text" name="mobile" placeholder="Mobile"/>
        <input  type="password" name="password" placeholder="password"/>
        <input  type="password" name="passwordConfirm" placeholder="passwordConfirm"/>
        <input type="submit" name="submit" value="INVIA"  />

      </form>
    </div>
  </div>

</body>
</html>

UPDATE查询似乎仅运行一次,如果您尝试在表单中填写第二个内容,则会崩溃

1 个答案:

答案 0 :(得分:0)

这样的事情怎么样?您不需要进行更新查询,只能进行一次MySql查询。

<?php
include("database.php");

$error = $lat = $lng = '';;
if(isset($_POST['submit']))
{
  // your code

  if(empty($error)) { // check if there is no error
    if(!empty($address)) {
      $request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$address."&sensor=true";
      $xml = simplexml_load_file($request_url) or die("url not loading");

      if ($xml->status == "OK") {
        $lat = (string) $xml->result->geometry->location->lat;
        $lng = (string) $xml->result->geometry->location->lng;
      }
    }

    $sql = "INSERT INTO users(username, name, surname, affiliation, department,address,position,email,web,telephone,mobile,password,privacy,lat,lng) VALUES('$username','$name','$surname','$affiliation','$department','$address','$position','$email','$web','$telephone','$mobile','$password','$privacy','$lat','$lng')";
    mysqli_query($database,$sql) or die(mysqli_error($database));
  } else {
     // some error handling
  }
}
?>