PHP and MySQL Problem

Status
Not open for further replies.

MisterVertigo

Smack-Fu Master, in training
58
Hello all! I have what I’m going to guess is a REALLY simple PHP/MySQL coding question that I’m just stumped on. I’ll warn you that this is going to be kind of long, but I want to provide as much info about what I’m trying to do so you guys have all the info to help me out.

I’m in the process of re-writing some old PHP code. The current code is being used in an old CMS that I don’t want to use anymore. I’m re-writing it so it just straight PHP and I can put it anywhere. Anyway…

In my SQL statement I have two tables I’m doing a LEFT JOIN on. The tables are called trooptrack_costumes and trooptrack_memcostumes. Here is what they look like:

Code:
Trooptrack_costumes
+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| id          | int(11)     | NO   | PRI | NULL    | auto_increment |
| designation | char(2)     | NO   |     |         |                |
| costume     | varchar(30) | NO   |     |         |                |
| hidden      | tinyint(1)  | NO   |     | 0       |                |
| sort        | tinyint(1)  | NO   |     | 0       |                |
+-------------+-------------+------+-----+---------+----------------+

Code:
trooptrack_memcostumes
+-----------+---------+------+-----+---------+----------------+
| Field     | Type    | Null | Key | Default | Extra          |
+-----------+---------+------+-----+---------+----------------+
| id        | int(11) | NO   | PRI | NULL    | auto_increment |
| tkid      | char(5) | NO   |     | 00000   |                |
| costumeid | int(11) | NO   |     | 0       |                |
+-----------+---------+------+-----+---------+----------------+

The costumes table is just a list of costumes that is used on a form where an admin can pick the costumes a person has. Since a member can have more than one, I made the memcostumes table which stores the member id (tkid) and the costumeid of the costumes they have. So, the values memcostumes.costumeid should match up with a record in costumes.id.

In my PHP code I’m running the following SQL statement:
Code:
$sql7 = 'SELECT trooptrack_costumes.id, trooptrack_memcostumes.tkid, designation, costume, sort FROM trooptrack_costumes LEFT JOIN trooptrack_memcostumes ON trooptrack_costumes.id = trooptrack_memcostumes.costumeid AND trooptrack_memcostumes.tkid = '.$id.' ORDER BY sort, designation';
$result7 = mysqli_fetch_array($conn, $sql7);

In this statement, $id is a variable I declared at the top of the code as well as $conn. I’m using those same variables for other queries on this page and they all work fine (that is why this one is $sql7). If I run this statement in phpMyAdmin I get the results I expect. It looks something like this:

7IOiR4X.jpg


In the tkid column I get NULL values unless there is a record for the chosen TKID in the memcostumes table. You will see there are a couple of rows here with '00001' in the tkid field. These are the ones that match up in memcostumes.

The problem is when I try to use any of the results later in my PHP code. I'm trying to do something like this in a form:
Code:
		<TD>
		<?php 
		while ($row7=mysqli_fetch_array($result7,MYSQLI_ASSOC)) {
			if ($row7["tkid"] == NULL) { ?>
				<input type="checkbox" name="costumes[]" value="<?php echo $row7["id"]; ?>">&nbsp;<?php echo $row7["designation"]; ?> - <?php echo $row7["costume"]; ?>
				<BR>
			<?php }
			else { ?>
				<input type="checkbox" name="costumes[]" value="<?php echo $row7["id"]; ?>" checked>&nbsp;<?php echo $row7["designation"]; ?> - <?php echo $row7["costume"]; ?><BR>
			<?php } } ?>
		</TD>

Any thoughts as to what I may be doing wrong? It acts like there are no results of the query. I'm pretty sure my SQL syntax is right, as it works in phpMyAdmin. I'm just not sure what I'm missing. Any help would be greatly appreciated!
 
First of all, don't use mysqli, use something that supports sane prepared statements, such as PDO...

edit: to be more helpful, I haven't used mysqli in a while, but you are calling mysqli_fetch_array() twice, once when you are assigning it to result, and again when you are looping through your result set. This stackexchange shows how to loop through a sql result set:
http://stackoverflow.com/questions/9243 ... tire-array
You'll notice that you're not actually executing a query.
 

MisterVertigo

Smack-Fu Master, in training
58
Well, that is something. I honestly have no idea what that is as I barely know what I'm doing anyway. It's something I'll look into, but at a glance it looks like something that would require me to rewrite 100% of my code. I'm not willing to do right now.

