Jump to content


Photo

PHP code, no idea why wrong, getting desperate


  • Please log in to reply
3 replies to this topic

#1 RoughitforGreen247

RoughitforGreen247
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 14 May 2006 - 09:14 PM

Hi, I'm having a lot of trouble with some code I am working on. I am trying to write a script that will allow me to ensure the appropriateness of pictures people upload before they appear on my website. To do that, I assign the value 1 to the screen column in the picture's corresponding mySQL db entry when every file is uploaded. I have a page that displays all the entries in the database with the value of one, and puts a little form box underneath. The columns in the database referenced in the code are: path (the filename of the picture like picture.jpg), name (the name the uploader selects to have displayed for the picture), description (the short line describing the picture that will be placed under the picture on the website).

Code:

<form action="done.php" method="post">
<?php
mysql_connect("localhost", "roughitf_firsty", "******") or die(mysql_error()); mysql_select_db("roughitf_first") or die(mysql_error()); //connects to the database
$data = mysql_query("SELECT * FROM uploads WHERE screen='1'") or die(mysql_error()); // This will load the entries that have not already been approved
$info = mysql_fetch_array($data);
echo "<h2><center>" . $info['name'] . "</h2>"; //Prints the name of the picture
echo "<center><br><br><br><br><img src='http://iequalsgamer....uploads/files/" . $info['path'] . "'><br><br>"; //prints the picture itself
echo "<center>" . $info['description'] . "<br><br>"; //prints the description
echo "<input type='checkbox' name='" . $info['path'] . "'> Is this inapropriate?<br><br>"; // Where i put in the decision, the input is named the path of the corresponding image
while($info = mysql_fetch_array($data)) //Loop, does same thing as above
{
echo "<h2><center>" . $info['name'] . "</h2>";
echo "<center><br><br><br><br><img src='http://iequalsgamer....uploads/files/" . $info['path'] . "'><br><br>";
echo "<center>" . $info['description'];
echo "<input type='checkbox' name='" . $info['path'] . "'> Is this inapropriate?<br><br>";
}
?>
<input type="submit">
</form>


That is the form code. It then sends the information to the code below, which goes through and deletes every entry that was checked by the form page, and changes the value to 2 on every entry that was not checked.

Code:

<?php
mysql_connect("localhost", "roughitf_firsty", "******") or die(mysql_error()); mysql_select_db("roughitf_first") or die(mysql_error()); //connects to the database
$data = mysql_query("SELECT * FROM uploads WHERE screen='1'") or die(mysql_error()); //Loads the entries again that have not been checked
$info = mysql_fetch_array($data);
$path = $info['path'];
$n = $_POST['$path']; //gets the value of the checkbox that corresponds to the picture
if ($n){
mysql_query("DELETE FROM uploads WHERE path ='$path'"); //deletes from the database when the checkbox was checked
unlink(files/$path); //This is supposed to delete the file, but I'm not sure whether it works, and is not the problem that concerns me
}
else {
$name = $info['name']; //loads the information about the entry for resubmitting
$description = $info['description'];
mysql_query("DELETE FROM uploads WHERE path ='$path'"); //deletes the entry
mysql_query("INSERT INTO uploads VALUES ( '$path','$name','$description','user','2')") or die(mysql_error()); //rewrites the entry with the value 2, marking that it has been checked
}
while($info = mysql_fetch_array($data)) //While loop just does the same thing over and over again
{
$path = $info['path'];
$n = $_POST['$path'];
echo $path . " <- PATH<br><br>";
if ($n){
mysql_query("DELETE FROM uploads WHERE path ='$path'");
unlink(files/$path);
}
else {
$name = $info['name'];
$description = $info['description'];
$rand = mysql_query("DELETE FROM uploads WHERE path ='$path'");
mysql_query("INSERT INTO uploads VALUES ( '$path','$name','$description','user','2')") or die(mysql_error());
}
}
?>


My problem is, the form does not appear to pass anything on, or atleast nothing my second piece of code can read. It always changes the value to 2, and never deletes it from the database. Thank you so much, I have been trying to find the problem in the code for over a week now.

#2 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 14 May 2006 - 09:46 PM

change lines
$n = $_POST['$path'];
to
$n = $_POST[$path];
or to
$n = $_POST["$path"];


#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 14 May 2006 - 09:56 PM

This line
<?php $n = $_POST['$path']; ?>
is your problem. Remove the single quotes. When you use the single quotes, PHP will look for the index of with the literal sting $path, not the value of the variable [!--coloro:red--][span style=\"color:red\"][!--/coloro--]$path[!--colorc--][/span][!--/colorc--]. Also remove the single quotes from a similar line later.

Your method still may not work, since only checkboxes that were checked are returned to your script.

Why do you do your code once and then again in the "while" loop? Just do all your code in the "while" loop.

Ken

#4 RoughitforGreen247

RoughitforGreen247
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 15 May 2006 - 01:31 AM

Thanks for the help, guys. The code still seems to be malfunctioning, but I really appreciate the attempt. I doubled the code because the code I learned off of at php.about.com was doubled, so I wasn't sure whether it was necessary. I've tried changing the checkbox around, but the POST still seems to return nothing. Ah well, I'll keep trying. And thanks again.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users