Iterate through a column in a MySQL table - php

I am doing questionnaire on php, I am new to php. When I try to show the answers, it only shows the first two questions. After the two questions, answers are not shown. I have attached the source here.
Please use the following link to view the page.
http://itsupportsrilanka.com/uaquiz/quiz.php
<body>
<form action="test.php" method="POST">
<?php
$result = select("SELECT * FROM questions ");
//$row = mysql_fetch_array($result);
?>
<?php
$i=1;
while($row = mysql_fetch_array($result))
{
?>
<table width="581" height="299" border="1">
<tr>
<td>Union Assurance Questionnaire</td>
</tr>
<tr>
<td><?php echo $i.'.' .$row['questions']; ?>
<?php $i++; ?>
</td>
</tr>
<tr>
<td>
<?php $qId=$row['question_id'];
$result1=select("SELECT * FROM answers WHERE questionId='$qId' ORDER BY RAND()");
while($row1=mysql_fetch_array($result1)){
?>
<input type="radio" name="answers" value="<?php echo $row1['answers'];?>" / ><?php echo $row1['answers']; ?><br/>
<?php
} ?>
</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
<?php
}
?>
</form>
</body>

Replace
$result = select("SELECT * FROM questions "); with $result = mysql_query("SELECT * FROM questions ");
and
$result1=select("SELECT * FROM answers WHERE questionId='$qId' ORDER BY RAND()"); with
$result1=mysql_query("SELECT * FROM answers WHERE questionId='$qId' ORDER BY RAND()");
and try again

Related

htaccess URL rewrite with PHP & MySQL [duplicate]

This question already has answers here:
Reference: mod_rewrite, URL rewriting and "pretty links" explained
(5 answers)
Closed 4 years ago.
My Question is: How can I show it http://localhost/jakir1/banner2/test.php/title-3 instead of http://localhost/jakir1/banner2/test.php?slug=title-3
When i click
title 1
title 2
title 3
Its Showed Output
Here is My Code:
<table border="1">
<tr>
<td>
Product Name
</td>
<td>
Description
</td>
</tr>
<?php $sql = "SELECT * FROM marchant_item ORDER BY id DESC";
$display = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($display)) { ?>
<tr>
<td><?php echo $row['item_name']; ?></td>
<td>
<?php echo $row['description']; ?>
</td>
</tr>
<?php }
?>
</table>
<?php
if (isset($_GET['slug'])) {
$data = $_GET['slug'];
?>
<table border="1">
<tr>
<td>
Product Name
</td>
<td>
category
</td>
</tr>
<?php $sql = "SELECT * FROM marchant_item WHERE slug ='$data'";
$display = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($display)) { ?>
<tr>
<td><?php echo $row['item_name']; ?></td>
<td>
<?php echo $row['category']; ?>
</td>
</tr>
<?php }
?>
</table>
<?php
}
?>
Output:
write this line in your .htaccess file--
RewriteRule ^test/([A-Za-z0-9-]+)?$ test.php?slug=$1 [NC]
update your code with
<td><?php echo $row['item_name']; ?></td>
and you get
http://localhost/jakir1/banner2/test/title-3

While loop in php stops content

The issue im having is when running a while loop inside of a while loop in php the html content after the second while loop is not being rendered
for a live demonstration http://naturalflame.altervista.org/home.php
I originally didnt use a continue after the second loop but then none of the other rows populate because it just stops after the second loop after adding the continue after the second loop the other rows populate but the description and action field are left out
<div id="main">
<h1>Product List</h1>
<table>
<tr>
<th>Size</th>
<th>Price</th>
<th>Flavor Selection</th>
<th>Flavor Description</th>
<th>Blend Selection</th>
<th>Action</th>
</tr>
<?
$sql="SELECT * FROM products ORDER BY price ASC";
$query=mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_object($query)){
$id = $row->id;
$size = $row->size;
$price = $row->price;
?>
<tr>
<td><? echo $size; ?></td>
<td>$<? echo $price; ?></td>
<td>
<select onchange="ChangeDescription(<? echo $id; ?>)" id="dSelection<? echo $id; ?>">
<option value="Select a flavor">Select a flavor</option>
<?
$sql2 = "SELECT * FROM flavors ORDER BY id ASC";
$query2 = mysql_query($sql2) or die(mysql_error());
while($row2 = mysql_fetch_object($query2)){
$name = $row2->name;
echo "
<option value='$name'>$name</option>
";
}
?>
</select>
</td>
<td id="description<? echo $id; ?>">Flavor Description</td>
<td>Add to cart</td>
</tr>
<?}?>
</table>
</div><!--end main-->
You seem to be using an object as an array causing an error on your page.
This code: $row['id'] is probably just supposed to be $id
If you hadn't of already put it in a placeholder it would be $row->id

