Jump to content

CGTroll

Members
  • Posts

    16
  • Joined

  • Last visited

Everything posted by CGTroll

  1. Thanks a lot Psycho! !mysqli_num_rows($result), Didn't know of that one. That will solve/simplify a lot of other cases too!
  2. I am trying to show two different options, depending on if the table is empty or not. It's a form to post images of artists. The user should be able to add images to existing artist, but if not artist has yet been added, then the option to add a new should be showed instead. I haven't added any artist to the DB yet, and therefor is empty, yet this code always shows the empty dropdown menu and never the text box. What is the best way to check if a query is empty or not to use in an if-statement? $query = "SELECT artist_id, artist_name FROM content_artists ORDER BY artist_name ASC"; $result = $mysqli->query($query); if ($result) { echo " <td>Pick from existing artits:</td> <td>"; echo "<select name='ac_artist_id'>"; while ($row = $result->fetch_array(MYSQLI_ASSOC)) { echo "<option value='" . $row['artist_id'] . "'>" . $row['artist_name'] . "</option>"; } echo "</select></td>"; } else { echo " <td>Name of artist:</td> <td><input type=\"text\" name=\"artist_name\"></td> "; }
  3. Here is the table structure. The only data stored on the column "news_image" is the file name, which for the test has been "test_bilde-1.jpg" (image_name = news_image): CREATE TABLE IF NOT EXISTS `content_news` ( `news_id` int(11) NOT NULL AUTO_INCREMENT, `news_priority` varchar(10) NOT NULL DEFAULT '', `news_layout` varchar(10) NOT NULL DEFAULT '', `news_menu_title` varchar(70) NOT NULL DEFAULT '', `news_title` varchar(150) NOT NULL DEFAULT '', `news_time` varchar(100) NOT NULL DEFAULT '', `news_image` varchar(200) NOT NULL DEFAULT '', `news_image_txt` varchar(150) NOT NULL, `news_content` longtext NOT NULL, `date` date NOT NULL DEFAULT '0000-00-00', KEY `id` (`news_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='Aktuelt på Oppegardkunstforening' AUTO_INCREMENT=65 ;
  4. If that was the case, then wouldn't the whole query fail? All other columns are updated, except the news_image.
  5. Could there be a bug or similar in the MySQL table causing this error?
  6. I have tried that as well and it still deletes the value in the table. I just can't understand what is causing this. Leaving those columns that doesn't need to updated out, is what I have used to do, but it didn't work, that's why I set it up like I did, setting the value by updating it with the old value. Screen dump from the query where I have left the column out.
  7. Yes, and that is what is strange, it says the correct filename is being sent to table, yet it is left blank.
  8. I have a setup where the user has the following options: - upload a new image to an existing article that has no existing image - Updating article removing existing image leaving article with no image at all - update article with a new image, replacing old image (delete it). Deleting old image and removing image name in table works. Replacing old image with a new and update table with new image name works. Uploading new image to an article that has no existing image and updating table with new image new works. But, if user only wants to update the article itself, but keep existing image, the old image name is removed in table. I have tried to update table by retrieving old image name and posting it again into the table, shows up blank I have tried to update table removing the code to update the $image_name at all by only updating those that are changed, this also removes the old image name, leaving 'image_name' in table blank. Any idea what could cause this? Here is my code where I have tried to update table with old image name: (The code has been edited as it is part of a file called upload_data.php that also handel 8 other tables for other pages. If the code is missing something, that would be why.. The complete file is attached). require_once 'includes/db_connect.php'; include 'includes/functions.php'; sec_session_start(); if(login_check($mysqli) !== true) { header('Location: ./login.php?login'); exit; } if ($_SESSION['perm'] < 69) //This is the lowest rank that can view the page all ranks above can view but none below. { echo "<script>"; echo "alert(\" Desverre ! Du har ikke rettigheter til å se denne siden!\");"; //Show Message Box echo "</script>"; echo "<script>"; echo "window.location = \"index.php\";"; //Send To Secure Index Page echo "</script>"; exit; } $_GET['pid']; $pid = $_GET['pid']; // Get data from form $content_id = $_POST["content_id"]; $news_priority = $_POST['news_priority']; $layout = $_POST['news_layout']; $news_menu_title = $_POST['news_menu_title']; $news_title = $_POST['news_title']; $news_time = $_POST['news_time']; $myFile = $_FILES['myFile']; $old_image = $_POST['old_image']; $news_image_txt = $_POST['news_image_txt']; $news_content = $_POST['news_content']; $date = $_POST['date']; // Define other options $table = "content_news"; $image = 'news_image'; define("UPLOAD_DIR", "../images/news/"); $imageToDelete = $r["news_image"]; $DirToDeleteIn = '../images/news/'; // Check if file was uploaded if (is_uploaded_file($_FILES['myFile']['tmp_name'])) { $fileCheck = "1"; } else { $fileCheck = "2"; } // If file exists, upload it if ($fileCheck == 1) { // Process file upload include "includes/process_image.php"; // End Process file upload } // END If file exists, upload it // Check if old image if($old_image != NULL) { // Check if it needs to be deleted $checkVars2 = array(4, null); if(in_array($layout, $checkVars2)) { // Delete image if (!unlink($DirToDeleteIn.$old_image)) { // If it failed to delete echo ("OBS! Klarte ikke slette $DirToDeleteIn$old_image , ERROR line 259"); // Pop-up-message echo "<script>"; echo "alert(\"Advarsel! Innholdet ble IKKE oppdatert fordi tilhoereden fil $DirToDeleteIn$imageToDelete heller ikke kunne slettes. Vennlisgt kontakt webmaster for hjelp!\");"; echo "window.location = \"administrate.php?pid=$pid\";"; echo "</script>"; exit; } else { // If file was deleted, show message echo ("Deleted $DirToDeleteIn$old_image"); } } } else { // If there are no old file, check if layout is set wrong. $checkVars3 = array(1, 2, 3); if(in_array($layout, $checkVars3)) { // If layout is set to 1, 2 or 3, check if this is correct by checking if new file is present if($fileCheck == 2) { // No new file was added, layout was set incorectly, fix it. $layout = 4; echo "New layout was set to $layout"; } } } // Check if existing file has to be deleted because if new file // First check if new file has been uploaded if($fileCheck == 1) { // Check if old file exist if($old_image != NULL) { // IF old file exist, delete it // Delete image if (!unlink($DirToDeleteIn.$old_image)) { // If it failed to delete echo ("OBS! Klarte ikke slette $DirToDeleteIn$old_image , ERROR line 259"); // Pop-up-message echo "<script>"; echo "alert(\"Advarsel! Innholdet ble IKKE oppdatert fordi existerende fil $DirToDeleteIn$imageToDelete heller ikke kunne slettes. Vennlisgt kontakt webmaster for hjelp!\");"; echo "window.location = \"administrate.php?pid=$pid\";"; echo "</script>"; exit; } else { // If file was deleted, show message echo ("Deleted $DirToDeleteIn$old_image"); } } } // End check to see if old file has to be deleted because of new file is uploaded. } // Prepeare update DB if($fileCheck == 2) // If new file is not uploaded { if($old_image != NULL) // If old file exists { if($layout != '4') // If layout is set to 1, 2, or 3 { // Update possible imge text and set feedback message for image $updated_image_txt = $news_image_txt; $feedback_on_image = " Er fortsatt " . $old_image; // Update DB with no new image $inputquery = "UPDATE `content_news` SET `news_priority`='$news_priority', `news_layout`='$layout', `news_menu_title`='$news_menu_title', `news_title`='$news_title', `news_time`='$news_time', `news_image`='$old_image', `news_image_txt`='$updated_image_txt', `news_content`='$news_content', `date`='$date' WHERE `news_id`='$content_id'"; // Print UPDATE results $mysqli->query($inputquery); print "<br /><br /><br /><br /><br /><br /><br />\n"; print "$inputquery <br />linje 357\n"; echo "<p>Intet nytt bilde å laste opp</p>"; echo "<script>"; echo "alert(\"Gratulerer! Nyheten er naa endret. Gammelt bilde: $feedback_on_image \");"; echo "window.location = \"administrate.php?pid=$pid\";"; echo "</script>"; if($mysqli->connect_errno) { echo 'OBS! Klarte ikke å endre nyhet. Kontakt webmaster for hjelp. '; } } else // If layout is set to 4 { // Set file name and file text to blank, and set feedback image for file unset($GLOBALS['updated_image_name']); unset($GLOBALS['updated_image_txt']); $feedback_on_image = $old_image . "- Er nå slettet"; // Update DB with no new image $inputquery = "UPDATE `content_news` SET `news_priority`='$news_priority', `news_layout`='$layout', `news_menu_title`='$news_menu_title', `news_title`='$news_title', `news_time`='$news_time', `news_image`='$updated_image_name', `news_image_txt`='$updated_image_txt', `news_content`='$news_content', `date`='$date' WHERE `news_id`='$content_id'"; // Print UPDATE results $mysqli->query($inputquery); print "<br /><br /><br /><br /><br /><br /><br />\n"; print "$inputquery <br />linje 357\n"; echo "<p>Intet nytt bilde å laste opp</p>"; echo "<script>"; echo "alert(\"Gratulerer! Nyheten er naa endret. Gammelt bilde: $feedback_on_image \");"; echo "window.location = \"administrate.php?pid=$pid\";"; echo "</script>"; if($mysqli->connect_errno) { echo 'OBS! Klarte ikke å endre nyhet. Kontakt webmaster for hjelp. '; } } } else { // Set feedback message $feedback_on_image = 'Var ingen.'; } // Update DB with no image and image_txt $inputquery = "UPDATE `content_news` SET `news_priority`='$news_priority', `news_layout`='$layout', `news_menu_title`='$news_menu_title', `news_title`='$news_title', `news_time`='$news_time', `news_content`='$news_content', `date`='$date' WHERE `news_id`='$content_id'"; // Print UPDATE results $mysqli->query($inputquery); print "<br /><br /><br /><br /><br /><br /><br />\n"; print "$inputquery <br />linje 357\n"; echo "<p>Intet nytt bilde å laste opp</p>"; echo "<script>"; echo "alert(\"Gratulerer! Nyheten er naa endret. Gammelt bilde: $feedback_on_image \");"; echo "window.location = \"administrate.php?pid=$pid\";"; echo "</script>"; if($mysqli->connect_errno) { echo 'OBS! Klarte ikke å endre nyhet. Kontakt webmaster for hjelp. '; } } // New file uploaded - update DB with new file name if($old_image) { // Set file name to old image file name $feedback_on_image = $old_image . "- Er nå slettet"; } else { // Set file name to blank $feedback_on_image = 'Var ingen.'; } $inputquery = "UPDATE `content_news` SET `news_priority`='$news_priority', `news_layout`='$layout', `news_menu_title`='$news_menu_title', `news_title`='$news_title', `news_time`='$news_time', `news_image`='$image_name', `news_image_txt`='$news_image_txt', `news_content`='$news_content', `date`='$date' WHERE `news_id`='$content_id'"; // Print UPDATE results $mysqli->query($inputquery); print "<br /><br /><br /><br /><br /><br /><br />\n"; print "$inputquery <br />linje 360\n"; echo "<p>Nytt bilde ble lagret som " . $image_name . " til mappe: .</p>"; // Check if DB was updated if($mysqli->connect_errno) { echo 'Feil! Klarte ikke å oppdatere nyhet med nytt bilde i databasen. Kontakt webmaster. '; } else { // Pop-up-message echo "<script>"; echo "alert(\"Gratulerer! Nyheten er naa oppdatert med nytt bilde " . $image_name . "! Gammelt bilde: $feedback_on_image\");"; echo "window.location = \"administrate.php?pid=$pid\";"; echo "</script>"; } // END check if DB was updated update_data.php
  9. I try to use this code to update a table, what am I doing wrong, cuz it ain't working? (guessing the problem lays on the last two rows..) $inputquery = "UPDATE content_about "; $inputquery .= "(about_description, about_content01_tittle, about_content01, about_content02_tittle, about_content02, about_content03_tittle, about_content03, about_content04_tittle, about_content04, about_content05_tittle, about_content05, about_content06_tittle, about_content06, about_content07_tittle, about_content07, about_content08_tittle, about_content08, about_content09_tittle, about_content09, about_content10_tittle, about_content10, about_content11_tittle, about_content11, about_content12_tittle, about_content12, date) "; $inputquery .= " VALUES "; $inputquery .= "('$about_description', '$about_content01_tittle', '$about_content01', '$about_content02_tittle', '$about_content02', '$about_content03_tittle', '$about_content03', '$about_content04_tittle', '$about_content04', '$about_content05_tittle', '$about_content05', '$about_content06_tittle', '$about_content06', '$about_content07_tittle', '$about_content07', '$about_content08_tittle', '$about_content08', '$about_content09_tittle', '$about_content09', '$about_content10_tittle', '$about_content10', '$about_content11_tittle', '$about_content11', '$about_content12_tittle', '$about_content12', '$date') "; $inputquery .= " WHERE "; $inputquery .= "(about_id = '$content_id')"; Thanks!
  10. It's a page consisting of multiple sections of script, so I took out what I though was important. Here is the section that handles the data from the form: // Get data from form $content_id = $_POST["content_id"]; $layout = $_POST["fpage_layout"]; $fpage_title = $_POST["fpage_title"]; $myFile = $_FILES['myFile']; $old_image = $_POST['old_image']; $fpage_content = $_POST["fpage_content"]; $date = $_POST["date"]; // Define options for deleting image file $table = "content_frontpage"; $image = 'fpage_image'; define("UPLOAD_DIR", "../images/fpage/"); $imageToDelete = $r["fpage_image"]; $DirToDeleteIn = '../images/fpage/'; // END Check if file was uploaded
  11. <table width="80%" border="0" cellspacing="0" cellpadding="0"> <tr> <td align="center" width="20%"><img src="graphics/layout_1_left.png" alt="" width="56" height="56"><br /> <input type="radio" id="imgOptFlag1" name="fpage_layout" value="1" <?PHP if ($fpage_layout == 1) { echo ' checked="CHECKED"' ; } ?> onclick="toggleImgOpt(event)"></td> <td align="center" width="20%"><img src="graphics/layout_2_right.png" alt="" width="56" height="56"><br /> <input type="radio" id="imgOptFlag1" name="fpage_layout" value="2" <?PHP if ($fpage_layout == 2) { echo ' checked="CHECKED"' ; } ?> onclick="toggleImgOpt(event)"></td> <td align="center" width="20%"><img src="graphics/layout_3_center.png" alt="" width="56" height="56"><br /> <input type="radio" id="imgOptFlag1" name="fpage_layout" value="3" <?PHP if ($fpage_layout == 3) { echo ' checked="CHECKED"' ; } ?> onclick="toggleImgOpt(event)"></td> <td align="center" width="20%"><img src="graphics/layout_4_no_image.png" alt="" width="56" height="56"><br /> <input type="radio" id="imgOptFlag0" name="fpage_layout" value="4" <?PHP if ($fpage_layout == 4) { echo ' checked="CHECKED"' ; } ?> onclick="toggleImgOpt(event)"></td> </tr> <tr> <td align="center">Bilde til venstre</td> <td align="center">Bilde til høyre</td> <td align="center">Bilde i senter</td> <td align="center">Ingen bilde</td> </tr> </table> <p> <?PHP if ($fpage_image_old) { echo " Following image is already uploaded:<br> <table> <tr> <td> <table width='100' border='0' cellspacing='2' cellpadding='2'> <tr> <td align='left' valign='top' class='credits1'><img src='../images/fpage/$fpage_image_old' width='100'> </td> <tr> </table> Name of image file: $fpage_image_old </td> <tr> </table> <input name='old_image' type='hidden' value='$fpage_image_old'> " ; } else { echo "No image is uploaded! " ; } ?> </p> <?PHP if($fpage_layout = 1 || 2 || 3) { ?> <div id="imageUploadOption" style="display:none; margin-left:20px"> <p> Change image:<br> <input name="myFile" type="file"> </p> </div> <?PHP } ?> That was the important part from the form. JS is not shown, but you see where it applies. Here are the section from the processing script that the error occures in. // Check if file was uploaded if (is_uploaded_file($_FILES['myFile']['tmp_name'])) { $fileCheck = "1"; } else { $fileCheck = "2"; } // Check if old image if($old_image) { // Check if it needs to be deleted $checkVars2 = array(4, null); if(in_array($layout, $checkVars2)) { // Delete image if (!unlink($DirToDeleteIn.$old_image)) { // If it failed to delete echo ("OBS! Klarte ikke slette $DirToDeleteIn$old_image , ERROR line 241"); // Pop-up-message echo "<script>"; echo "alert(\"Advarsel! Innholdet ble IKKE oppdatert fordi tilhoereden fil $DirToDeleteIn$imageToDelete heller ikke kunne slettes. Vennlisgt kontakt webmaster for hjelp!\");"; echo "window.location = \"administrate.php?pid=$pid\";"; echo "</script>"; exit; } else { // If file was deleted, show message echo ("Deleted $DirToDeleteIn$old_image"); } } } else { // If there are no old file, check if layout is set wrong. $checkVars3 = array(1, 2, 3); if(in_array($layout, $checkVars3)) { // If layout is set to 1, 2 or 3, check if this is correct by checking if new file is present if($fileCheck == 2) { // No new file was added, layout was set incorectly, fix it. $layout = 4; echo "New layout was set to $layout"; } } } Its the if($old_image) that sometimes works and sometimes fails. When it fails it changes the layout value to 4 and thinks there are no old image present. I'm sure there are better way to do this, but this is as far as my PHP knowledge reaches. Thanks!
  12. Sorry, I can see that. I'll try simplify it before I post it. Thanks
  13. I have a form that gets a image name from a table if present, if not certain checks should be done when the form is passed. I have three options for placement of the image on the page (1, 2 and 3), and the option not to have an image (4). If a user already have an image and pick not to have one, the old file should be deleted. If the user just wish to change the placement of the image on the page, the old image is kept, but the layout value is updated in the DB. The problem is now that when I wish to only change the placement of the image, it sometimes work and sometimes don't. It seems like the name of the old image not always is passed, and it works and fails on all three options. The check on the passed form is done depending on the value of the imagename. If($imagename) then do so and so, if not, do so and so. Seems like the if($filename) sometimes fails to be passed from the form so that the form thing the placemnt value should be 4 since no image is present, event when it is. Any idea what this could be? (In the form I have a JS run on the radio button options of the placement, to hide the option to upload a new image or not. If 4 (no image is checked) the option to pick a file to upload is hidden, 1, 2 or 3 = show.) Thanks
  14. What is the best way to clear a value in a table? Scenario: If user choose to delete article image, the filename in the table should be cleared. I have used $old_image = ""; to clear the name and then update table, but that doesn't seem to clear the value completely, as when I do a check afterwards if filename was deleted by using statement if($filename), it always return True, as in, there is a value present for $filename even when name has been cleared. Have a got this correct or is there something else I am missing, In advance, thanks!
×
×
  • 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.