PHP代码无法正常工作!无法执行mysqli查询

时间:2016-04-14 12:01:12

标签: php mysqli

所以我的代码有两个版本

  1. 哪个不起作用
  2. 哪个有效...
  3. 此代码不起作用:

    <?php
    $status=0;
    $user_id=$friend_id="";
    $user_name=$_COOKIE['usernam'];
    $friend_name=$_POST['text'];
    $connect=mysqli_connect("localhost","root","*censored*","mydbrun");
    $sql1="SELECT id FROM users WHERE username='".$user_name."'";
    $result=mysqli_query($connect,$sql1);
    if(mysqli_num_rows($result)>0)
    {
        $row=mysqli_fetch_array($result);
        $user_id=$row['id'];
    }
    $connect1=mysqli_connect("localhost","root","*censored*","mydbrun");
    $sql2="SELECT id FROM users WHERE username='".$friend_name."'";
    $result1=mysqli_query($connect1,$sql2);
    if(mysqli_num_rows($result1)>0)
    {
        $row1=mysqli_fetch_array($result1);
        $friend_id=$row1['id'];
    }
    $connect2=mysqli_connect("localhost","root","*censored*","mydbrun");
    $sql="INSERT INTO friends(person_one,person_two,status)           VALUES('".$user_id."','".$friend_id."','".$status."')";
    if(mysqli_query($connect2,$sql)){
        echo "Request sent";
    }
    else{
        echo "Request could not be sent";
    }
    ?>
    

    显示“请求无法发送” 但是当我尝试这段代码时......

    <?php
    $status=0;
    $user_id=$friend_id="";
    $user_name="rishav37";
    $friend_name="vivek";
    $connect=mysqli_connect("localhost","root","*censored*","mydbrun");
    $sql1="SELECT id FROM users WHERE username='".$user_name."'";
    $result=mysqli_query($connect,$sql1);
    if(mysqli_num_rows($result)>0)
    {
        $row=mysqli_fetch_array($result);
        $user_id=$row['id'];
    }
    $connect1=mysqli_connect("localhost","root","*censored*","mydbrun");
    $sql2="SELECT id FROM users WHERE username='".$friend_name."'";
    $result1=mysqli_query($connect1,$sql2);
    if(mysqli_num_rows($result1)>0)
    {
        $row1=mysqli_fetch_array($result1);
        $friend_id=$row1['id'];
    }
    $connect2=mysqli_connect("localhost","root","*censored*","mydbrun");
    $sql="INSERT INTO friends(person_one,person_two,status)           VALUES('".$user_id."','".$friend_id."','".$status."')";
    if(mysqli_query($connect2,$sql)){
        echo "Request sent";
    }
    else{
        echo "Request could not be sent";
    }
    ?>
    

    但是此代码有效!我甚至检查了$_COOKIE['usernam']$_POST['text']会发生什么,他们已经存储了“rishav37”和“vivek”......我不知道为什么以前的代码不起作用! 有人帮忙,先谢谢。

0 个答案:

没有答案