Jump to content

PHP assistance plesae


apellegrino

Recommended Posts

In my class we are to insert an H1 heading into our webpage using PHP. I have the code, but I can't figure out how to remove the multiple headings within the table. Everything in the code is what is required. Attached is an image of the page. Please help<!-- This first line is used to get the database connection information --><?php require_once('dbconnect.php'); ?><?php//The SELECT statement is pulling two columns (content and cat_name) from the table name and ad_table//The only row(s) being returned are rows where the cat_code is equal to the numeric value being passed from the categories menu selection$cat = (isset($_GET['cat']) ? $_GET['cat'] : null);$result = mysqli_query($link, "SELECT content, cat_name FROM ad_table WHERE cat_code='$cat'");if (!$result){$error = 'Error fetching data: ';$e= mysqli_error($link);echo ('bad results');echo($e);//exit();}else{$heading = "xxxx"; // sets heading to garbage so can display correct headingwhile ($row = mysqli_fetch_array($result)){if($heading != $row['cat_name']) { //checks to see if heading is different//?> <h1><?phpecho $row['cat_name']; ?></h1><?php$heading = $row['cat_name'];}// table to dispaly content results?><table width="400" border="1" cellpadding="0"><tr><td><?php echo $row['content']; ?></td></tr></table><?php} // ends the while loop} // ends the if statement?>

post-175435-0-27698500-1414020254_thumb.jpg

Link to comment
Share on other sites

The if statement inside the while loop is printing the heading if it is different from the heading for the last record. If that's not what you want to do, then what do you want to do? You can remove that entire if statement to not print the headings at all. Also, tell your teacher that I'm very disappointed that he's not teaching you about prepared statements. Your code is vulnerable to SQL injection attacks. If you want to see how it's vulnerable, you can change the URL like this so that it will print every record from the database instead of only those for 1 category. Your URL has a value on the end called cat, maybe it looks like this:page.php?cat=123Change the value of cat to this, and it will print every record in the database:page.php?cat=%27%20OR%20%271%27%3D%271The reason that works is because your page is vulnerable to SQL injection because you aren't using prepared statements, so hopefully he teaches that also.

Link to comment
Share on other sites

This is just a beginner's php class, probably why they haven't taught us about prepared statements. I honestly don't know if the if statement is required, but it was in the code they supplied.

Now they're asking to place an H1 heading to the page. Meaning if a user clicks on one the categories, than the heading specifies which category they're viewing; but there is multiple headings within the table, and I only want one. Does that help?

Link to comment
Share on other sites

Well, it looks like it is printing multiple categories, which one should it print on top? Under the heading where it says "Categories", it says "Your menu selection was Pets!" Is that the one they want you to show? If so, look at the code for where it prints that, and copy it to another part of the page to print in your h1.

Link to comment
Share on other sites

Then you need to remove the code that prints those, which is the if statement. Look at the code that prints the line on the bottom telling you what you chose, and use the same value to print the heading on top before the loop.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...