-4

I have re-written this question because of all the down votes and it would seem that either no one understood the question or was unwilling to help a newbie. So I'll as it this way: Can someone tell/show me why this code does not work? (500 error)

OR... at the very least point me in the right direction? I AM WILLING TO LEARN, I just don't know where to begin or who/where to learn from as I am not sure what to even ask other than how do you run multiple "else" statements... that however left me more confused than I already am though!

Any "HELP" would be greatly appreciated!

<?php
$servername = "localhost";
$username   = "****";
$password   = "***";
$dbname     = "***";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

$bname    = $_POST['bname'];
$baddress = $_POST['baddress'];
$bcity    = $_POST['bcity'];
$bstate   = $_POST['bstate'];
$zipcode  = $_POST['zipcode'];

// Check connection
if ($conn->connect_error)
  {
    die("Connection failed: " . $conn->connect_error);
  }

$sql    = "SELECT Login FROM `USERS` WHERE Business_Name = '" . $_POST["bname"] . "' AND Business_Address = '" . $_POST["baddress"] . "' AND Business_State = '" . $_POST["bstate"] . "' AND Business_Zip = '" . $_POST["zipcode"] . "' LIMIT 1";
$result = $conn->query($sql);

if ($result->num_rows > 0)
  {
    // output data of each row
    while ($row = $result->fetch_assoc())
      {
        echo "https://www.***.com/realsite.php?Login=" . $row["Login"] . "";
      }
  }
else
  {
    $sql    = "SELECT Login FROM `DATA` WHERE Business_Name = '" . $_POST["bname"] . "' AND Business_Address = '" . $_POST["baddress"] . "' AND Business_State = '" . $_POST["bstate"] . "' AND Business_Zip = '" . $_POST["zipcode"] . "' LIMIT 1";
    $result = $conn->query($sql);

    if ($result->num_rows > 0)
      {
        // output data of each row
        while ($row = $result->fetch_assoc())
          {
            echo "https://www.***.com/demo.php?Login=" . $row["Login"] . "";
          }
      }
    else
      {
        $sql = "INSERT INTO `DATA` (Business_Name, Business_Address, Business_City, Business_State, Business_Zip)
                SELECT '$bname', '$baddress', '$bcity', '$bstate', '$zipcode' FROM (SELECT 1) t
                WHERE NOT EXISTS (SELECT Login, Business_Name,Business_Address FROM `DATA` WHERE Business_Name='$bname' AND Business_Address='$baddress')";


        if (mysqli_query($conn, $sql))
          {

            echo "<a href='https://www.servedwell.com/realsite.php?Login=" . $row["Login"] . "'>LINK</a>";

          }        
        else
          {
            echo "Error: " . $sql . "<br>" . mysqli_error($conn);
          }
      }
    mysqli_close($conn);

  }
?>
11
  • 3
    First of all, please post this code with some sort of proper indentation, so that it is more easily visible what belongs to what.
    – 04FS
    Oct 28, 2019 at 11:03
  • 1
    So what exactly is stopping you from making your third query in the else branch of the second part then?
    – 04FS
    Oct 28, 2019 at 11:04
  • 04FS: I'll read up on "proper indentation". I have no idea what that even means in relation to scripts but I'm sure it's important. As for what is stopping me.... I've tried putting it in the "{ echo "Error: " . $sql . "<br>" . mysqli_error($conn); }" area and bumping down the error, but it breaks the script entirely. You did read the opening statement about me being one of those "nubes" right? I would not be here if I already new the answer ;-)
    – Thor
    Oct 28, 2019 at 11:25
  • Your code is vulnerable to SQL injection. You should use prepared statements.
    – Dharman
    Oct 28, 2019 at 11:36
  • Dharman: Thank you. Finally got some info I can actually use and learn from! As to my question that now has four(4) down votes, haha....(WOW, just wow... ) Any idea how to accomplish what I am asking?
    – Thor
    Oct 28, 2019 at 12:38

1 Answer 1

0

Credit goes to @Ancaron for properly formatting code and correcting a syntax error. Final posted code works fine.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.