sandbudd Posted October 28, 2010 Share Posted October 28, 2010 This is to upload an image then write the link to the database. It does upload the image but does not write the link to the database. Any help would be great... <?php // Connect database. // ***** This part will process when you Click on "Submit" button ***** // Check, if you clicked "Submit" button if($_GET['page'] == "update") { $id=$_GET['id']; $url=$_POST['url']; $title=$_POST['title']; $photo=$_FILES['photo']['name']; // < --- this line // Do update statement. mysql_query("update ads set photo='$photo', url='$url', title='$title' where id='$id'") or die(mysql_error()); } // ************* End update part ************* // *** Select data to show on text fields in form. *** // Get id parameter (GET method) $id=$_GET['id']; // Get records in all columns from table where column id equal in $id and put it in $result. $result=mysql_query("select * from ads where id='$id'"); // Split records in $result by table rows and put them in $row. $row=mysql_fetch_assoc($result); ?> <?php // Close database connection. mysql_close(); ?> <?php //This is the directory where images will be saved $target = "http://www.entertainfortwayne.com/images/ads/"; $target = $target . basename( $_FILES['photo']['name']); //This gets all the other information from the form $pic=($_FILES['photo']['name']); //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) ?> <!-- END OF PHP CODES AND START HTML TAGS --> <html> <style type="text/css"> <!-- .style2 {font-size: 12px} --> </style> <body> <br> <br> <!-- set this form to POST method and target this form to itself ($PHP_SELF;)--> <form enctype="multipart/form-data" id="form1" name="form1" method="post" action="?id=<? echo $id; ?>&page=update"> <table width="707" border="0" cellpadding="3" cellspacing="0" class="appfields"> <tr> <td><p> </p> <p><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14"><strong><?php echo "<img src=http://www.entertainfortwayne.com/images/ads/".$row['photo'] ."> "; ?></strong></span></p> <p> <label><span class="style2">ALT/TITLE</span> <input type="text" name="title" id="title" value="<?php echo $row['title'] ?>"> </label> </p> <p><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14"><strong> <label> </label> </strong><span class="style2">HTTP://</span><strong> <label> <input type="text" name="url" id="url" Value="<?php echo $row['url'] ?>"> </label> <br> <br> </strong></span><strong> <input type="file" name="photo" /> </strong> <input type="submit" name="Submit" value="Submit" /> Image needs to be 200px wide</p></td> </tr> </table> <p> </p> <p> </p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/217140-thaought-we-had-resolved-but-still-not-working/ Share on other sites More sharing options...
sandbudd Posted October 28, 2010 Author Share Posted October 28, 2010 sorry writes the link does not upload the image Quote Link to comment https://forums.phpfreaks.com/topic/217140-thaought-we-had-resolved-but-still-not-working/#findComment-1127740 Share on other sites More sharing options...
sandbudd Posted October 28, 2010 Author Share Posted October 28, 2010 I have gone through this thing and cant find the error...too close to the problem Quote Link to comment https://forums.phpfreaks.com/topic/217140-thaought-we-had-resolved-but-still-not-working/#findComment-1127749 Share on other sites More sharing options...
Andy-H Posted October 28, 2010 Share Posted October 28, 2010 I wrote a class for file uploads, you can use that if you like and just add the database update. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/217140-thaought-we-had-resolved-but-still-not-working/#findComment-1127755 Share on other sites More sharing options...
Andy-H Posted October 28, 2010 Share Posted October 28, 2010 Updated them. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/217140-thaought-we-had-resolved-but-still-not-working/#findComment-1127767 Share on other sites More sharing options...
akitchin Posted October 28, 2010 Share Posted October 28, 2010 i would guess the issue with your original code is that move_uploaded_file cannot use URL wrappers in the location. try changing your target to use the filesystem's directory location. Quote Link to comment https://forums.phpfreaks.com/topic/217140-thaought-we-had-resolved-but-still-not-working/#findComment-1127782 Share on other sites More sharing options...
sandbudd Posted November 1, 2010 Author Share Posted November 1, 2010 changed it to this is it is really weird but what happens say I upload an image called cans.jpg .... it does not put it in the image folder but in the root and is displayed as imagescans.jpg it does write it to the database correctly. <?php if($_GET['page'] == "update") { $id=$_GET['id']; $url=$_POST['url']; $title=$_POST['title']; $photo=$_FILES['photo']['name']; // < --- this line // Do update statement. mysql_query("update ads set photo='$photo', url='$url', title='$title' where id='$id'") or die(mysql_error()); } // ************* End update part ************* // *** Select data to show on text fields in form. *** // Get id parameter (GET method) $id=$_GET['id']; // Get records in all columns from table where column id equal in $id and put it in $result. $result=mysql_query("select * from ads where id='$id'"); // Split records in $result by table rows and put them in $row. $row=mysql_fetch_assoc($result); ?> <?php // Close database connection. mysql_close(); ?> <?php //This is the directory where images will be saved $target = "images"; $target = $target . basename( $_FILES['photo']['name']); //This gets all the other information from the form $pic=($_FILES['photo']['name']); //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) ?> Quote Link to comment https://forums.phpfreaks.com/topic/217140-thaought-we-had-resolved-but-still-not-working/#findComment-1129154 Share on other sites More sharing options...
Andy-H Posted November 1, 2010 Share Posted November 1, 2010 <?php //connect to database if($_GET['page'] == "update") { $id = (int)$_GET['id']; $title = $_POST['title']; $photo = $_FILES['photo']['name']; //This is the directory where images will be saved $target = "images"; $target = $target . DIRECTORY_SEPARATOR . $photo; $urlTarget = $target . '/' . $photo; //move file from temp location to target location if( move_uploaded_file($_FILES['photo']['tmp_name'], $target) ) { $url = 'http://' . $_SERVER['SERVER_NAME'] . $urlTarget; // Do update statement. mysql_query("update ads set photo='$photo', url='$url', title='" . mysql_real_escape_string($title) . "' where id=$id") or die(mysql_error()); } } // Close database connection. mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/217140-thaought-we-had-resolved-but-still-not-working/#findComment-1129163 Share on other sites More sharing options...
sandbudd Posted November 1, 2010 Author Share Posted November 1, 2010 Hi Andy-H now all I get is a blank page? Quote Link to comment https://forums.phpfreaks.com/topic/217140-thaought-we-had-resolved-but-still-not-working/#findComment-1129164 Share on other sites More sharing options...
Andy-H Posted November 1, 2010 Share Posted November 1, 2010 I need you to post the FULL code of the page, I don't really understand what you're trying to do atm but I know that what you posted is only a part of the code. I can't understand why your upload logic is spread throught various code blocks. Quote Link to comment https://forums.phpfreaks.com/topic/217140-thaought-we-had-resolved-but-still-not-working/#findComment-1129166 Share on other sites More sharing options...
sandbudd Posted November 1, 2010 Author Share Posted November 1, 2010 Andy-H I am simply trying to write an upload script that uploads the image to the folder images and then updates the database with the name of the image... Here is the complete script I have now. <?php // Connect database. $host=""; // Host name. $db_user=""; // MySQL username. $db_password=""; // MySQL password. $database=""; // Database name. mysql_connect($host,$db_user,$db_password); mysql_select_db($database); // ***** This part will process when you Click on "Submit" button ***** // Check, if you clicked "Submit" button if($_GET['page'] == "update") { $id = (int)$_GET['id']; $title = $_POST['title']; $photo = $_FILES['photo']['name']; //This is the directory where images will be saved $target = "images"; $target = $target . DIRECTORY_SEPARATOR . $photo; $urlTarget = $target . '/' . $photo; //move file from temp location to target location if( move_uploaded_file($_FILES['photo']['tmp_name'], $target) ) { $url = 'http://' . $_SERVER['SERVER_NAME'] . $urlTarget; // Do update statement. mysql_query("update ads set photo='$photo', url='$url', title='" . mysql_real_escape_string($title) . "' where id=$id") or die(mysql_error()); } // Close database connection. mysql_close(); ?> <!-- END OF PHP CODES AND START HTML TAGS --> <html> <style type="text/css"> <!-- .style2 {font-size: 12px} --> </style> <body> <br> <br> <!-- set this form to POST method and target this form to itself ($PHP_SELF;)--> <form enctype="multipart/form-data" id="form1" name="form1" method="post" action="?id=<? echo $id; ?>&page=update"> <table width="707" border="0" cellpadding="3" cellspacing="0" class="appfields"> <tr> <td><p> </p> <p><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14"><strong><?php echo "<img src=images".$row['photo'] ."> "; ?></strong></span></p> <p> <label><span class="style2">ALT/TITLE</span> <input type="text" name="title" id="title" value="<?php echo $row['title'] ?>"> </label> </p> <p><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14"><strong> <label> </label> </strong><span class="style2">HTTP://</span><strong> <label> <input type="text" name="url" id="url" Value="<?php echo $row['url'] ?>"> </label> <br> <br> </strong></span><strong> <input type="file" name="photo" /> </strong> <input type="submit" name="Submit" value="Submit" /> Image needs to be 200px wide</p></td> </tr> </table> <p> </p> <p> </p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/217140-thaought-we-had-resolved-but-still-not-working/#findComment-1129168 Share on other sites More sharing options...
Andy-H Posted November 1, 2010 Share Posted November 1, 2010 I don't understand, if it's an upload page, why are you pulling info from your database prior to the upload? If it's a page to update the details stored about an existing photo, why is there an upload box? Your logic seems flawed, explain exactly what it is supposed to do? Quote Link to comment https://forums.phpfreaks.com/topic/217140-thaought-we-had-resolved-but-still-not-working/#findComment-1129171 Share on other sites More sharing options...
sandbudd Posted November 1, 2010 Author Share Posted November 1, 2010 Andy-H it pulls the information of the db row and displays it in a form...then it can be changed including the image and updated... Quote Link to comment https://forums.phpfreaks.com/topic/217140-thaought-we-had-resolved-but-still-not-working/#findComment-1129179 Share on other sites More sharing options...
Andy-H Posted November 1, 2010 Share Posted November 1, 2010 <?php // Connect database. $host=""; // Host name. $db_user=""; // MySQL username. $db_password=""; // MySQL password. $database=""; // Database name. mysql_connect($host,$db_user,$db_password); mysql_select_db($database); // ***** This part will process when you Click on "Submit" button ***** // Check, if you clicked "Submit" button if( isset($_POST['submit']) ) { $id = (int)$_GET['id']; $title = $_POST['title']; $photo = basename($_FILES['photo']['name']); $photo = explode('.', $photo); $photo = implode(uniqid() . '.', $photo); //This is the directory where images will be saved $target = 'images'; $target = $target . DIRECTORY_SEPARATOR . $photo; $urlTarget = $target . '/' . $photo; //move file from temp location to target location if( move_uploaded_file($_FILES['photo']['tmp_name'], $target) ) { $url = 'http://' . $_SERVER['SERVER_NAME'] . '/' . $urlTarget; // Do update statement. mysql_query("update ads set photo='$photo', url='$url', title='" . mysql_real_escape_string($title) . "' where id=$id") or die(mysql_error()); } $query = "SELECT url, title FROM ads WHERE id = $id"; $result = mysql_query($query)or trigger_error(mysql_error()); $row = mysql_fetch_row($result); // Close database connection. mysql_close(); ?> <!-- END OF PHP CODES AND START HTML TAGS --> <html> <style type="text/css"> <!-- .style2 {font-size: 12px} --> </style> <body> <br> <br> <!-- set this form to POST method and target this form to itself ($PHP_SELF;)--> <form enctype="multipart/form-data" id="form1" name="form1" method="post" action="?id=<?php echo (int)$id; ?>"> <table width="707" border="0" cellpadding="3" cellspacing="0" class="appfields"> <tr> <td><p> </p> <p><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14"<img src="<?php echo $row[0]; ?>" alt="<?php echo stripslashes(htmlentities($row[1], ENT_QUOTES)); ?>" title="<?php echo stripslashes(htmlentities($row[1], ENT_QUOTES)); ?>" /></span></p> <p> <span class="style2"><label for="title">ALT/TITLE</label></span> <input type="text" name="title" id="title" value="<?php echo stripslashes(htmlentities($row[1], ENT_QUOTES)); ?>"> </p> <br> <br> <strong><label for="img">Choose a photo</label></strong> <input type="file" name="photo" id="img" /> </strong> <input type="submit" name="submit" value="Submit" /> Image needs to be 200px wide</p></td> </tr> </table> <p> </p> <p> </p> </form> </body> </html> That what you wanted? //Wouldn't allow people to change the url, just have it happen on upload. //Where does the $id variable get defined in the first place? Quote Link to comment https://forums.phpfreaks.com/topic/217140-thaought-we-had-resolved-but-still-not-working/#findComment-1129185 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.