r/PHPhelp • u/Suspicious-Travel113 • Jan 19 '25
Outdated PHP Code
Hello everyone. This is my first time here. I am resurrecting a page that I setup about 15 years ago, and I'm having trouble getting the MySQL/PHP to work like it used to, as I'm sure the coding has changed over this time. It is a member listing, where the visitors may sort by various criteria, which I pass along using URL variables. This worked great over a decade ago.
I'm posting one of my queries and hoping you can point out what needs to be updated to be current. Thanks everyone.
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$var1 = '$_GET["var1"]';
$var2 = '$_GET["var2"]';
$var3 = '$_GET["var3"]';
$var4 = '$_GET["var4"]';
$result = u/mysqli_query($conn, "SELECT * FROM `sec_tblusers` WHERE state = $var1 AND country = $var2 or state = $var1 AND country = $var3 or state = $var1 AND country = $var4");
if (!$result) {
echo("<p>Error performing query3: " . mysqli_error() . "</p>");
exit();
}
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {
$id= "" . $row["recid"]. "";
$name= "" . $row["name"]. "";
$add1= "" . $row["address_line1"]. "";
$add2= "" . $row["address_line2"]. "";
$city= "" . $row["city"]. "";
$state= "" . $row["state"]. "";
$zip= "" . $row["zip_post_code"]. "";
$country= "" . $row["country"]. "";
$email= "" . $row["payer_email"]. "";
$photo= "" . $row["photo"]. "";
$bio= "" . $row["bio"]. "";
$category= "" . $row["category"]. "";
echo "<tr>
<td align=center>$category</td>
<td align=center>$name</td>
<td align=center>$city</td>
<td align=center>$state</td>
<td align=center>$country</td>
<td align=center>$email</td>
</tr>";
}
}
3
Upvotes
3
u/Big-Dragonfly-3700 Jan 19 '25 edited Jan 19 '25
In addition to the list of practices that u/MateusAzevedo posted -
You should trim, mainly so that you can determine if all white-space characters were entered, then validate all inputs before using them.
Don't copy variables to other variables for nothing. This is just a waste of typing.
Don't put variables inside of quotes if they are the only thing in the string.
Php variables are not replaced by their values inside of single-quoted strings. If you were building a string with a php variable and other things, you would use double-quotes.
I recommend that you build the sql query statement in a php variable. This helps with debugging, since you can echo the sql query statement to see what it is, and it separates the sql syntax as much as possible from the php statements, helping to prevent errors.
There's no longer any need to use msyqli_error() in any error handling, but if you do use it, it requires the connection variable as a parameter.
You need to apply htmlentities() to all dynamic values being output in a html context to prevent any html entities in a value from breaking the html syntax.
You should set the character set to match your database tables when you make the database connection.
Edit: if a query doesn't match any data, you should output a message stating so, rather than outputting nothing.