douwayuan3063 2014-04-27 11:08
浏览 40
已采纳

php代码中途停止运行

I have created a php function that allows users to save their address on the database. My issue is that part of the code doesn't run at all. The code stops running at $result2= "SELECT * FROM Addressv4 WHERE Userid = '".$id."'";

It then starts working when it reaches this line of code $insert_query = "INSERT INTO Addressv4 (Userid, Housenumber, Street, Town, Postcode, DefaultAddress) values ('$id', '$Number', '$Street', '$Town','$Postcode', '1')";

I haven't received any syntax errors when running the code either.

Any help would be grateful.

<?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条回答 默认 最新

  • duanqiao1947 2014-04-27 11:26
    关注

    maybe it's better to use

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

    that way the query will always return something, now there is a chance your query can return false..

    what is the output of var_dump($sql1); ?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测