How can I accept or delete any pending post? - php

I am very very disappointed this time. I wasted my whole day and I couldn't help me out. Here is the problem:
I need accept or delete the pending jobs. If I accept, only then it will publish on my site. Please look at the image.
Here I am trying this code to handle this matter. Firstly if anyone post a new job, I pass a hidden value=0 according to the job.
<input type="hidden" name="pending" value=0/>
Then I store it in my database.
INSERT INTO job (Pending) VALUES ('$_POST[pending]');
Then I go to the image(above) page to accept or delete.
echo "<a href='accept.php?accept=1'>ACCEPT</a> Or ";
echo "<a href='accept.php?accept=0'>DELETE</a>";
After accepting Pending column will be replaced from 0 to 1 and thus it will be published. Or deleting time, the job will be simply be deleted from database.
$accept=$_GET['accept'];
if($accept==1){
mysql_query("UPDATE job SET Pending='$accept'
WHERE ID='5'");
}
else{
mysql_query("DELETE FROM job WHERE ID='5'");
}
In the 'job' table, I use a primary auto increment column named ID. I put 5 for ID. So this code will work only for the pending job which ID is 5. So my problem is here. How can I alternate this 5 with a dynamic variable?
FOR MORE ASSISTANCE HERE IS the CODE OF 'PENDING JOBS' PAGE;
$result1 = mysql_query("SELECT * FROM job ORDER BY ID DESC");
while($row1 = mysql_fetch_array($result1))
{
$id=$row1['ID'];
$cat=$row1['Category'];
$title=$row1['Title'];
$type=$row1['Type'];
$desp=$row1['Description'];
$salary=$row1['Salary'];
$day=$row1['Day'];
$month=$row1['Month'];
$year=$row1['Year'];
$info=$row1['Contact'];
$pending=$row1['Pending'];
$now = time();
$last_date = strtotime("$year-$month-$day");
$datediff = $last_date - $now;
$day_left=ceil($datediff/(60*60*24));
if($day_left>=0&&$pending==0){
echo "<div class=cat>Job field: $cat<br/></div>";
echo "<div class=yel2>";
echo "This is a <b>$type time</b> job and we are looking for <b>$title</b>. $desp<br/>";
if ($salary!=0) echo "Salary: $salary<br/>";
echo "Contact info: $info<br/>Last date: $day-$month-$year";
if($day_left==0)
echo "<br/><span class=ash><i>Today is the last day to apply</i><br/>";
else
echo "<br/><span class=ash><i>$day_left day(s) left</i></span><br/>";
echo "<span class=acc><a href='accept.php?accept=1'>ACCEPT</a><span> Or ";
echo "<span class=del><a href='accept.php?accept=0'>DELETE</a></span>";

Just pass the id along with the accept url.
echo "<span class=acc><a href='accept.php?id=$id&accept=1'>ACCEPT</a><span> Or ";
You can grab the id inside the accept.php script using
$post_id = $_GET['id'];
The code would require little change
$accept = intval($_GET['accept']); // safe against sql-injections
$post_id = intval($_GET['id']);
if($accept==1){
mysql_query("UPDATE job SET Pending='$accept'
WHERE ID='$post_id'");
}
else{
mysql_query("DELETE FROM job WHERE ID='$post_id'");
}

Related

Showing database entries on a PHP calendar

I'm creating a web-based booking system where a user can add bookings into a database, and the details stored in the database are (among others) "DateBooked, StartTime, EndTime, Room".
Here's the bit that pulls the relevant info for the day the user has clicked on from the database:
$query="SELECT * FROM bookings WHERE DateBooked = '{$year}-{$selectedmonth}-{$selectedday}'";
$result = mysql_query($query);
$todayarray = mysql_fetch_array($result);
And the PHP:
$roomcount = 4;
$room = 1;
while ($room <= $roomcount)
{
echo "\n<div class=\"roomtimes\">";
echo "\n<table border=1>";
echo "\n<tr><th class=\"titlecell\">Room $room</th></tr>";
$cellnum = 10;
while ( $cellnum < 23 )
{
echo "\n<tr>";
echo "\n<td class=\"linkcell";
if ($selectedtime==$cellnum)
{
echo " selectedcell";
}
echo "\">";
echo "$cellnum:00</td>";
echo "\n</tr>";
$cellnum++;
}
$room++;
echo "\n</table>";
echo "\n</div>";
}
So my question is, how can I add a bit of text saying "BOOKED" inside each table cell if a entry exists for that room and the number in that cell is in between the start time and end time of that booking?
I'm new to this site so let me know if I've done something wrong or you need more information, thanks!
You need to divide the cell content html and insert your "BOOKED" text there. And I suggest you use mysql_fetch_assoc so you get and associative array and it's easier to get the fields. If I'm understanding your code correctly you might be able to use something like this. This example is a replacement for the row that has the link output in it. I've used mysq_fetch_assoc here to get the StartTime and EndTime fields from the database. You might need to change the fields a bit depending in what data format they are.
echo "$cellnum:00";
if ($todayarray['StartTime'] <= $cellnum && $todayarray['EndTime'] >= $cellnum && $todayarray['Room'] == $room) echo 'BOOKED';
echo "</td>";

I am trying to have my "Details" page to reference two seperate parts of my server that are linked via techID [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
Okay, so I have my page "List Customers" which then links to "List Jobs" adding on ?custID= into the URL, then on List jobs I have used $_GET["custID"] so my query uses the custID from the URL. that works fine, lists out my jobs for said customer using their ID.
My problem comes up now, I need the links on this page to give my third page the techID as well. But the techID is in jobDetails, so my query cannot give them the techID as it is querying my job section.
This is my "ListJobs.php" page, which upon choosing a customer in the page before it, loads this page with the url ListJobs.php?custID=001 (001 being an example, it will give the number depending on your choice of customer).
<?php
// Get data from the database depending on the value of the id in the URL
mysql_select_db($database_con_sim5, $con_sim5);
$query_Recordset1 = "SELECT * FROM job WHERE custID=" . $_GET["custID"] ;
$Rs1 = mysql_query($query_Recordset1);
// Loop the recordset $rs
while($row = mysql_fetch_array($Rs1)) {
$strName1 = $row['jobID'] . " " . $row['jobDesc'] . " " . $row['computerName'];
$strLink = "<a href = 'jobDetails.php?jobID=".$row['jobID']."'>".$strName1."</a>";
// Write the data of the person
echo "<li>" . $strLink . " </li>";
}
// Close the database connection
mysql_close();
?>
Then on pressing one of the links, link to jobDetails.php?jobID= with the job number.
I am able to show all the details in Job Detail with this, but I also need my Tech Name to appear, Tech Name is not in Job Details, but Tech ID is.
Here is my Job Details page coding :
<?php
// Get data from the database depending on the value of the id in the URL
mysql_select_db($database_con_sim5, $con_sim5);
$query_Recordset1 = "SELECT * FROM jobDetail WHERE jobID=" . $_GET["jobID"] ;
$query_Recordset2 = "SELECT technician.techName FROM technician
WHERE techID=" . $query_Recordset1["techID"] ;
$Rs1 = mysql_query($query_Recordset1);
$Rs2 = mysql_query($query_Recordset2);
while($row1 = mysql_fetch_array($Rs1)) {
while($row2 = mysql_fetch_array($Rs2)) {
echo "<dt><strong>Job Note ID:</strong></dt><dd>".$row1["jobNoteID"]."</dd>";
echo "<dt><strong>Job Notes:</strong></dt><dd>".$row1["jobNotes"]."</dd>";
echo "<dt><strong>Date Completed:</strong></dt><dd>".$row1["dateCompleted"]."</dd>";
echo "<dt><strong>Time Spent:</strong></dt><dd>".$row1["timeSpent"]."</dd>";
echo "<dt><strong>Job ID:</strong></dt><dd>".$row1["jobID"]."</dd>";
echo "<dt><strong>Technician ID:</strong></dt><dd>".$row1["techID"]."</dd>";
echo "<dt><strong>Technician Name:</strong></dt><dd>".$row2["techName"]."</dd>";
}
}
// Close the database connection
mysql_close();
?>
The error I am getting is:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\Sim5Server\Pages\jobDetails.php on line 129
That line is:
while($row2 = mysql_fetch_array($Rs2)) {
I hope I am making any sense AT ALL.
To sum up, I need my final page to show data from mysql technician using the Primary/index key techID.
Some way to add techID onto either listJobs' links to job details or in job details' second Recordset.
EDIT:
I should probably state this will never be used on the net, I only need it to work for an assignment. in future, thanks to a comment, I will no longer be using mysql_* I am jsut using them as my entire workbook tells us to use it.
Try making this change in your jobDetails.php page
$query_Recordset1 = "SELECT * FROM jobDetail WHERE jobID=". (int) $_GET["jobID"];
$Rs1 = mysql_query($query_Recordset1) or die(mysql_error());
while($row1 = mysql_fetch_array($Rs1)) {
$query_Recordset2 = "SELECT technician.techName FROM technician
WHERE techID=" . $row1["techID"] ;
$Rs2 = mysql_query($query_Recordset2) or die(mysql_error());
while($row2 = mysql_fetch_array($Rs2)) {
For the second query you were using the result resource from executing the first query as techID. Also you will have to query for techName from inside the while loop fetching job details, since only then will you have the techID.

How do I loop through results and display day of the week once at every change in day using php and mysql?

with my current query and loop:
$sched = mysql_query("SELECT *
FROM `shows`
ORDER BY `shows`.`show_time` ASC")
or die(mysql_error());
echo "<ul>";
while($row = mysql_fetch_array($sched)){
echo "<li><a href=\"#$row[id]\">";
echo $row['title'];
echo "</li>";
}
echo "</ul>";
This works great for displaying my results like this:
Name of show 1
Name of show 2
Name of show 3
However, I want to add an item to the list at the beginning of every change in day so it would display as follows:
Monday
Name of show 1
Name of show 2
Tuesday
Name of show 3
Wednesday
Name of show 4
I can't quite wrap my brain around the loop needed to do this. It might be helpful to know that the field 'show_time' is a datetime type, so it has the information for both time and day of week.
Thanks.
Simple tweak:
echo "<ul>";
$curDay='';
while($row = mysql_fetch_array($sched)){
$d=date('l',strtotime($row['show_time']));
if($d!=$curDay){
echo '<li>'.$d.'</li>';
}
$curDay=$d;
echo '<li><a href="#',$row['id'],'">',$row['title'],"</li>";
}
echo "</ul>";
Initialize $curDay, and then each time through the loop, check to see if the particular day is different than the last time through the loop (or different from the initial value)
The best way to do this is to keep a flag in your loop, and compare to the previous value.
Eg.
$previousDay = '';
while($row = mysql_fetch_assoc()) {
if ($previousDay != date('l', $row['show_time'])) {
echo '<h2>' . $date('l', $row['show_time']) . '</h2>';
}
...
$previousDay = date('l', $row['show_time']);
}
Adjust your query to sort by show_time first.
"SELECT * FROM `shows` ORDER BY `show_time`, `shows` ASC"
Then keep track of the current day as Shad suggests, parsing show_time to determine the day.

Rating script problem

5 star rating script I have is made based on this tutorial http://php.about.com/od/finishedphp1/ss/rating_script.htm
I have changed it a bit based on comments on the mentioned site,but script still has some issues.
When i rate something script refreshes the site and adds the needed parameters in query string but the rest of the script is not triggered by it.
Echo "Rate ";
Echo "1 | ";
Echo "2 | ";
Echo "3 | ";
Echo "4 | ";
Echo "5";
this thing is pretty much skipped.
$mode = $_GET['mode'];
$voted = $_GET['voted'];
$id = $_GET['id'];
if ($mode=='vote')
{
if(isset($_COOKIE['146829gigapuding']))
{
Echo "Sorry You have already ranked that site";
}
else
{
$month = 2592000 + time();
setcookie('146829gigapuding',Voted,$month);
mysql_query ("UPDATE searchengine SET rating = rating+$voted, votes = votes+1 WHERE id = $id");
Echo "Your vote has been cast";
}
}
The sql connection,query and bunch of other code is there but there were no problems with it,i tried moving the code order but nothing.
Another thing that worries me is there a way to remove the ?mode=vote... parameters afther vote is cast.
tnx in advance.
As I see here
".$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']."?mode=vote&voted=1&id=".$data[id]."
you have ? (question mark) twice, which makes your url invalid. Try changing it to
".$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']."&mode=vote&voted=1&id=".$data[id]."
and see what will happen :)
p.s.: the change is ? to & before mode

PHP & MySQL vote count problem?

I found this script on about.com which I'm trying to learn from on how to create a rating system but the script for some reason wont count a vote when the link is clicked and just reloads the page.
I was wondering how can I fix this problem? And what part of the code do I need to change and where?
Here is the full script below.
<?php
// Connects to your Database
mysql_connect("localhost", "root", "", "sitename") or die(mysql_error());
mysql_select_db("sitename") or die(mysql_error());
//We only run this code if the user has just clicked a voting link
if ( $mode=="vote") {
//If the user has already voted on the particular thing, we do not allow them to vote again $cookie = "Mysite$id";
if(isset($_COOKIE[$cookie])) {
echo "Sorry You have already ranked that site <p>";
} else {
//Otherwise, we set a cooking telling us they have now voted
$month = 2592000 + time();
setcookie(Mysite.$id, Voted, $month);
//Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating
mysql_query ("UPDATE vote SET total = total+$voted, votes = votes+1 WHERE id = $id");
echo "Your vote has been cast <p>";
}
}
//Puts SQL Data into an array
$data = mysql_query("SELECT * FROM vote") or die(mysql_error());
//Now we loop through all the data
while($ratings = mysql_fetch_array( $data )) {
//This outputs the sites name
echo "Name: " .$ratings['name']."<br>";
//This calculates the sites ranking and then outputs it - rounded to 1 decimal
if($ratings['total'] > 0 && $ratings['votes'] > 0) {
$current = $ratings['total'] / $ratings['votes'];
} else {
$current = 0;
}
echo "Current Rating: " . round($current, 1) . "<br>";
//This creates 5 links to vote a 1, 2, 3, 4, or 5 rating for each particular item
echo "Rank Me: ";
echo "Vote 1 | ";
echo "Vote 2 | ";
echo "Vote 3 | ";
echo "Vote 4 | ";
echo "Vote 5<p>";
}
?>
$mode is never set? While it may have worked if register globals was on, it is not on by default any more (and is removed in later versions of PHP)
//We only run this code if the user has just clicked a voting link
if ( $mode=="vote") {
Maybe you mean
if ( $_GET['mode']=="vote") {
The same goes for $id and $voted, which are also never set.
EDIT
I also would like to add, that if I went and changed id to 1';DROP TABLE vote; You would have a whole lot of data lost. Look at SQL Injection
EDIT
If the row in the table doesn't exist, you will need to INSERT it before you can UPDATE it.
I can also see $cookie is never set, looking at the code it should be 'Mysite' . $id. I added quotes for the string, though PHP will treat any unquoted text as string but avoid misunderstanding and errors later, its always a good idea.
Also this script assumes PHP option register_globals is on, you need to make that register_globals = ON in your php.ini

Categories