2014-04-27 186 views
0

我創建了一個php函數,允許用戶將他們的地址保存在數據庫中。我的問題是,部分代碼根本無法運行。該代碼在$result2= "SELECT * FROM Addressv4 WHERE Userid = '".$id."'";php代碼中途停止運行

停止運行它,然後開始工作,當它到達此行的代碼$insert_query = "INSERT INTO Addressv4 (Userid, Housenumber, Street, Town, Postcode, DefaultAddress) values ('$id', '$Number', '$Street', '$Town','$Postcode', '1')";

無論是運行代碼的時候我還沒有收到任何語法錯誤。

任何幫助將不勝感激。

<?php 

include 'dbconnect.php'; 

$connection = mysqli_connect($db_host, $db_username, $db_password, $db_database); 
// Check connection 
if (mysqli_connect_errno($connection)) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

// Getting data from HTML Form 
$Number = $_POST['streetnumber']; 
$Street = $_POST['street']; 
$Town = $_POST['town']; 
$Postcode = $_POST['postcode']; 
$Username = $_POST['Username']; 

$sql = mysqli_query($connection, "SELECT * FROM Userv2 WHERE Username = '".$Username."'"); 

if ($sql){ 
    while($row = mysqli_fetch_array($sql)){ 

     $id = $row['Id']; 

    } 
} 

$result2= "SELECT * FROM Addressv4 WHERE Userid = '".$id."'"; 

$sql1 = mysqli_query($connection, $result2); 
$count = count($sql1); 

if($count >=1){ 

    echo 'Sorry you can only have 1 default address'; 
} 

$insert_query = "INSERT INTO Addressv4 (Userid, Housenumber, Street, Town, Postcode, DefaultAddress) 
values ('$id', '$Number', '$Street', '$Town','$Postcode', '1')"; 


$result = mysqli_query($connection, $insert_query); 

header("Location: http://sots.brookes.ac.uk/~10031187/viewaddress.php"); 

mysqli_close($connection); 
?> 
+1

你怎麼知道那兩行之間沒有跑? – lagbox

+0

當我在這些行之間放置回顯或var轉儲時,什麼都沒有發生 – Dolaps

+0

當你在$ result2 =後面加上var_dump = ....它不顯示? – lagbox

回答

1

也許是更好地使用

SELECT COUNT(Userid) AS countId FROM.. 
if ($row['countId'] > 1) { 

這樣的查詢將始終返回的東西,現在有一定機率使你的查詢可以返回false ..

什麼的var_dump輸出($ SQL1); ?

0

$ sql1是resulset。你不能計算這樣的行數。 Try:

$sql1_count = mysqli_num_rows($sql1)