I will take a look at your link. I'm sure it's something obvious and I've just been staring at it too long. Thank you!

EDIT: I see what you said about executing the query, that was elsewhere in the code and I realized I forgot to post that. This is at the top of the page:

Code:
$sql7 = 'SELECT trooptrack_costumes.id, trooptrack_memcostumes.tkid, designation, costume, sort FROM trooptrack_costumes LEFT JOIN trooptrack_memcostumes ON trooptrack_costumes.id = trooptrack_memcostumes.costumeid AND trooptrack_memcostumes.tkid = '.$id.' ORDER BY sort, designation';
$result7 = mysqli_fetch_array($conn, $sql7);
 

Drizzt321

Ars Legatus Legionis
34,922
Subscriptor++
I honestly have no idea what that is as I barely know what I'm doing anyway.

This is a big trouble sign for me. Do you mean you don't really know PHP at all? Or you don't really know programming at all? If this is a work/business thing, I'd recommend hiring someone to do the conversion, or to migrate it to a newer/better CMS. Honestly, with all of the crap that people do on the Internet (and they LOVE to target PHP pages) it's better to go through someone who actually knows what they are doing and has kept up to date. Are you also going to be maintaining this? Are you providing sufficient documentation for the person who will (eventually) be maintaining it? Lots of other points can be made and some are made by others already in this thread.
 

MisterVertigo

Smack-Fu Master, in training
58
I appreciate your concerns. This is NOT at all for a business or for anything like that. This is strictly a hobby. While others will be using it, I'll be the one making any changes or anything to it. It's not anything where uptime is business critical. None of the code I'm working on so far is even exposed to the internet yet.

With that being said, I've been doing PHP for quite a while now, I'm just not a professional at it. I'm an IT guy by trade, and I taught myself PHP, HTML, CSS, and MySQL in my spare time. I've done some cool things, but I'll NEVER consider myself a real "coder". The end-goal of this project is to use this custom code I'm writing and embed it into pages controlled by a quality CMS. The CMS handles all the complicated parts in regards to logins, security, etc, and my pages will be added on to the back-end. I just want to get some data stored in a database, and have a pretty HTML page display it for visitors.

Once I get everything re-written and working I'm planning on going back and learning about prepared statements and PDO. Anyone have a good link as to where a noob like myself can learn about what prepared statements are, why they are better, and how to use them? The link above about PDO kind of assumes you already know what you are looking at.
 

Drizzt321

Ars Legatus Legionis
34,922
Subscriptor++
Ah, ok. So much more hobby/learning kind of thing, OK. You still should do prepared statements/PDO and such. More specifically, here's a Mitre page, 2011 CWE/SANS Top 25 Most Dangerous Software Errors which roughly boils down to:

  • SQL Injection
  • OS Command Injection
  • Cross-site Scripting
  • Unrestricted Upload of File with Dangerous Type
  • Cross-Site Request Forgery (CSRF)
  • URL Redirection to Untrusted Site ('Open Redirect')

There's lots of information out there (including links from that page) that can help you understand what they are, and then also how to mitigate them using a given language (in your case PHP + MySQL, and presumably Linux + Apache).

As for PDO references, obviously PHP.net reference, and from a brief glance https://phpdelusions.net/pdo seems good.

EDIT: A lot of PHP programmers are folks like you who picked it up as a secondary thing and don't program in general much otherwise, or are turned out through "Web Developer" certification programs and may not think as much about (although I never took any of those) good security and mitigation as part of their up-front development and as things to tack on after it's all built, or use whatever Wordpress/etc security stuff exists. And PHP itself doesn't really help enforce good practices, sometimes the opposite, and so it's very much targeted by all sorts of people and commonly exploited.
 
Once I get everything re-written and working I'm planning on going back and learning about prepared statements and PDO. Anyone have a good link as to where a noob like myself can learn about what prepared statements are, why they are better, and how to use them? The link above about PDO kind of assumes you already know what you are looking at.

In a nutshell, bad things will happen if $id isn't a number but instead something like:
Code:
"1;DROP TABLE trooptrack_memcostumes;--"
A whole bunch of your data will get deleted.

Using prepared statements tells the database that $id is strictly data and nothing in $id should be interpreted as a query or queries. You can use prepared statements with mysqli, too. The default behavior is arguably more secure than PDO.
 
Status
Not open for further replies.