php代码中途停止运行

时间:2014-04-27 11:08:16

标签: php

我创建了一个php函数,允许用户在数据库中保存他们的地址。我的问题是部分代码根本没有运行。代码停止在$result2= "SELECT * FROM Addressv4 WHERE Userid = '".$id."'";

运行

然后当它到达这行代码$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);
?>

2 个答案:

答案 0 :(得分:1)

也许最好使用

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

这样查询总会返回一些东西,现在你的查询有可能返回false ..

var_dump($ sql1)的输出是什么; ?

答案 1 :(得分:0)

$ sql1是一个结果集。你无法计算这样的行数。 试试:

$sql1_count = mysqli_num_rows($sql1)