Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is a university project and I have a page will a php calculator where users enter '$valuea' which is likely to be between 0-550 then they will hit submit which subtracts '$valueb' which is 250, giving them '$answer'.
What I need help with is I was to have a particular image echo out once the user hit submit, which depends on the figure of $answer.
Here's what I have so far but it is not working;
<?php
if (isset($_POST['valuea'])) $valuea = $_POST['valuea'];
if (isset($_POST['valueb'])) $valueb = '250';
$answer = $valuea - $valueb;
echo <<<_END
<form method='post' action='calculator.php'>
<table border='0' width='500px' cellpadding='3' cellspacing='1' class="table">
<tr class="calcheading"><td colspan="2"><strong>Work out how much you could be saving</strong></td></tr>
<tr class="calcrow"><td>How much do you spend a year?</td><td align="center"><input type='text' name='valuea' value="$valuea"/></td></tr>
<tr class="calcrow"><td>Minus the average price of an n-power student tarrif* Leave Blank:</td><td align="center"><input type='text' name='valueb' value="$valueb"/></td></tr>
<tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/></td></tr>
_END;
?>
<tr class="calcrow">
<td><i>You could be saving:</td>
<td align="center"><input type="text" value="<?php echo round($answer)?>"></td></i>
</tr>
</table>
</form>
<?php
if($db_server){
$image1 = mysqli_query("SELECT URL FROM images WHERE imagename='image1');"
$image2 = mysqli_query("SELECT URL FROM images WHERE imagename='image2');"
$image3 = mysqli_query("SELECT URL FROM images WHERE imagename='image3');"
$image4 = mysqli_query("SELECT URL FROM images WHERE imagename='image4');"
$image5 = mysqli_query("SELECT URL FROM images WHERE imagename='image5');"
if( $answer > 0 and $answer < 150 )?>
<img src="<?php echo $image1;?>"/>
else if( $answer < 250)
<img src="<?php echo $image2;?>"/>
else if( $answer < 350)
<img src="<?php echo $image3;?>"/>
else if( $answer < 450)
<img src="<?php echo $image4;?>"/>
else if( $answer < 550)
<img src="<?php echo $image5;?>"/>
<?php
else
echo 'error';
}
?>
Your code is a real mess. I've cleaned it a bit.
<?php
$valuea = (isset($_POST['valuea']) && is_numeric($_POST['valuea'])) ? $_POST['valuea'] : 0;
$valueb = 250;
$answer = $valuea - $valueb;
?>
<form method='post' action='calculator.php'>
<table border='0' width='500px' cellpadding='3' cellspacing='1' class="table">
<tr class="calcheading">
<td colspan="2"><strong>Work out how much you could be saving</strong></td>
</tr>
<tr class="calcrow">
<td>How much do you spend a year?</td>
<td align="center"><input type='text' name='valuea' value="$valuea"/></td>
</tr>
<tr class="calcrow">
<td>Minus the average price of an n-power student tarrif* Leave Blank:</td>
<td align="center"><input type='text' name='valueb' value="$valueb"/></td>
</tr>
<tr class="submit">
<td colspan="2"><input type='submit' value='Calculate'/></td>
</tr>
<tr class="calcrow">
<td><i>You could be saving:</td>
<td align="center"><input type="text" value="<?php echo round($answer)?>"></td></i>
</tr>
</table>
</form>
<?php
if($db_server){
switch( $answer ){
case $answer > 0 and $answer < 150 : $image = mysqli_query("SELECT URL FROM images WHERE imagename = 'image1'");
break;
case $answer < 250 : $image = mysqli_query("SELECT URL FROM images WHERE imagename = 'image2'");
break;
case $answer < 350 : $image = mysqli_query("SELECT URL FROM images WHERE imagename = 'image3'");
break;
case $answer < 450 : $image = mysqli_query("SELECT URL FROM images WHERE imagename = 'image4'");
break;
case $answer < 550 : $image = mysqli_query("SELECT URL FROM images WHERE imagename = 'image5'");
break;
}
echo "<img src='$image' alt='' />";
}
?>
Related
I want to display image from database but currently it displays only name of image. need help. here is my code
submit.php
<?php
$radio = $_POST['RadioGroup1'];
$img_banner = $_FILES['bannerImage']['name'];
$target_file = "uploads/";
if(!empty($img_banner))
{
$errors = array();
$file_name = $_FILES['bannerImage']['name'];
$file_size = $_FILES['bannerImage']['size'];
$file_tmp = $_FILES['bannerImage']['tmp_name'];
$file_type = $_FILES['bannerImage']['type'];
$file_ext = strtolower(end(explode('.',$_FILES['bannerImage']['name'])));
$k = move_uploaded_file($file_tmp,"uploads/".$file_name);
}
$date = $_POST['date'];
$qry_banner = "INSERT INTO `banner_tbl`(`banner_img`, `date`) VALUES ('$img_banner','$date')";
$res_banner = mysql_query($qry_banner);
$banner_tbl_id = mysql_insert_id();
$sec_id = $_POST['sectionID'];
$sec_active_image = $_POST['activeimage'];
$sec_title = $_POST['section_title'];
$total_sec = count($sec_id);
for($i=0;$i<$total_sec;$i++)
{
$qry_section1 = "INSERT INTO `section_lt_tbl`(`banner_id`, `sectionID`, `activeimage`, `sectiontitle`) VALUES
('$banner_tbl_id','$sec_id[$i]','$sec_active_image[$i]','$sec_title[$i]')";
$res_section1 = mysql_query($qry_section1);
$sec_tbl_id1 = mysql_insert_id();
$section_generated_id[] = $sec_tbl_id1;
}
$array_section_ids = array();
for($i=1;$i<=$radio;$i++)
{
$j = $i-1 ;
$array_section_ids[$i] = $section_generated_id[$j];
}
for($h=1;$h<=count($array_section_ids);$h++)
{
$news_ids_sec[$h] = $_POST['sec_'.$h.'_new'];
$news_title_sec[$h] = $_POST['sec_'.$h.'_title_news'];
$sec_1_desc[$h] = $_POST['sec_'.$h.'_desc'];
$sec_1_newslink[$h] = $_POST['sec_'.$h.'_newslink'];
$sec_1_news_url[$h] = $_POST['sec_'.$h.'_news_url'];
$sec_1_news_img[$h] = $_FILES['sec_'.$h.'_news_img']['name'];
$temp_file[$h] = $_FILES['sec_'.$h.'_news_img']['tmp_name'];
$section = $array_section_ids[$h];
for($c=0;$c<count($news_ids_sec[$h]);$c++)
{
$id = $news_ids_sec[$h][$c];
// $sec_id = $array_section_ids[$h][$c];
$title= $news_title_sec[$h][$c];
$desc = $sec_1_desc[$h][$c];
$link = $sec_1_newslink[$h][$c];
$url = $sec_1_news_url[$h][$c];
$img = $sec_1_news_img[$h][$c][0];
$tmp_name = $temp_file[$h][$c][0];
$qry_news_insert = "INSERT INTO `news_lt_tbl`(`newsid`, `news_sec_id`, `newstitle`, `Description`, `titleoflink`, `urlofnews`, `news_img`) VALUES ('$id','$section','$title','$desc','$link','$url','$img')";
$res_news = mysql_query($qry_news_insert);
$file_name = $img;
$file_tmp = $tmp_name;
$file_ext = strtolower(end(explode('.',$file_name)));
$k = move_uploaded_file($file_tmp,"uploads/".$file_name);
}
}
?>
<?php
$qry_test = "SELECT * FROM `section_lt_tbl` WHERE sec_id = 162";
$res_tses = mysql_query($qry_test);
$arr = mysql_fetch_assoc($res_tses);
?>
<?php
$query = "SELECT `newsid`, `newstitle`, `Description`, `titleoflink`, `urlofnews`, `news_img` FROM `news_lt_tbl` ";
$result_news = mysql_query($query);
$file_name = $img;
$file_tmp = $tmp_name;
$file_ext = strtolower(end(explode('.',$file_name)));
$k = move_uploaded_file($file_tmp,"uploads/".$file_name);
?>
And
<table align="center" width="600">
<tr>
<tr>
<td bgcolor="#ffffff" valign="bottom" align="left"><img border="0" src="http://www.nrsadvisors.com/email-images/mainlogo.jpg" alt="NRS Advisors"/></td>
</tr>
<tr>
<td style="border-top-width:2px; border-top-style:solid; border-top-color: #3E7DBD;" bgcolor="#dbdbdb"><img border="0" src="http://www.nrsadvisors.com/email-images/banner-hero-image.png" alt="bannerimg" width="550" /><a name="featuredstartup" style="height:0;margin:0;padding:0"></a></td>
</tr>
<td bgcolor="#dbdbdb" align="center"><table border="0" cellpadding="0" cellspacing="0" width="550" align="center">
<tr>
<td width="25%"><a href="#featuredstartup"><img src="uploads/<?php echo $arr['activeimage'] ?>_blue.jpg" width="125px" height="134px" />
</a></td>
<td width="25%"><img src="uploads/<?php echo $arr['activeimage'] ?>_grey.jpg" width="125px" height="134px" /></td>
<td width="25%"><img src="uploads/<?php echo $arr['activeimage'] ?>_grey.jpg" width="125px" height="134px" /></td>
<td width="25%"><img src="uploads/<?php echo $arr['activeimage'] ?>_grey.jpg" width="125px" height="134px" /></td>
</tr>
</table>
</td>
</tr>
<?php
while ($row = mysql_fetch_array($result_news)) {
?>
<table align="center" width="600">
<tr>
<span style="font-size:14px;line-height:17px;font-weight:normal;color:#333333">
<td align="left">
<span style="font-weight:bold">
<?php echo $row['newsid'] ?>
</span>
</td>
</span>
</tr>
<tr>
<td>
<span align='left' style='color:#3E7DBD;font-weight:bold'>
<span style='font-size:14px;line-height:17px;font-weight:normal;color:#333333'><span style='font-weight:bold'>
<?php echo $row['newstitle'] ?>
-</span><br><br>
</td>
</tr>
<tr>
<td><?php echo $row['Description'] ?></td><br><br>
</tr>
<tr>
<td>
<a href="<?php echo $row['urlofnews'] ?>" style='font-size:14px;line-height:17px;color:#3FC47B; text-decoration:none;'><?php echo $row['titleoflink'] ?></a></td><br><br>
</tr>
<tr>
<td><?php echo $row['news_img'] ?></td>
</tr>
</table>
<?php
}
?>
You need to use img tag to display Image. like this:
<img src="your_upload_path/<?php echo $row['news_img'] ?>" />
Specify the upload image path in img src like this
<img src="Upload Path/<?php echo $row['news_img'] ?" />
I have built a web page that updates the status to either active, inactive or "NA".
"N/A" = 0
active = 1
inactive = -1
This all works fine
These are therefore inserted into database: 1, 0 and -1
Now i want to make a update page that only shows inactive values, in other words, only shows -1
Now I want to update this like I have inserted it on the insert page
if($_REQUEST['update']=="Yes")
{
if($_GET['status']==-1){
$update = mysql_query("update causelist set msg=0 where id='".$_GET['uid']."'");
}elseif($_GET['status']==1){
$update = mysql_query("update causelist set msg=-1, where id='".$_GET['uid']."'");
}elseif($_GET['status']==0){
$update = mysql_query("update causelist set msg=1 where id='".$_GET['uid']."'");
}
}
?>
<table width="100%" style="table-layout:fixed;">
<tbody>
<?php
$query = mysql_query("select * from causelist where CaseType='A'");
while($res= mysql_fetch_array($query))
{ ?>
<tr >
<td class="blueh" width="5px" style="height:40px;">
<?php echo $res['SNo']; ?>
</td>
<td class="purpleh" width="25px" style="height:40px;">
<font size="3" style="color:black;line-height:100%;font-family:'Calibri';">
<?php echo $res['CaseNo']; ?>
</font>
</td>
<td class="purpleh" width="25px" style=" width=20px;height:40px;">
<font size="3" style="color:black;line-height:100%;font-family:'Calibri';">
<?php echo $res['CouncilName']; ?>
</font>
</td>
<td class= "purpleh" width="25px" style="width=20px;height:40px;">
<font size="3" style="color:black;line-height:100%;font-family:'Calibri';">
<?php echo $res['resadv']; ?>
</font>
</td>
<td class="redh" width="3px" style=";line- height:50%;">
<?php if($res['msg']==1)
{
//echo "Active";
echo "<img src='images/r.png'/>";
}
elseif($res['msg']==-1)
{
//echo "Inactive";
echo "<img src='images/take.png'/>";
}
elseif($res['msg']==0)
{
//echo "NA";
echo "<img src='images/none.png'/>";
} ?></td>
<td class="redh" width="3px" style="center;height:5px;line-height:50%;">
<a href="case.php?uid=<?php echo $res['id']; ?>&update=Yes&status=
<?php echo $res['msg']; ? >">
<img src="images/p2.png"/></a></td>
</tr>
<?php }}?>
</tbody>
</table>
in this edit file i have to choose the same picture file or another picture file to be able update the rows. but i want to be able to change whichever i want without touching the other rows. any ideas over here?
<?php
include('../config.php');
include ('../ac.thumbs.php');
if ($_GET['sil']) {
$sil = htmlspecialchars(stripslashes($_GET['sil']));
mysql_query("DELETE FROM CloseOut WHERE itemNO = $sil limit 1");
echo "<h1>$sil item nolu kayit silinmistir</h1><br>";
}
$edit = htmlspecialchars(stripslashes($_GET['edit']));
$data = mysql_query("SELECT * FROM CloseOut WHERE itemNO=$edit") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{ echo "
<center>
[ Delete ]<br>
<a href=../../large.php?f=upload/".$info['resim'], $thumblarge .">
<img src=../../small.php?f=upload/".$info['resim'], $thumbsmall .">
</a>";
$editresim = $info[resim];
$editisim = $info[isim];
$editalan1 = $info[alan1];
$editalan2 = $info[alan2];
$editalan3 = $info[alan3];
$editalan4 = $info[alan4];
}
//Resimlerin yuklenecegin yer
$target = "../../upload/";
//////////////////////////////////////////////////////////////////////
///////////////////////////////Resim1/////////////////////////////////
//////////////////////////////////////////////////////////////////////
$target1 = $target . basename( $_FILES['resim1']['name']) ;
//Formdan gelen bilgileri almasi icin
$resim=($_FILES['resim1']['name']);
$isim=$_POST['isim1'];
$alan1=$_POST['alan11'];
$alan2=$_POST['alan21'];
$alan3=$_POST['alan31'];
$alan4=$_POST['alan41'];
/////////////Formdaki bos alanlara bos bilgi kayit etmemesi icin////////////////////////
$updates = array();
if (!empty($resim))
$updates[] = 'resim="'.mysql_real_escape_string($resim).'"';
if (!empty($isim))
$updates[] = 'isim="'.mysql_real_escape_string($isim).'"';
if (!empty($alan1))
$updates[] = 'alan1="'.mysql_real_escape_string($alan1).'"';
if (!empty($alan2))
$updates[] = 'alan2="'.mysql_real_escape_string($alan2).'"';
if (!empty($alan3))
$updates[] = 'alan3="'.mysql_real_escape_string($alan3).'"';
if (!empty($alan4))
$updates[] = 'alan4="'.mysql_real_escape_string($alan4).'"';
$updates = implode(', ', $updates);
/////////////////////////////////////////////////////////////////////////////////////////
//update etmesi icin
if(move_uploaded_file($_FILES['resim1']['tmp_name'], $target1))
{
mysql_query("UPDATE CloseOut SET $updates WHERE itemNO='$edit' ");
echo "Tebrikler, ". basename( $_FILES['resim1']['name']). " isimli dosya yuklendi ve database'e basariyla islendi!<br>";
}
?>
<br>
<center>
<table width="900" border="5" style="background-color:white; "bordercolor="#000000">
<tr>
<td width="120" height="50" bgcolor= "545454"><b>RESIM</b></td>
<td width="120" height="50" bgcolor= "545454"><b>NAME</b></td>
<td width="120" height="50" bgcolor= "545454"><b>CATAGORY</b></td>
<td width="120" height="50" bgcolor= "545454"><b>QUANTITY</b></td>
<td width="120" height="50" bgcolor= "545454"><b>LOCATION</b></td>
<td width="120" height="50" bgcolor= "545454"><b>PRICE</b></td>
</tr>
<tr>
<form enctype="multipart/form-data" method="post" action="">
<td><input type="file" name="resim1" value="<?php echo $editresim;?>"> </td>
<td><input type="text" name="isim1" value="<?php echo $editisim;?>"> </td>
<td><input type="text" name="alan11" value="<?php echo $editalan1;?>"> </td>
<td><input type="text" name="alan21" value="<?php echo $editalan2;?>"> </td>
<td><input type="text" name="alan31" value="<?php echo $editalan3;?>"></td>
<td><input type="text" name="alan41" value="<?php echo $editalan4;?>"></td>
</tr>
<td colspan="5"><center><input type="submit"></center></td>
</form>
</table>
</center>
I'm trying to use php with mysql. basically i've an index page where user fills a form and another page where all rows are displayed. i've checkboxes for each row for deleting the selected row/rows. i'm trying to create a new page (namely details) where it shows only the selected row.
I'm trying to use $_GET but i could not do it. maybe the syntax is wrong. any help is welcome.
here are the relative code parts:
display.php:
<?
require_once('auth.php');?>
<html>
<head>
<title>Goruntule</title>
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
</head>
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="checkboxForm">
<?
require "config.php"; // All database details will be included here
$page_name="display.php";
$start=$_GET['start']; // To take care global variable if OFF
if(!($start > 0)) { // This variable is set to zero for the first page
$start = 0;
}
$eu = ($start -0);
$limit = 10; // No of records to be shown per page.
$this1 = $eu + $limit;
$back = $eu - $limit;
$next = $eu + $limit;
// WE have to find out the number of records in our table. We will use this to break the pages
$query2=" SELECT * FROM table1 ";
$result2=mysql_query($query2);
echo mysql_error();
$nume=mysql_num_rows($result2);
/////// The variable nume above will store the total number of records in the table////
/////////// Now let us print the table headers ////////////////
$bgcolor="#f1f1f1";
echo "<TABLE width=80% align=center cellpadding=5 cellspacing=0> <tr>";
echo "<td bgcolor='dfdfdf' > <font face='arial,verdana,helvetica' color='#000000' size='2'>#</font></td>";
echo "<td bgcolor='dfdfdf' > <font face='arial,verdana,helvetica' color='#000000' size='2'>ID</font></td>";
echo "<td bgcolor='dfdfdf' > <font face='arial,verdana,helvetica' color='#000000' size='2'>Time</font></td>";
echo "</tr>";
////////////// Now let us start executing the query with variables $eu and $limit set at the top of the page///////////
$query=" SELECT * FROM table1 ORDER BY id DESC limit $eu, $limit ";
$result=mysql_query($query);
echo mysql_error();
//////////////// Now we will display the returned records in side the rows of the table/////////
while($rows = mysql_fetch_array($result))
{
if($bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}
else{$bgcolor='#f1f1f1';}
echo "<tr>";
echo "<td><input name='checkbox[]' type='checkbox' value='" . $rows[id] . "'></td>";
echo "<td align=left bgcolor=$bgcolor id='title'> <font face='Verdana' size='1'>$rows[id]</font></td>";
echo "<td align=left bgcolor=$bgcolor id='title'> <font face='Verdana' size='1'>$rows[DateTime]</font></td>";
echo "<td>Details</td>";
//here is the problematic line i guess
echo "</tr>";
}
echo "</table>";
////////////////////////////// End of displaying the table with records ////////////////////////
///// Variables set for advance paging///////////
$p_limit=100; // This should be more than $limit and set to a value for whick links to be breaked
$p_f=$_GET['p_f']; // To take care global variable if OFF
if(!($p_f > 0)) { // This variable is set to zero for the first page
$p_f = 0;
}
$p_fwd=$p_f+$p_limit;
$p_back=$p_f-$p_limit;
//////////// End of variables for advance paging ///////////////
/////////////// Start the buttom links with Prev and next link with page numbers /////////////////
echo "<table align = 'center' width='50%'><tr><td align='left' width='20%'>";
if($p_f<>0){print "<a href='$page_name?start=$p_back&p_f=$p_back'><font face='Verdana' size='2'>PREV $p_limit</font></a>"; }
echo "</td><td align='left' width='10%'>";
//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
if($back >=0 and ($back >=$p_f)) {
print "<a href='$page_name?start=$back&p_f=$p_f'><font face='Verdana' size='2'>PREV</font></a>";
}
//////////////// Let us display the page links at center. We will not display the current page as a link ///////////
echo "</td><td align=center width='30%'>";
for($i=$p_f;$i < $nume and $i<($p_f+$p_limit);$i=$i+$limit){
if($i <> $eu){
$i2=$i+$p_f;
echo " <a href='$page_name?start=$i&p_f=$p_f'><font face='Verdana' size='2'>$i</font></a> ";
}
else { echo "<font face='Verdana' size='4' color=red>$i</font>";} /// Current page is not displayed as link and given font color red
}
echo "</td><td align='right' width='10%'>";
///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
if($this1 < $nume and $this1 <($p_f+$p_limit)) {
print "<a href='$page_name?start=$next&p_f=$p_f'><font face='Verdana' size='2'>NEXT</font></a>";}
echo "</td><td align='right' width='20%'>";
if($p_fwd < $nume){
print "<a href='$page_name?start=$p_fwd&p_f=$p_fwd'><font face='Verdana' size='2'>NEXT $p_limit</font></a>";
}
echo "</td></tr></table>";
?>
<tr>
<td colspan="14" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete">
<form>
<INPUT TYPE="BUTTON" VALUE="Previous" ONCLICK="window.location.href='http://......../util'">
</FORM></td>
</tr>
<?php
$checkbox=$_POST['checkbox'];
if($_REQUEST['delete']=='Delete'){
foreach($checkbox as $id => $value)
{$sql="DELETE FROM table1 WHERE id='$value'";
$result = mysql_query($sql);
}
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=display.php\">";
}
}
?>
details
<?
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
detail.php
<?php
require_once('auth.php');
$host="localhost";
$username="";
$password="";
$db_name="";
$tbl_name="table1";
mysql_connect("$host", "$username", "$password")or die("Cannot connect ". mysql_error());
mysql_select_db("$db_name")or die("Cannot select DB ". mysql_error());
$num=$_GET['var1'];
$query = "SELECT * FROM table1 where id='$num'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_row($result) or die(mysql_error());
?>
<table border="0" align="center" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="13" align="center" bgcolor="#FFFFFF"><strong>Bölge</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Time</strong></td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name="checkbox[]" type="checkbox" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF" align="center"><? echo $row['13']; ?></td>
<td bgcolor="#FFFFFF" align="center"><? echo $row['0']; ?></td>
</tr>
<tr>
<td colspan="14" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete">
<form>
<input type=button value="Close" onClick="javascript:window.close();">
</form>
</tr>
<?php
$checkbox=$_POST['checkbox'];
if($_REQUEST['delete']=='Delete'){
foreach($checkbox as $key=>$value)
{$sql="DELETE FROM $tbl_name WHERE id='$value'";
$result = mysql_query($sql);
}
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=display.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
<html><head><link href="loginmodule.css" rel="stylesheet" type="text/css" /></head></html>
As I've said, the only problem i'm guessing is with the syntax, or something small as i can echo the row when i gave the var1 a specific id.
I'm sorry if i'm reposting but i couldn't find an answer. Thanks!
Edit: I'm thinking of deleting checkbox parts and adding gif links in the while loop where users can delete, edit or detailed view of the corresponding row. seems easier i guess.
Make sure that the details link is within the while block.
<?php
while($rows = mysql_fetch_array($result)) {
echo 'Details';
}
?>
Looks like you should not be using double quotes.
<? echo "$rows[id]" ?>
Should be
<?php echo $rows[id]; ?>
I also suggest you use 'id' as the name of the get rather than 'var1'. 'var1' does not mean anything whereas 'id' makes more sense.
Details
It's because you are using $rows outside your while-loop. You have to put the
Details
inside the while loop.
//EDIT:
Alright, I've stripped your code to the very neccessary according to your problem. So don't just copy/paste the code, it probably won't work. But read it carefully, and I hope you get the idea and see what may be wrong with your code ;)
display.php
<?php
require_once('auth.php');
require "config.php";
$page_name="display.php";
$start = (isset($_GET['start']) && $_GET['start'] < 1) ? 0 : $_GET['start'];
$eu = ($start-0);
$limit = 10;
$query="SELECT * FROM table1 ORDER BY id DESC limit $eu, $limit";
$result=mysql_query($query);
echo mysql_error();
$i = 0; //counter for the bg-color
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="checkboxForm">
<table width="80%" align="center" cellpadding="5" cellspacing="0">
<?php while($rows = mysql_fetch_array($result)) :
$bgcolor = $i%2 == 0 ? '#ffffff' : '#f1f1f1';
?>
<tr>
<td>
<input name="checkbox[]" type="checkbox" value="<?php echo $rows['id']; ?>">
</td>
<td style="align: left; font-family: Verdana; font-size: 10px; background-color: <?php echo $bgcolor; ?>;" id="title">
<?php echo $rows['id']; ?>
</td>
<td style="align: left; font-family: Verdana; font-size: 10px; background-color: <?php echo $bgcolor; ?>;" id="date">
<?php echo $rows['DateTime']; ?>
</td>";
<td>
Details
</td>
</tr>
<?php endwhile; ?>
</table>
</form>
detail.php
<?php
require_once('auth.php');
$num= isset($_GET['var1']) ? $_GET['var1'] : '';
$query = "SELECT * FROM table1 where id='$num'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
?>
<form name="form1" method="post" action="">
<table border="0" align="center" cellspacing="1" cellpadding="0">
<input name="checkbox[]" type="checkbox" value="<? echo $rows['id']; ?>">
<? echo $row['13']; ?>
<? echo $row['0']; ?>
<input name="delete" type="submit" id="delete" value="Delete">
<button onClick="javascript:window.close();">Close</button>
</table>
</form>
And not only for security's sake, you should get more familiar with PHP and some design patterns before publishing your website.
I am making a database, which will interact with a SQL table.
What I have achieved so far:
Add rows to the table.
Delete rows from the table.
Search rows from the table.
Paginate the results.
What I need to achieve:
A log in prompt when a guest tries to
access the page.
In fact, I have successfully installed a log in script for it, but it seems to not work properly, here is the error:
Fatal error: Allowed memory size of
25165824 bytes exhausted (tried to
allocate 77824 bytes) in
/home/vol3/byethost12.com/b12_3598660/htdocs/coordbase/database.php on line 238
Now that I do not have permission to allow more memory from my host, I would need a way around this.
I have already tried separating the file into multiple pages, but it seems that it still tried to allocate the same amount of bytes.
Here is the file:
<?php
require_once('db.php'); // for database details
ini_set('display_errors',1);
error_reporting (E_ALL ^ E_NOTICE);
require('../include/session.php');
if (!$session->isMember())
{
header("../resources.php");
}
else
{
$self = $_SERVER['PHP_SELF']; //the $self variable equals this file
$ipaddress = ("$_SERVER[REMOTE_ADDR]"); //the $ipaddress var equals users IP
$connect = mysql_connect($host,$username,$password) or die('<p class="error">Unable to connect to the database server at this time.</p>');
mysql_select_db($database,$connect) or die('<p class="error">Unable to connect to the database at this time.</p>');
require('../include/header.php');//Page Header
if($_GET['cmd'] == "delete")
{
echo "<center><h1>Delete</h1></center>";
if(isset($_POST['delete'])) {
$time = date("Y-m-d H:i:s");
$queryc = "DELETE FROM coords WHERE id=".$_GET['id'].";";
$resultc = mysql_unbuffered_query("$queryc") or die("Could not delete the selected base from the database at this time, please try again later.");
$sqls = "INSERT INTO reports SET ip='$ipaddress', date='$time';";
//run the query. if it fails, display error
$report = mysql_unbuffered_query("$sqls") or die("Could not add report to the database, but the base has been deleted successfully.");
echo "<center>The selected base has been deleted from the database successfully!<br>
<a href=http://www.teamdelta.byethost12.com/coordbase/database.php>Back to Main</a><br><br>
<font color=\"red\"><b>YOUR IP HAS BEEN LOGGED. ABUSE OF THIS SYSTEM WILL RESULT IN AN IP BAN!</b></font></center>";
}
else
{
$queryd = "SELECT * FROM coords WHERE id=".$_GET['id'].";";
$resultf = mysql_unbuffered_query("$queryd") or die('<p class="error">There was an unexpected error grabbing the base from the database.</p>');
?>
<center>
<table>
<table width="83%" border="1">
<tr>
<td ><b>Tag</b></td>
<td ><b>Guild</b></td>
<td ><b>Player</b></td>
<td ><b>Base</b></td>
<td ><b>Location</b></td>
<td ><b>Econ</b></td>
<td ><b>Comments</b></td>
</tr>
<?php
while ($rowa = mysql_fetch_array($resultf)) {
$id = stripslashes($rowa['id']);
$tag = stripslashes($rowa['tag']);
$guild = stripslashes($rowa['guild']);
$name = stripslashes($rowa['name']);
$base = stripslashes($rowa['base']);
$location = stripslashes($rowa['location']);
$comment = stripslashes($rowa['comment']);
$id = stripslashes($rowa['id']);
$econ = stripslashes($rowa['econ']);
$maxecon = stripslashes($rowa['maxecon']);
echo('<tr><center><td>['.$tag.']</td><td>'.$guild.'</td><td>'.$name.'</td><td>'.$base.'</td><td>'.$location.'</td><td>'.$econ.'/'.$maxecon.'</td><td>'.$comment.'</td></center></tr>');
}
?>
</table>
</table>
<b>Are you sure you wish to delete the selected base?</b>
<br>
<input type="button" value="Cancel" id="button1" name="button1"onclick="window.location.href='database.php';">
<form action="<?php $self ?>" name="deletefrm" method="post" align="right" valign="bottom" onsubmit="return validate();">
Confirm Delete<input type=checkbox name="confirm"><input type="submit" name="delete" value="Delete" />
</form>
</center>
<br>
<center><font color="red"><b>YOUR IP WILL BE LOGGED. ABUSE OF THIS SYSTEM WILL RESULT IN AN IP BAN!</b></font></center>
<?php
}
}
else
{
if(isset($_POST['add'])) {
?>
<tr>
<td style="background: url(http://www.teamdelta.byethost12.com/barbg.jpg) repeat-x top;">
<center><b><font color="#F3EC84">»Info«</font></b></center>
</td>
</tr>
<tr><!--info content-->
<td style="background: #222222;">
<?php
//fetch data
$data = strip_tags(mysql_real_escape_string($_POST['list']));
$comment = strip_tags(mysql_real_escape_string($_POST['comment']));
$data_lines = explode( "\\r\\n", $data );
$comment_lines = explode("\\r\\n", $comment);
for($i=0;$i<count($data_lines);$i++)
{
$data_fields = explode( ",", $data_lines[$i]);
$time = time();
$queryb = "INSERT INTO coords SET
tag='{$data_fields[0]}',
guild='{$data_fields[1]}',
name='{$data_fields[2]}',
base='{$data_fields[3]}',
econ='{$data_fields[5]}',
maxecon='{$data_fields[6]}',
location='{$data_fields[4]}',
comment='{$comment_lines[$i]}',
ipaddress='$ipaddress' ,
date='$time';";
// if it succeeds, display message
if (mysql_unbuffered_query($queryb))
{
echo('<p class="success">Successful posting of ['.$data_fields[3].']!</p>');
}
else
{
echo('<p class="error">Error could not post ['.$data_fields[3].'] to database!</p>');
}
}//end for loop
}//end if $_POST['add'] statement
?>
<?php
if (isset($_GET['cmd']) == "add"){
?>
<!--start inputbox-->
<center><table width="100%">
<tr>
<td style="background: url(http://www.teamdelta.byethost12.com/barbg.jpg) repeat-x top;">
<center><b><font color="#F3EC84">»Add«</font></b></center>
</td>
</tr>
<tr>
<td style="background: #222222;"><!-- at the bottom of the page, we display our comment form -->
<form action="<?php $self ?>" method="post" onsubmit="return valid(this)">
<table width="100%" border ="0" valign="top">
<tr>
<td>
List:
</td>
<td align="left">
<textarea name="list" rows="10" cols="70"></textarea>
</td>
<td valign="top">
<font color="red"><b>[Post list arranged like so!]</b></font><br>
<br>
E.G:<br>
<br>
(tag),(guild),(player,(base),(coordinates),(econ),(maxecon)<br>
~TD~,~Team Delta~,DarkLink,Base1,D03:56:21:11,101,101<br>
FARM,Guild896,player 5,Base #3,D69:62:89:10,98,135<br>
</td>
</tr>
</tr>
<td>
Comment:
</td>
<td>
<textarea name="comment" rows="10" cols="70"></textarea>
</td>
<td>
<font color="red"><b>[Post comments on a new line for each base!]</b></font><br>
E.G "PS 10/10 PR 10/10"<br>
"PR 5/5 DT 10/10"
</td>
<td>
<td>
</td>
<td valign="bottom" align="right">
<p>
<input type="submit" name="add" value="Add" />
</p>
</td>
</tr>
</table>
</form>
Back to Main
</td>
</tr>
</table></center>
<!--end input box-->
<?php
}
else
{
if (isset($_GET['search']) == "do"){
$title = "<center><h1>Results</h1>";
$search = stripslashes($_GET['searchterm']);
$asearch = trim($search);
$bsearch = strip_tags($asearch);
$csearch = mysql_real_escape_string($bsearch);
$types = "types of search";
switch ($_GET['type']){
case 'name':
$types = "name";
break;
case 'tag':
$types = "tag";
break;
case 'guild':
$types = "guild";
break;
default:
$types = "";
echo "<center><b>Please select a search type before continuing! You are being redirected, please wait.<br>
Click here, if you do not wish to wait.</b></center>";
header("Refresh: 5; url=http://www.teamdelta.byethost12.com/coordbase/database.php");
exit;
break;
}
$querya = "SELECT * FROM coords WHERE `{$types}` LIKE '%{$csearch}%' ORDER BY `{$types}`;";
$result = mysql_unbuffered_query("$querya") or die("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$querya}");
if (mysql_num_rows($result) < 1) {
echo $title;
echo "<b><center>We are sorry to announce that the search term provided: \"{$search}\", yielded no results. <br>"
."<hr>"
."New Search</center></b>";
exit;
}else {
echo $title;
?>
<b>for "<?php echo $search;?>".</b>
<hr>
<table>
<table width="83%" border="1">
<tr>
<td ><b>Tag</b></td>
<td ><b>Guild</b></td>
<td ><b>Player</b></td>
<td ><b>Base</b></td>
<td ><b>Location</b></td>
<td ><b>Econ</b></td>
<td ><b>Comments</b></td>
<td ><b>Delete</b></td>
</tr>
<?php
while ($row = mysql_fetch_array($result)) {
$id = stripslashes($row['id']);
$tag = stripslashes($row['tag']);
$guild = stripslashes($row['guild']);
$name = stripslashes($row['name']);
$base = stripslashes($row['base']);
$location = stripslashes($row['location']);
$comment = stripslashes($row['comment']);
$id = stripslashes($row['id']);
$econ = stripslashes($row['econ']);
$maxecon = stripslashes($row['maxecon']);
echo('<tr><center><td>['.$tag.']</td><td>'.$guild.'</td><td>'.$name.'</td><td>'.$base.'</td><td>'.$location.'</td><td>'.$econ.'/'.$maxecon.'</td><td>'.$comment.'</td><td><a href=database.php?id='.$id.'&cmd=delete>Delete</a></td></center></tr>');
}
echo "New Search";
?>
</table>
</table>
<?php
}
}
else{
// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM coords";
$result = mysql_unbuffered_query($sql, $connect) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
// number of rows to show per page
$rowsperpage = 10;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
?>
</center>
<!--start inputbox-->
<center>
<table width="83%">
<tr>
<td style="background: url(http://www.teamdelta.byethost12.com/barbg.jpg) repeat-x top;">
<center><b><font color="#F3EC84">»Search«</font></b></center>
</td>
</tr>
<tr>
<td style="background: #222222;"><!-- at the bottom of the page, we display our comment form -->
<form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm" onsubmit="return valid(this)">
<table border ="0" width="100%">
<tr>
<td><center>
Search For: <input type="text" name="searchterm">
Player <input type="radio" name="type" value="name" checked> |
Guild Tag <input type="radio" name="type" value="tag"> |
Guild Name <input type="radio" name="type" value="guild">
<input type="hidden" name="search" value="do">
<input type="submit" value="Search">
Add new bases
</tr>
</center>
</td>
</tr>
</form>
</td>
</tr>
</table>
</center>
<!--end input box-->
<hr>
<center>
<table>
<table width="83%" border="1">
<tr>
<td ><b>Tag</b></td>
<td ><b>Guild</b></td>
<td ><b>Player</b></td>
<td ><b>Base</b></td>
<td ><b>Location</b></td>
<td ><b>Econ</b></td>
<td ><b>Comments</b></td>
<td ><b>Delete</b></td>
</tr>
<?php
$query = "SELECT * FROM coords ORDER BY `tag` ASC LIMIT $offset, $rowsperpage;";
$result = mysql_unbuffered_query("$query") or die('<p class="error">There was an unexpected error grabbing routes from the database.</p>');
// while we still have rows from the db, display them
while ($row = mysql_fetch_array($result)) {
$id = stripslashes($row['id']);
$tag = stripslashes($row['tag']);
$guild = stripslashes($row['guild']);
$name = stripslashes($row['name']);
$base = stripslashes($row['base']);
$location = stripslashes($row['location']);
$comment = stripslashes($row['comment']);
$id = stripslashes($row['id']);
$econ = stripslashes($row['econ']);
$maxecon = stripslashes($row['maxecon']);
echo('<tr><center><td>['.$tag.']</td><td>'.$guild.'</td><td>'.$name.'</td><td>'.$base.'</td><td>'.$location.'</td><td>'.$econ.'/'.$maxecon.'</td><td>'.$comment.'</td><td><a href=database.php?id='.$id.'&cmd=delete>Delete</a></td></center></tr>');
}
?>
</table>
</table>
<?php
/****** build the pagination links ******/
// range of num links to show
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/
}//end else of search
}//end else of add
}//end else of delete
?>
</center>
<?php
require('../include/footer.php');//Page footer
}
?>
This would be around line 238:
<?php
//fetch data
$data = strip_tags(mysql_real_escape_string($_POST['list']));
$comment = strip_tags(mysql_real_escape_string($_POST['comment']));
$data_lines = explode( "\\r\\n", $data );
$comment_lines = explode("\\r\\n", $comment);
for($i=0;$i<count($data_lines);$i++)
{
$data_fields = explode( ",", $data_lines[$i]);
$time = time();
$queryb = "INSERT INTO coords SET
tag='{$data_fields[0]}',
guild='{$data_fields[1]}',
name='{$data_fields[2]}',
base='{$data_fields[3]}',
econ='{$data_fields[5]}',
maxecon='{$data_fields[6]}',
location='{$data_fields[4]}',
comment='{$comment_lines[$i]}',
ipaddress='$ipaddress' ,
date='$time';";
// if it succeeds, display message
if (mysql_unbuffered_query($queryb))
{
echo('<p class="success">Successful posting of ['.$data_fields[3].']!</p>');
}
else
{
echo('<p class="error">Error could not post ['.$data_fields[3].'] to database!</p>');
}
}//end for loop
}//end if $_POST['add'] statement
?>
I have noticed that the memory exceeds the limit when I include session.php to my file.
The problem is that I need that file for my log in prompt to work.
Check for recursions, this code cannot possibly exhaust memory. Try adding echo's around the code.