Jump to content

[SOLVED] Can someone troubleshoot a simple script?


yuckysocks

Recommended Posts

Hi,

 

My goal is to show a table of user-posted results to an admin, who can then approve the data and change the value of the "approved" column from 0 to 1.

 

<?php

// Connecting, selecting database
$link = mysql_connect('localhost', 'xxxx', 'xxxx')
    or die('Could not connect: ' . mysql_error());
mysql_select_db('case_studies') or die('Could not select database');

if(!isset($cmd))
{
// Performing SQL query FOR UNAPPROVED CASES
$query = 'SELECT id, schoolname, schoolcity, state FROM casestudies WHERE approved = 0';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
echo "<table class=\"sortable\">\n";
echo "<th>School Name</th> <th>School City</th> <th>State</th> <th>Approval Link</th>";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr>\n";
    echo "\t\t<td><a href='casestudy.php?id=".$line['id']."'>".$line['schoolname']."- Check me for Accuracy</a></td>\n";
    echo "\t\t<td>".$line['schoolcity']."</td>\n";
    echo "\t\t<td>".$line['state']."</td>\n";
    echo "\t\t<td><a href='admin.php?cmd=approve&id=".$line['id']."'>Approve the $schoolname case study and make it public</a>";
    echo "\t</tr>\n";
}
echo "</table>\n";
}

if($_GET["cmd"]=="approve")
{
$id = $_GET["id"];
$sql = "UPDATE casestudies SET approved = 1 WHERE id = $id";
}

// Closing connection
mysql_close($link);
?>

 

 

Some ideas are cobbled together from different sample scripts, so I don't really know where the issue lies. When I click on the "Approve" link, it goes to a blank page, but the data remains unchanged. Help!

 

Also, thanks so much to everyone in the forum, you all are VERY helpful.

 

-Alex

Link to comment
Share on other sites

This should work

<?php

// Connecting, selecting database
$link = mysql_connect('localhost', 'xxxx', 'xxxx')
    or die('Could not connect: ' . mysql_error());
mysql_select_db('case_studies') or die('Could not select database');

if(!isset($cmd))
{
// Performing SQL query FOR UNAPPROVED CASES
$query = 'SELECT id, schoolname, schoolcity, state FROM casestudies WHERE approved = 0';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
echo "<table class=\"sortable\">\n";
echo "<th>School Name</th> <th>School City</th> <th>State</th> <th>Approval Link</th>";
while ($line = mysql_fetch_assoc($result)) {
$id = $line["id"];
$schoolname = $line["schoolname"];
$schoolcity = $line["schoolcity"];
$state = $line["state"];
echo <<<END
\t<tr>\n;
    \t\t<td><a href='casestudy.php?id=$id'>$schoolname- Check me for Accuracy</a></td>\n;
    \t\t<td>$schoolcity</td>\n;
    \t\t<td>$state</td>\n;
    \t\t<td><a href='admin.php?cmd=approve&id=$id'>Approve the $schoolname case study and make it public</a>;
    \t</tr>\n
END;
}
echo "</table>\n";
}

if($_GET["cmd"]=="approve")
{
$id = $_GET["id"];
$sql = "UPDATE casestudies SET approved = 1 WHERE id = $id";
}

// Closing connection
mysql_close($link);
?>

Link to comment
Share on other sites

What I have right now is

 

<?php

// Connecting, selecting database
$link = mysql_connect('localhost', 'xxxx', 'xxxx')
    or die('Could not connect: ' . mysql_error());
mysql_select_db('case_studies') or die('Could not select database');

if(!isset($cmd))
{
// Performing SQL query FOR UNAPPROVED CASES
$query = 'SELECT id, schoolname, schoolcity, state FROM casestudies WHERE approved = 0';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
echo "<table class=\"sortable\">\n";
echo "<th>School Name</th> <th>School City</th> <th>State</th> <th>Approval Link</th>";
while ($line = mysql_fetch_assoc($result)) {
$id = $line["id"];
$schoolname = $line["schoolname"];
$schoolcity = $line["schoolcity"];
$state = $line["state"];
echo <<<END
\t<tr>\n;
    \t\t<td><a href='casestudy.php?id=$id'>$schoolname- Check me for Accuracy</a></td>\n;
    \t\t<td>$schoolcity</td>\n;
    \t\t<td>$state</td>\n;
    \t\t<td><a href='admin2.php?cmd=approve&id=$id'>Approve the $schoolname case study and make it public</a>;
    \t</tr>\n
END;
}
echo "</table>\n";
}

if($_GET["cmd"]=="approve")
{
$id = $_GET["id"];
$sql = "UPDATE casestudies SET approved = 1 WHERE id = $id";
}

// Closing connection
mysql_close($link);
?>

 

Clicking on the links still won't update my data... Maybe my query is wrong?

 

Also, fyi, I stripped the semi-colons from the echo-->HTML section. I ended up with 5 semis x 3 records = 15 semi colons in a line up top.

 

Any hints would be great. Thanks!

Link to comment
Share on other sites

holy crap I'm blind!

<?php

// Connecting, selecting database
$link = mysql_connect('localhost', 'xxxx', 'xxxx')
    or die('Could not connect: ' . mysql_error());
mysql_select_db('case_studies') or die('Could not select database');

if(!isset($cmd))
{
// Performing SQL query FOR UNAPPROVED CASES
$query = 'SELECT id, schoolname, schoolcity, state FROM casestudies WHERE approved = 0';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
echo "<table class=\"sortable\">\n";
echo "<th>School Name</th> <th>School City</th> <th>State</th> <th>Approval Link</th>";
while ($line = mysql_fetch_assoc($result)) {
$id = $line["id"];
$schoolname = $line["schoolname"];
$schoolcity = $line["schoolcity"];
$state = $line["state"];
echo <<<END
\t<tr>\n;
    \t\t<td><a href='casestudy.php?id=$id'>$schoolname- Check me for Accuracy</a></td>\n;
    \t\t<td>$schoolcity</td>\n;
    \t\t<td>$state</td>\n;
    \t\t<td><a href='admin2.php?cmd=approve&id=$id'>Approve the $schoolname case study and make it public</a>;
    \t</tr>\n
END;
}
echo "</table>\n";
}

if($_GET["cmd"]=="approve")
{
$id = $_GET["id"];
$sql = "UPDATE casestudies SET approved = 1 WHERE id = $id";
mysql_query($sql) or die(mysql_error()); /* FORGOT TO RUN THE QUERY..don't feel bad. I missed it too */
}

// Closing connection
mysql_close($link);
?>

Link to comment
Share on other sites

Well, at least you're blind and not a total newbie who didn't recognize that at all. I printed out the file, and went through it line by line, writing in English what each part did, and next to

 

$sql = QUERY

 

I wrote "performs operation". Shows how much I still have to learn! Thanks so much for all the help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.