I want to delete multiple images from source folder through PHP but the code for deleting the multiple images from the folder is not working

<?php include("header.php"); ?>
<?php
if (#$_POST['delete']=="Delete"){
$count=count($_POST['delbx']);
for($i=0;$i<$count;$i++){
$delete = "DELETE FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
$resulty = mysqli_query($conn, $delete) or die(mysql_error());
$select_delete = "SELECT `a_image` FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
$resultrowdy = $conn->query($select_delete);
$rowdy = $resultrowdy->fetch_assoc();
$path="admin/".$rowdy['a_image'];
echo $path;
unlink($path);
echo '<script>window.location="view_user.php"</script>';
}
} ?>
<div class="table-responsive">
<table class="table">
<caption>All Users</caption>
<?php
$sql = "SELECT a_id, a_name, a_phone, a_password, a_role, a_mail, a_image FROM admin";
$result = $conn->query($sql);
if ($result->num_rows > 0) {?>
<thead>
<tr>
<th><form action="view_user.php" method="post"><input name="delete" type="submit" id="delete" value="Delete"></th><th>S. No.</th> <th>Name</th> <th>Phone No.</th> <th>Mail Id</th> <th>Role</th> <th>Password</th> <th>Image</th>
</tr>
</thead>
<?php
while($row = $result->fetch_assoc()) { ?>
<tbody>
<tr>
<th scope="row">
<?php echo $row["a_id"]; ?>
</th>
<td align="center" bgcolor="#FFFFFF">
<input name="delbx[]" type="checkbox" id="delbx[]" value="<?php echo $row["a_id"]; ?>" />
</td>
<td>
<?php echo $row["a_name"]; ?>
</td>
<td>
<?php echo $row["a_phone"]; ?>
</td>
<td>
<?php echo $row["a_mail"]; ?>
</td>
<td>
<?php echo $row["a_role"]; ?>
</td>
<td>
<?php echo $row["a_password"]; ?>
</td>
<td>
<img src="admin/<?php echo $row["a_image"]; ?>" width="60" height="40">
</td>
<th>
Edit
</th>
</tr>
</tbody>
<?php
}
} else {
echo "0 results";
}?>
</table>
</form>
</div>
<?php include("footer.php"); ?>
The code I mention is not deleting the multiple images from the source folder but deleting the multiple data from database whereas I am trying to delete images from the source folder along with data please help thanks in advance
One of the problem is you are deleting the row and trying to select image column from the deleted row.. dont use user supplied variables directly in your query
your code should be
for($i=0;$i<$count;$i++){
$select_delete = "SELECT `a_image` FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
$resultrowdy = $conn->query($select_delete);
$rowdy = $resultrowdy->fetch_assoc();
$delete = "DELETE FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
if(mysqli_query($conn, $delete)){
$path="admin/".$rowdy['a_image'];
unlink($path);
echo '<script>window.location="view_user.php"</script>';
}
}

multiple mysql queries not executing in single page?

