Listbox shows blank line space below each element. Increased the width also but still the blank line appears. I have attached the screenshot for reference.I have attached the code file. I am using a function to select the records from the dropdown list . The dropdown list is working but blank record shows below every value which is shown in the screenshot.
<?php
$selected = '';
function get_options($select) {
$categories = array('Information Technology' => 1, 'Management' => 2);
$options = '';
while (list($k, $v) = each($categories)) {
if ($select == $v) {
$options .= '<option value="' . $v . '" selected>' . $k . '<option>';
} else {
$options .= '<option value="' . $v . '" >' . $k . '<option>';
}
}
return $options;
}
require_once('dbconnect.php');
if (isset($_POST['categories'])) {
$selected = $_POST['categories'];
echo $selected;
}
if ($selected == 1) {
$selectedcat = 'Information Technology';
$selectsql = "SELECT * FROM courses where ccategory='$selectedcat'";
} else
if ($selected == 2) {
$selectedcat = 'Management';
$selectsql = "SELECT * FROM courses where ccategory='$selectedcat'";
} else {
$selectsql = "SELECT * FROM courses";
}
//require_once('dbconnect.php');
include('header-basic-light.php');
//$selectsql="SELECT * FROM courses";
$res = (mysqli_query($con, $selectsql));
if (!mysqli_query($con, $selectsql)) {
die(mysqli_error($con));
}
mysqli_close($con);
//header('Location:index.php');
?>
<HTML>
<head>
<title>"View Information"</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="categories">Select the Category : </label>
<select name="categories" style="width:250px;" onchange="this.form.submit();">
<?php echo get_options($selected); ?>
</select>
</form>
<h2>View Information</h2>
<table class="table">
<tr>
<th>#</th>
<th>cname</th>
<th>start_date</th>
<th>duration</th>
<th>Remarks</th>
<th>Options</th>
</tr>
<?php
while ($r = mysqli_fetch_assoc($res)) {
?>
<tr>
<td><?php echo $r['cno']; ?></td>
<td><?php echo $r['cname']; ?></td>
<td><?php echo $r['start_date']; ?></td>
<td><?php echo $r['duration']; ?></td>
<td><?php echo $r['remarks']; ?></td>
<?php
if ($r['ccategory'] == 'Information Technology') {
$catnum = 1;
}
if ($r['ccategory'] == 'Management') {
$catnum = 2;
}
?>
<td>Details  
</tr>
<?php
}
?>
</table>
</body>
</html>
Listbox screenshot
1st : You missed forward slash in option close tag </option>
$options.='<option value="'.$v.'" selected>'.$k.'</option>';
2nd : Try to use prepared statement to avoid sql injection .
Related
I have the following code in my program that works perfectly with the WAMP server. But I had to change the WAMP server for XAMPP and I don’t know why it doesn’t execute some parts of the code. There’s also no error message.
The problem is that in the table code <table>, it doesn’t show after the third <tr> or when the 'Toggle' starts. It also doesn’t show the submit button.
I don’t understand why everything works correctly in WAMP but not in XAMPP. Can someone help me see why it fails? Thank you
Project.php
<?php
include_once("Conexion.php");
include_once("functions.php");
ob_start();
$_SESSION['ex_time'] = date('Y-m-d H:i:s');
$list_ex = array();
$result = doSearch($conn);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="styleProject.css">
</head>
<body>
<div id="centered_A">
<form action='' method='post'>
<table id="exercises">
<tr>
<th colspan="3"><h2>Exercises</h2></th>
</tr>
<tr>
<th><h3>Exercise_ID</h3></th>
<th><h3>Title</h3></th>
<th><h3>Difficulty
<form id="difficulty_form" method="get">
<select id="filter" name="filter" onchange="document.getElementById('difficulty_form').submit()">
<option value="All">Todas</option>
<?php
for ($i = 1; $i <= 5; $i++) {
$selected = '';
if (isset($_GET['filter']) && $_GET['filter'] == $i) {
$selected = ' selected';
}
echo "<option value=\"$i\"$selected>$i</option>\n";
}
?>
</select>
</form>
</h3></th>
</tr>
<?php
$order = 1;
$num_ex = 1;
while($row = $result->fetch_assoc())
{
array_push($list_ex, $row["exercise_id"]);
$sql_check_ex = "select * from answers a where a.student_id=".$_SESSION['user_id']." and a.exercise_id_fk=".$row["exercise_id"].";";
$result_check_ex = $conn->query($sql_check_ex);
?>
<tr>
<td><?php echo $num_ex; ?></td>
<!---Click Toggle Exercise-->
<td><a onclick="myFunction(<?php echo $row["exercise_id"] ?>)" role="button" class="btn" target="_blank" ><?php echo $row["title"]; ?></a>
</td>
<td><?php echo $row["difficulty"]; ?></td>
</tr>
<!--- Toggle --->
<tr id="toggle<?php echo $row["exercise_id"] ?>" style="display:none">
<td colspan="3">
<?php
$sql = "SELECT * FROM exercises WHERE exercise_id='".$row["exercise_id"]."'";
$result_ej = $conn->query($sql);
$row_ej = $result_ej->fetch_assoc();
?>
<p><?php $row["exercise_id"] ?> <?php echo $row["text"]?></p>
<br><!--- Radio Button --->
<?php
if ($row["type"] == 0) {
$ansarray = explode("\n", $row["image_path"]);
$randomans = [];
for($i=0; $i<count($ansarray); $i++) {
$a = array($i+1, $ansarray[$i]);
array_push($randomans, $a);
}
shuffle($randomans);
for($i=0; $i<count($randomans); $i++) {
echo "<div class=\"radio\" style=\"text-align:left; display:flex; vertical-align: baseline;\">";
echo "<input type=\"radio\" style=\"margin-top: 8px; cursor:pointer;\" name=\"choice".$row["exercise_id"]."\" value= \"".$randomans[$i][0]."\" />".$randomans[$i][1]."<br>";
echo "</div>";
}
echo "<input type=\"text\" name=\"choicetext".$row["exercise_id"]."\" value='multi' style=\"display:none;\">";
} else {
?>
<input type="radio" style="margin-top: 8px" name="choice<?php echo $row["exercise_id"] ?>" value= "0" checked="checked" style="display:none;" />
<?php
}
?>
<br>
<!--- Select level --->
<p2>Select difficulty level:</p2>
<select name="choose<?php echo $row["exercise_id"] ?>" id="choose<?php echo $row["exercise_id"] ?>">>
<option value="1" <?php if($row["difficulty"]=="1") { echo "selected"; } ?> >1</option>
<option value="2" <?php if($row["difficulty"]=="2") { echo "selected"; } ?> >2</option>
</select>
</td>
</tr><!---Finish Toggle --->
<?php
$order++;
$num_ex++;
}
?>
</table>
<br><!---Submit Button-->
<input class="buttonSubmit" type="submit" name="submit_proj_ex" value="Submit">
</form>
<script>
function myFunction(ejer_id) {
var x = document.getElementById("toggle" + ejer_id);
if (x.style.display === "none") {
x.style.display = "table-row";
} else {
x.style.display = "none";
}
}
</script>
</div>
</body>
<?php
if (isset($_POST['submit_proj_ex'])) {
include_once("store_answers.php");
header('location: Results.php');
}
?>
</html>
functions.php
<?php
function doSearch($conn, $fields = "*") {
$sql = "SELECT $fields FROM exercises, ex_tag, tags where exercise_id = exercise_id_fk and tag_id = tag_id_fk";
if (isset($_GET['filter']) && $_GET['filter'] !== 'All') {
$filter = (int) trim($_GET['filter']);
$sql .= " and exercises.difficulty = '$filter'";
}
if (!empty($_SESSION['tags_array'])) {
$sql .= " and (";
foreach ($_SESSION['tags_array'] as $tagId)
$sql .= 'tag_id = ' . $tagId . ' or ';
$sql .= "tag_id = -1)";
}
$sql .= ' group by exercise_id_fk';
return $conn->query($sql);
}
?>
Run phpinfo() in both WAMP and XAMP. I'm not certain what your issue is, but if it works in one and not the other, perhaps they are using different versions of php. If this is the case, you will need to use the same version of php in your XAMP as you were using in WAMP, or go through your code and try to figure out how to convert your code, such that it works correctly with the version on your XAMP. The former is probably the path of least resistance.
I have modified function.php in th wordpress and called php page from the wordpress menu using shortcode. I can able to connect and display php page.
In the php page able to display all the records intially without selecting value from dropdown list. But when I change the value in dropdown list instead of showing the filtered records depending on the dropdown list the page goes to wordpress index page. I suspect onaction command as a submit its not staying in the called php page instead it goes to the wordpress index page.
<?php
$selected = '';
function get_options($select) {
$categories = ['Select Category' => 0, 'Information Technology' => 1, 'Management' => 2];
$options = '';
while (list($k, $v) = each($categories)) {
if ($select == $v) {
$options .= '<option value="' . $v . '" selected>' . $k . '</option>';
} else {
$options .= '<option value="' . $v . '" >' . $k . '</option>';
}
}
//var_dump($options);
//echo var_dump($options)."<br>";
return $options;
}
require_once('dbconnect.php');
if (isset($_POST['categories'])) {
$selected = $_POST['categories'];
echo $selected;
}
if ($selected == 1) {
$selectedcat = 'Information Technology';
$selectsql = "SELECT * FROM courses where ccategory='$selectedcat'";
} else if ($selected == 2) {
$selectedcat = 'Management';
$selectsql = "SELECT * FROM courses where ccategory='$selectedcat'";
} else {
$selectsql = "SELECT * FROM courses";
}
//require_once('dbconnect.php');
//include('header-basic-light.php');
//$selectsql="SELECT * FROM courses";
$res = (mysqli_query($con, $selectsql));
if (!mysqli_query($con, $selectsql)) {
die(mysqli_error($con));
}
mysqli_close($con);
//header('Location:index.php');
?>
<HTML>
<head>
//<title>"View Information"</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="categories">Select the Category : </label>
<select name="categories" style="width:250px;" onchange="this.form.submit();">
<?php echo get_options($selected); ?>
</select>
</form>
<h2>View Information</h2>
<table class="table">
<tr>
<th>#</th>
<th>cname</th>
<th>start_date</th>
<th>duration</th>
<th>Remarks</th>
<th>Options</th>
</tr>
<?php
while ($r = mysqli_fetch_assoc($res)) {
?>
<tr>
<td><?php echo $r['cno']; ?></td>
<td><?php echo $r['cname']; ?></td>
<td><?php echo $r['start_date']; ?></td>
<td><?php echo $r['duration']; ?></td>
<td><?php echo $r['remarks']; ?></td>
<?php if ($r['ccategory'] == 'Information Technology') {
$catnum = 1;
}
if ($r['ccategory'] == 'Management') {
$catnum = 2;
} ?>
<td>Details  
</tr>
<?php } ?>
</table>
</body>
</html>
The usage of a short code is a requirement? Why not creating a page-slug.php, override it via a child theme and getting redirections and urls via standard permalink function?
I have been following a tutorial for a php shopping cart,
I have checked my code over and I am to the point where the add to cart button SHOULD be adding the products into the sidebar.
However, it appears to skip over my if statement and go straight to the else error message stating the product ID is invalid.
I have checked that the SKU in the database match thought that are displayed in $id so i'm a little lost as to why this error persists?
PHP for Products:
<?php
session_start();
if (isset($_GET['action']) && $_GET['action'] == "add") {
$id = $_GET['id'];
if (isset($_SESSION['cart'][$id])) {
$_SESSION['cart'][$id]['quantity']++;
} else {
$sql2 = "SELECT * FROM products WHERE SKU=$id";
$query2 = mysql_query($sql2);
if(mysql_num_rows($query2) != 0){
$row2 = mysql_fetch_array($query2);
$_SESSION['cart'][$row2['SKU']] = array("quantity" => 1, "price" => $row2['price']);
} else {
$message = "This product ID is invalid";
}
}
}
?>
<h2 class="message"><?php if(isset($message)){echo $message;} ?></h2>
<h1>Product Page</h1>
<table>
<tr>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Action</th>
</tr>
<?php
$sql = "SELECT * FROM products ORDER BY SKU ASC";
$query = mysql_query($sql)or die(mysql_error());
while($row = mysql_fetch_assoc($query)){
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo "£" . $row['price']; ?></td>
<td>Add to cart</td>
</tr>
<?php
}
?>
PHP for Index.php:
<?php
session_start();
require_once("connect.php");
if (isset($_GET['page'])) {
$pages = array("products","cart");
if (in_array($_GET['page'],$pages)) {
$page = $_GET['page'];
} else {
$page = "products";
}
} else {
$page = "products";
}
?>
<html>
<head>
<link rel="stylesheet" href="reset.css" />
<link rel="stylesheet" href="style.css" />
<title>Shopping Cart - WebThatWorks Ltd</title>
</head>
<body>
<div id="container">
<div id="main"><?php require($page. ".php"); ?></div>
<div id="sidebar">
<h1>Cart</h1>
<?php
if (isset($_SESSION['cart'])) {
$sql = "SELECT * FROM products WHERE SKU IN (";
foreach ($_SESSION['cart'] as $id => $value) {
$sql .=$id. ",";
}
$sql = substr($sql,0,-1) . ")ORDER BY SKU ASC";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)){
?>
<p><?php echo $row['name']; ?><?php echo $_SESSION['cart'][$row['SKU']]['quantity']; ?></p>
Go To Cart
<?php
}
} else {
echo "<p>Your Cart Is Empty. <br /> Please Add some products</a>";
}
?>
If you require me to post the structure of my database, I shall do so
The problem is your SKU is a string and so it needs quotes in the query:
$sql2 = "SELECT * FROM products WHERE SKU='" . mysql_real_escape_string($id) . "'";
I also added the escape call to prevent SQL Injection, but for best security and other benefits, you should switch to a modern API such as PDO or MySQLi and use prepared statements.
You will also need to make sure the quotes are added to the IN query as well.
foreach ($_SESSION['cart'] as $id => $value) {
$sql .="'" . mysql_real_escape_string($id) . "',";
}
I want to have the ability to show all the entries in a database table and by each one give the user the ability to delete specific ones.
I am currently using a for each loop that loops through the database showcasing each entry.
$result = mysql_query("SELECT * FROM KeepScores");
$fields_num = mysql_num_fields($result);
echo "<table><tr>";
// printing table headers
echo "<td>Recent Posts</td>";
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
How would to add a delete button that appears by each one and removes the entry from the database table?
You can do it with forms:
//main.php
<?php $result = mysql_query("SELECT * FROM KeepScores"); ?>
<table>
<tr>
<td>Recent Posts</td>
</tr>
<?php while($row = mysql_fetch_array($result)) : ?>
<tr>
<td><?php echo $row['field1']; ?></td>
<td><?php echo $row['field2']; ?></td>
<!-- and so on -->
<td>
<form action="delete.php" method="post">
<input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>" />
<input type="submit" value="Delete" />
</form>
</td>
</tr>
<?php endwhile; ?>
</table>
//delete.php:
<?php
if(isset($_POST['delete_id'] && !empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id);
header('Location: main.php');
}
Or you can do it with jQuery and AJAX:
//main.php
<?php $result = mysql_query("SELECT * FROM KeepScores"); ?>
<table>
<tr>
<td>Recent Posts</td>
</tr>
<?php while($row = mysql_fetch_array($result)) : ?>
<tr id="<?php echo $row['id']; ?>">
<td><?php echo $row['field1']; ?></td>
<td><?php echo $row['field2']; ?></td>
<!-- and so on -->
<td>
<button class="del_btn" rel="<?php echo $row['id']; ?>">Delete</button>
</td>
</tr>
<?php endwhile; ?>
</table>
<script>
$(document).ready(function(){
$('.del_btn').click(function(){
var del_id = $(this).attr('rel');
$.post('delete.php', {delete_id:del_id}, function(data) {
if(data == 'true') {
$('#'+del_id).remove();
} else {
alert('Could not delete!');
}
});
});
});
</script>
//delete.php
<?php
if(isset($_POST['delete_id'] && !empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
$result = mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id);
if($result !== false) {
echo 'true';
}
}
It's all untested and sure needs some adjustment for your specific project, but I think you get the idea and I hope it helps.
Next time, please post your schema if you ask stuff about database.
I thought I would improve on this a little bit by wrapping the delete post in a class and function. I was having the same problem. and this worked great for me. Thanks again # Quasdunk
<?php
// Class to hold the remove post function
class someClass{
//Function for removing the post
function removePost(){
if(isset($_POST['delete_id']) && (!empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
$result = mysql_query("DELETE FROM post WHERE post_id='".$delete_id."' AND post_member='" . $_SESSION['SESS_USER'] . "'");
if($result !== false) {
echo 'true';
}
}
}
}
if(isset($_SESSION['SESS_MEMBER_ID'])){
$member = $_SESSION['SESS_USER'];
$res = mysql_query("SELECT * FROM post WHERE post_member='$member' ORDER BY timestamp DESC") or die(mysql_error());
$i = new someClass;
while($row = mysql_fetch_array($res)){
echo '<div style="width:100%;margin:0 auto;border-top:thin solid #000;">';
echo '<div style="width:600px;margin:0 auto;padding:20px;">';
echo $row['post_text'] . '<br>';
$postID = $row['post_id'];
echo '<div style="border-top:thin solid #000;padding:10px;margin-top:5px;background-color:#CCC;">';
echo 'You posted this on: ' . $row['post_date'] . '#' . $row['post_time'];
echo '<div style="float:right;">
<form method="post" action="'. $i->removePost() .'">
<input type="hidden" name="delete_id" value="'.$row['post_id'].'" >
<input type="submit" value="Delete Post">
</form>
</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
}
?>
Produce a key to each table, using jquery,then link it to a php file which an accept the key and delete from the specific table (which also can be passed through jquery)
I'm trying to write some sort of forum homepage, based off work someone else did. Here's what I've got so far:
Forums
<?php
$crumbs = explode(",", $user['data']['depts']);
foreach ($crumbs as &$value) {
$data = $db->query("SELECT * FROM tbl_depts WHERE id = '" . $value . "'");
$crumb = $data->fetch_assoc();
$data = $db->query("SELECT * FROM tbl_forums WHERE deptid = '" . $value . "'");
$forumcount = $data->num_rows;
$forum = $data->fetch_assoc();
?>
<div class="h3top"><?php echo $crumb['name']; ?></div>
<div class="info2alt">
<?php
while (($row = $data->fetch_array()) !== FALSE) {
$forum[] = $row;
}
foreach ($forum as $row) {
if ($forumcount >= 1) {
if ($forum['lastpost'] == "") {
$forum['lastpost'] = "--";
}
?>
<div class="info4">
<table width="100%" border="0" cellspacing="1" cellpadding="4">
<tr>
<td width="55%" align="left" valign="middle" class="zebraodd"><?php echo $row['name']; ?></td>
<td width="10%">Threads: <?php echo $forum['threadcount']; ?><br />Posts: <?php echo $forum['postcount']; ?></td>
<td width="35%">Last Post: <?php echo $forum['lastpost']; ?></td>
</tr>
</table>
</div>
<?php
}
?>
<?php if ($forumcount >= 1) { ?>
<div class="info3bottom"></div>
<?php
}
}
?>
</div>
<?php
}
?>
This is the current data in the table:
Now, when I try to use this code, I get this:, such that the first one is shown so many times, however, the next one does not appear.
How do I fix this?
you want to use $value['threadcount'] etc. $forum is the array containing all the rows. i wonder that you get some output at all …
fetch_assoc() will only fetch a single row from the resultset. you'd have to use a loop to fill an array:
while(($row = $data->fetch_assoc()) !== FALSE) {
$forum[] = $row;
}
you can then iterate over your $forum array with a foreach loop:
foreach($forum as $row) {
echo htmlspecialchars($row['threadcount']);
// etc.
}
trying to fix this mess …
<?php
$crumbs = explode(",", $user['data']['depts']);
foreach ($crumbs as $crumb) { ?>
<div class="h3top"><?php echo htmlspecialchars($crumb['name']);?></div>
<?
}
$result = $db->query("SELECT * FROM tbl_forums WHERE deptid = " . (int)$id . "");
$forumcount = $result->num_rows;
while(($row = $data->fetch_assoc()) !== FALSE) {
if ($row['lastpost'] == "") { $row['lastpost'] = "--";}
?>
<div class="info2alt">
<div class="info4">
<table width="100%" border="0" cellspacing="1" cellpadding="4">
<tr>
<td width="55%" align="left" valign="middle" class="zebraodd"><?php echo htmlspecialchars($forum['name']); ?></td>
<td width="10%">Threads: <?php echo htmlspecialchars($forum['threadcount']); ?><br />Posts: <?php echo htmlspecialchars($forum['postcount']); ?></td>
<td width="35%">Last Post: <?php echo htmlspecialchars($forum['lastpost']); ?></td>
</tr>
</table>
</div>
<div class="info3bottom"></div>
<?php
}
?>