2016-05-27 8 views
-2

なぜこのクエリUPDATEが初めて動作するのか説明できますか?ページを更新して2回目の記録を試みると、latlngの値を除くすべてが記録されます。UDDATEクエリは1回だけうまく動作します

<?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クエリー機能と思われ、その後、あなたはこのような何かについてどう

+0

クラッシュはどういう意味ですか? –

+1

それが一度しか動作しない場合は、あなたが得なければならない制約エラーを私たちに伝えることを気にしませんでした。 –

+0

あなたはまだ 'mysql_'と' mysqli'エラーがあります。 mysql_ *は使わないでください。 – chris85

答えて

0

形で第2の充填をしようとするとクラッシュしますか?あなたは更新クエリを作成する必要はありません、あなたは1つだけの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 
    } 
} 
?> 
関連する問題