I have created a script that inserts some details into a form and and below it displays the values inserted in a table. There is a single mysql connection which should work. The insert and select from the advert table is not working! I dont seem to understand why. my table are created properly, there are no errors because i have checked it it anther code. Only this code is not working.
Can someone tell me why are the sql statements not working?
<?php
include('header.php');
session_start();
$user = $_SESSION['username'];
mysql_select_db("ladyjoy_fs", mysql_connect("localhost", "root", ""))or die(mysql_error());
$login=mysql_query("select * from user where user_name='$user'")or die(mysql_error());
$row=mysql_fetch_row($login);
$_SESSION['userid'] = $row[0];
?>
<body>
<?php
$users = mysql_query("select * FROM gcm_users")or die(mysql_error());
if ($users != false)
$no_of_users = mysql_num_rows($users);
else
$no_of_users = 0;
;
?>
<center>
</br>
</br>
<div id="container">
<div id="header">
<div class="alert alert-success"><label>Welcome Advertiser</label></div>
</div>
<table>
<thead>
<td>
<tr>Home | </tr>
<tr>My Advertisements | </tr>
<tr>My Account | </tr>
<tr>Logout | </tr>
</td>
</thead>
</table>
<br/>
<table class="table table-bordered">
<div class="alert alert-success">Creating New Notifications</div>
</table>
<div style="float:center;">
<h4>New Advertisement</h4>
<h5>No of Devices Registered: <?php echo $no_of_users; ?></h5>
<form id="form1" method="POST" onsubmit="return sendPushNotification()">
<label class="control-label" for="inputEmail">Title</label>
<input type="text" name="ad_title" id="ad_title" class="txt_title" placeholder="Notification Title">
<label class="control-label" for="inputEmail">Description</label>
<input type="text" name="ad_desc" id="ad_desc" class="txt_desc">
<label class="control-label" for="inputEmail">Picture</label>
<input type="text" name="ad_pic" id="ad_pic" placeholder="URL">
<label class="control-label" for="inputEmail">Location</label>
<input type="text" name="ad_location" id="ad_location" placeholder="Venue"><br/>
<input type="hidden" name="regId" value="<?php echo $_SESSION['userid'] ?>"/>
<button type="submit" id="add_supply" name="add_supply" class="btn btn-info">Send Notification</button>
</div>
</form>
<?php
if (isset($_POST['add_supply'])){
$title=$_POST['ad_title'];
$desc=$_POST['ad_desc'];
$url=$_POST['ad_pic'];
$location=$_POST['ad_location'];
$userid=$_POST['regId'];
$sql2 = "insert into advert (title,description,url,location,user_id) values('$title','$desc','$url','$location','$userid')";
mysql_query($sql2)or die(mysql_error());
}
?>
<br/>
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" >
<thead>
<tr>
<th>Advertisement ID</th>
<th>Title</th>
<th>Description</th>
<th>Picture</th>
<th>Location</th>
</tr>
</thead>
<tbody>
<?php
$query=mysql_query("select * from advert where user_id = ".$_SESSION['userid']."")or die("Error at query : "+mysql_error());
while(($row=mysql_fetch_array($query))){
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['url']; ?></td>
<td><?php echo $row['location']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</center>
</body>
</html>
$query=mysql_query("select * from advert where user_id = '".$_SESSION['userid']."' ")or die("Error at query : "+mysql_error());
I think it is error in syntax. Please check it.
or you can try out this code ->
$user = $_SESSION["username"];
$res = "select * from user where user_name='$user'";
$result = mysql_query($res) or die("query fail to execute".mysql_error());
while($row= mysql_fetch_array($result))
{
$ac= $row['id'];
$a= ("select * FROM gcm_users WHERE user_id='$ac'");
$re = mysql_query($a) or die("wrong query".mysql_error());
while($row= mysql_fetch_array($re))
{
}
}
note->and yes as mention above your code is vulnerable to sql injection.

Removing access from user levels

sorry for my code but this is only a practice code, i m trying to remove user access links, i displayed all the links that corresponds to the selected user access here's the code and database
Select user levels
<p class="left"><b>User Level Information</b></p><br>
<table class="left">
<tr>
<td id="TD3"><B>User Level ID</B></td>
<td id="TD3"><B>User Level Name</B></td>
</tr>
<?php
$employee = mysql_query("SELECT * FROM cms_userlevels ORDER BY `cms_userlevels`.`user_level` ASC ");
while($row1 = mysql_fetch_array($employee)){
?>
<tr>
<td id="TD3"><?php echo $row1['user_level']; ?></td>
<td id="TD3"><?php echo "<a href='manageuserlevels.php?id=".$row1['user_level']."'>"; ?> <?php echo $row1['user_type']; ?> </a></td>
</tr>
<?php } ?>
</table>
Selected User level
if(isset($_POST['Update']))
{
if(isset($_POST['delete']))
{
$delete1= mysql_query("DELETE FROM cms_userlevels where PROGRAM_ID = '$userId'");
header('location:manageuserlevel.php');
}
else
{
$update = mysql_query("UPDATE cms_program set
PROGRAM_TITLE='$_POST[progti]',
PROGRAM_DESCRIPTION='$_POST[progde]'
where
PROGRAM_ID='$userId'");
}
}
?>
<?php
$userId = $_GET['id'];
$sql = mysql_query("select * from cms_userlevels where user_level='$userId'");
$row2 = mysql_fetch_array($sql);
echo "
<center>
<table class='left'>
<form method='post' action='managemanageuserlevels.php?id=". $userId. "'>
<tr>
<td id='TD4'>User Level ID </td>
<td id='TD4'><input type='text' name='usertype' value='".$row2['user_type']."'></td>
</tr>
<tr>
<td colspan='2'><center><input type='submit' name='submit' value='Update'></td>
</tr>
</form>
</table>";
?>
<table class="left">
<tr>
<td>Remove Access:</td></tr>
<?php
$haslevel = mysql_query("select * from cms_userlevels, cms_level_page_matches, cms_pages
WHERE cms_userlevels.user_level = '$userId'
AND cms_level_page_matches.page_id = cms_pages.id");
while($getlevel = mysql_fetch_array($haslevel)){
$showhaslevel1 = $getlevel['id'];
$showhaslevel2 = $getlevel['page_title'];
?>
<tr><td>
<input type="checkbox" name="checkbox[]" value="<? echo $showhaslevel1; ?>"><?php echo $showhaslevel2; ?></td></tr>
<?php } ?>
the tables used
cms_userlevels
user_level user_type
1 Admin
2 Student
user_pages
id page name
1 page1.php
2 page2.php
user_level_page_matches
id user_level page_id
1 1 1
2 2 2

Categories