sandbudd Posted November 22, 2009 Share Posted November 22, 2009 Hey guys having a brain fart here... Here is what I have. I have a script that updates the database and works great and I have a script that uploads a file to a folder and works great but I am having troubles combining the two? Please help.. code to update database <?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=$_GET['id']; $header=$_POST['header']; $header2=$_POST['header2']; $header3=$_POST['header3']; $footer=$_POST['footer']; $footer2=$_POST['footer2']; $footer3=$_POST['footer3']; // Do update statement. mysql_query("update ads set header='$header', header2='$header2', header3='$header3', footer='$footer', footer2='$footer2', footer3='$footer3' where id='$id'") or die(mysql_error()); header("location:display.php"); exit; } // ************* 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); // Close database connection. mysql_close(); ?> <!-- END OF PHP CODES AND START HTML TAGS --> <html> <script type="text/javascript"> <!-- function MM_goToURL() { //v3.0 var i, args=MM_goToURL.arguments; document.MM_returnValue = false; for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'"); } //--> </script> <body> <!-- set this form to POST method and target this form to itself ($PHP_SELF;)--> <form id="form1" name="form1" method="post" action="?id=<? echo $id; ?>&page=update"> <table width="707" border="1" cellpadding="3" cellspacing="0" class="appfields"> <tr> <td width="25%"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Header</span></td> <td width="25%"><input name="header" type="file" id="header" value="<? echo $row['header']; ?>" size="50" /></td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Header 2<br> </span></td> <td><input name="header2" type="text" id="header2" value="<? echo $row['header2']; ?>"/></td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Header 3<strong><br> </strong></span></td> <td><strong> <input name="header3" type="text" id="header3" value="<? echo $row['header3']; ?>"/> </strong></td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Footer<strong><br> </strong></span></td> <td><strong> <input name="footer" type="text" id="footer" value="<? echo $row['footer']; ?>"/> </strong></td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Footer 2<br> </span></td> <td><strong> <input name="footer2" type="text" id="footer2" value="<? echo $row['footer2']; ?>"/> </strong></td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Footer 3<br> </span></td> <td><strong> <input name="footer3" type="text" id="footer3" value="<? echo $row['footer3']; ?>"/> </strong></td> </tr> </table> <br> <br> <table width="707" border="0" cellspacing="0" cellpadding="0"> <tr> <td><input type="submit" name="Submit" value="Submit" /></td> <td><div align="right"> <input name="Cancel" type="submit" id="Cancel" onClick="MM_goToURL('parent','display.php');return document.MM_returnValue" value="Cancel"> </div></td> </tr> </table> </form> </body> </html> uploads a file <?php //This is the directory where images will be saved $target = 'images/'.$_FILES['header']['name']; //This gets all the other information from the form $header= $_FILES['header']['name']; //Writes the photo to the server if(move_uploaded_file($_FILES['header']['tmp_name'], $target)) ?> need help combining these codes please. Quote Link to comment Share on other sites More sharing options...
sandbudd Posted November 22, 2009 Author Share Posted November 22, 2009 basically in short I need that when the update is done a file can be loaded to a file...does not have to be that exact code...thanks again in advance. Quote Link to comment Share on other sites More sharing options...
sandbudd Posted November 22, 2009 Author Share Posted November 22, 2009 friendly bump guys Quote Link to comment Share on other sites More sharing options...
sandbudd Posted November 22, 2009 Author Share Posted November 22, 2009 any help guys? Quote Link to comment Share on other sites More sharing options...
sandbudd Posted November 22, 2009 Author Share Posted November 22, 2009 anyone have any ideas? Quote Link to comment Share on other sites More sharing options...
emopoops Posted November 22, 2009 Share Posted November 22, 2009 i dont understand what u are doing. just add the code at the bottom. how do u need it to be combined? Quote Link to comment Share on other sites More sharing options...
sandbudd Posted November 22, 2009 Author Share Posted November 22, 2009 hi emopoops I tried that but it does not work...what I am trying to do is that a file will upload to the server. Quote Link to comment Share on other sites More sharing options...
emopoops Posted November 23, 2009 Share Posted November 23, 2009 u mean u need the script to work? it doesnt work? Quote Link to comment Share on other sites More sharing options...
sandbudd Posted November 23, 2009 Author Share Posted November 23, 2009 yes emopoops it is not loading the file to the server Quote Link to comment Share on other sites More sharing options...
mattal999 Posted November 23, 2009 Share Posted November 23, 2009 To make a long story short: Put the code for the upload into the part which handles the form posting. But you also need to set the form to the right format as well. See code below: <?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=$_GET['id']; $header=$_POST['header']; $header2=$_POST['header2']; $header3=$_POST['header3']; $footer=$_POST['footer']; $footer2=$_POST['footer2']; $footer3=$_POST['footer3']; // Do update statement. mysql_query("update ads set header='$header', header2='$header2', header3='$header3', footer='$footer', footer2='$footer2', footer3='$footer3' where id='$id'") or die(mysql_error()); //This is the directory where images will be saved $target = 'images/'.$_FILES['header']['name']; //This gets all the other information from the form $header= $_FILES['header']['name']; //Writes the photo to the server move_uploaded_file($header, $target); header("location:display.php"); exit; } // ************* 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); // Close database connection. mysql_close(); ?> <!-- END OF PHP CODES AND START HTML TAGS --> <html> <script type="text/javascript"> <!-- function MM_goToURL() { //v3.0 var i, args=MM_goToURL.arguments; document.MM_returnValue = false; for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'"); } //--> </script> <body> <!-- set this form to POST method and target this form to itself ($PHP_SELF;)--> <form id="form1" name="form1" method="post" action="?id=<? echo $id; ?>&page=update" enctype="multipart/form-data"> <table width="707" border="1" cellpadding="3" cellspacing="0" class="appfields"> <tr> <td width="25%"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Header</span></td> <td width="25%"><input name="header" type="file" id="header" value="<? echo $row['header']; ?>" size="50" /></td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Header 2<br> </span></td> <td><input name="header2" type="text" id="header2" value="<? echo $row['header2']; ?>"/></td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Header 3<strong><br> </strong></span></td> <td><strong> <input name="header3" type="text" id="header3" value="<? echo $row['header3']; ?>"/> </strong></td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Footer<strong><br> </strong></span></td> <td><strong> <input name="footer" type="text" id="footer" value="<? echo $row['footer']; ?>"/> </strong></td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Footer 2<br> </span></td> <td><strong> <input name="footer2" type="text" id="footer2" value="<? echo $row['footer2']; ?>"/> </strong></td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Footer 3<br> </span></td> <td><strong> <input name="footer3" type="text" id="footer3" value="<? echo $row['footer3']; ?>"/> </strong></td> </tr> </table> <br> <br> <table width="707" border="0" cellspacing="0" cellpadding="0"> <tr> <td><input type="submit" name="Submit" value="Submit" /></td> <td><div align="right"> <input name="Cancel" type="submit" id="Cancel" onClick="MM_goToURL('parent','display.php');return document.MM_returnValue" value="Cancel"> </div></td> </tr> </table> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
sandbudd Posted November 23, 2009 Author Share Posted November 23, 2009 mattal999 the code still doesn't upload the file and now doesn't populate the db? Quote Link to comment Share on other sites More sharing options...
sandbudd Posted November 23, 2009 Author Share Posted November 23, 2009 I'm getting this error Notice: Undefined index: page in /update_display.php on line 18 here is line 18 if($_GET['page'] == "update") Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted November 23, 2009 Share Posted November 23, 2009 several things here. consider using <?php instead of short tags <? .. one day your server will have updated php.ini to allow for only long tags and you will be right back here. // ***** This part will process when you Click on "Submit" button ***** // Check, if you clicked "Submit" button if($_GET['page'] == "update") 1. your code will process NOT when the submit button has been pushed, but when it reads ?page=update from the URL. it's not doing what you think it's doing. 2. why not just scrap GET altogether .. no reason to mix the two (GET && POST) in this situation. you can accomplish the same thing by just adding a hidden field with the user ID: <?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'])) { if (!empty ($_POST['header'])) { if (is_uploaded_file ($_FILES['header']['tmp_name'])) { //This is the directory where images will be saved $target = 'images/'.$_FILES['header']['name']; //This gets all the other information from the form $header = $_FILES['header']['name']; //Writes the photo to the server move_uploaded_file ($_FILES['header']['tmp_name'], $target) or die ('File not moved.'); } else { die ('File not uploaded.'; } } $_POST = array_map ('mysql_real_escape_string', $_POST); $id = $_POST['id']; $header = $_POST['header']; $header2 = $_POST['header2']; $header3 = $_POST['header3']; $footer = $_POST['footer']; $footer2 = $_POST['footer2']; $footer3 = $_POST['footer3']; // Do update statement. $sql = mysql_query(" update `ads` set `header`='{$header}', `header2`='{$header2}', `header3`='{$header3}', `footer`='{$footer}', `footer2`='{$footer2}', `footer3`='{$footer3}' where `id`='{$id}' ") or trigger_error (mysql_error()); if ($sql) { header("location:display.php"); exit (0); } } else { // ************* End update part ************* // *** Select data to show on text fields in form. *** // Get id parameter (GET method) $id = ((isset ($_GET['id']) && (ctype_digit ($_GET['id']))) ? mysql_real_escape_string ($_GET['id']) : ''); if (!empty ($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_array ($result); // Close database connection. mysql_close(); } ?> <!-- END OF PHP CODES AND START HTML TAGS --> <html> <script type="text/javascript"> <!-- function MM_goToURL() { //v3.0 var i, args=MM_goToURL.arguments; document.MM_returnValue = false; for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'"); } //--> </script> <body> <!-- set this form to POST method and target this form to itself ($PHP_SELF;)--> <form id="form1" name="form1" method="post" action=""> <input type="hidden" name="id" value="<?php echo $id; ?>" /> <table width="707" border="1" cellpadding="3" cellspacing="0" class="appfields"> <tr> <td width="25%"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Header</span></td> <td width="25%"><input name="header" type="file" id="header" value="<?php echo $row['header']; ?>" size="50" /></td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Header 2<br> </span></td> <td><input name="header2" type="text" id="header2" value="<?php echo $row['header2']; ?>"/></td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Header 3<strong><br> </strong></span></td> <td><strong> <input name="header3" type="text" id="header3" value="<?php echo $row['header3']; ?>"/> </strong></td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Footer<strong><br> </strong></span></td> <td><strong> <input name="footer" type="text" id="footer" value="<?php echo $row['footer']; ?>"/> </strong></td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Footer 2<br> </span></td> <td><strong> <input name="footer2" type="text" id="footer2" value="<?php echo $row['footer2']; ?>"/> </strong></td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Footer 3<br> </span></td> <td><strong> <input name="footer3" type="text" id="footer3" value="<?php echo $row['footer3']; ?>"/> </strong></td> </tr> </table> <br> <br> <table width="707" border="0" cellspacing="0" cellpadding="0"> <tr> <td><input type="submit" name="Submit" value="Submit" /></td> <td><div align="right"> <input name="Cancel" type="submit" id="Cancel" onClick="MM_goToURL('parent','display.php');return document.MM_returnValue" value="Cancel"> </div></td> </tr> </table> </form> </body> </html> <?php } ?> Quote Link to comment Share on other sites More sharing options...
sandbudd Posted November 23, 2009 Author Share Posted November 23, 2009 mrMarcus now I get a blank page with no error? Quote Link to comment Share on other sites More sharing options...
sandbudd Posted November 23, 2009 Author Share Posted November 23, 2009 any ideas? Quote Link to comment Share on other sites More sharing options...
sandbudd Posted November 23, 2009 Author Share Posted November 23, 2009 here is the code that I am using and getting this error? Notice: Undefined index: header in /update_display.php on line 24 File not uploaded. here is line 24 { if (is_uploaded_file ($_FILES['header']['tmp_name'])) { <?php error_reporting(E_ALL); ini_set('display_errors', '1'); ?> <?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'])) { if (!empty ($_POST['header'])) { if (is_uploaded_file ($_FILES['header']['tmp_name'])) { //This is the directory where images will be saved $target = 'images/'.$_FILES['header']['name']; //This gets all the other information from the form $header = $_FILES['header']['name']; //Writes the photo to the server move_uploaded_file ($_FILES['header']['tmp_name'], $target) or die ('File not moved.'); } else die ('File not uploaded.'); } $_POST = array_map ('mysql_real_escape_string', $_POST); $id = $_POST['id']; $header = $_POST['header']; $header2 = $_POST['header2']; $header3 = $_POST['header3']; $footer = $_POST['footer']; $footer2 = $_POST['footer2']; $footer3 = $_POST['footer3']; // Do update statement. $sql = mysql_query(" update `ads` set `header`='{$header}', `header2`='{$header2}', `header3`='{$header3}', `footer`='{$footer}', `footer2`='{$footer2}', `footer3`='{$footer3}' where `id`='{$id}' ") or trigger_error (mysql_error()); if ($sql) { header("location:display.php"); exit (0); } } else { // ************* End update part ************* // *** Select data to show on text fields in form. *** // Get id parameter (GET method) $id = ((isset ($_GET['id']) && (ctype_digit ($_GET['id']))) ? mysql_real_escape_string ($_GET['id']) : ''); if (!empty ($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_array ($result); // Close database connection. mysql_close(); } ?> <!-- END OF PHP CODES AND START HTML TAGS --> <html> <script type="text/javascript"> <!-- function MM_goToURL() { //v3.0 var i, args=MM_goToURL.arguments; document.MM_returnValue = false; for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'"); } //--> </script> <body> <!-- set this form to POST method and target this form to itself ($PHP_SELF;)--> <form id="form1" name="form1" method="post" action=""> <input type="hidden" name="id" value="<?php echo $id; ?>" /> <input name="header" type="file" id="header" value="<?php echo $row['header']; ?>" size="50" /> <table width="707" border="1" cellpadding="3" cellspacing="0" class="appfields"> <tr> <td width="25%"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Header</span></td> <td width="25%"> </td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Header 2<br> </span></td> <td><input name="header2" type="text" id="header2" value="<?php echo $row['header2']; ?>"/></td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Header 3<strong><br> </strong></span></td> <td><strong> <input name="header3" type="text" id="header3" value="<?php echo $row['header3']; ?>"/> </strong></td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Footer<strong><br> </strong></span></td> <td><strong> <input name="footer" type="text" id="footer" value="<?php echo $row['footer']; ?>"/> </strong></td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Footer 2<br> </span></td> <td><strong> <input name="footer2" type="text" id="footer2" value="<?php echo $row['footer2']; ?>"/> </strong></td> </tr> <tr> <td><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14">Footer 3<br> </span></td> <td><strong> <input name="footer3" type="text" id="footer3" value="<?php echo $row['footer3']; ?>"/> </strong></td> </tr> </table> <br> <br> <table width="707" border="0" cellspacing="0" cellpadding="0"> <tr> <td><input type="submit" name="Submit" value="Submit" /></td> <td><div align="right"> <input name="Cancel" type="submit" id="Cancel" onClick="MM_goToURL('parent','display.php');return document.MM_returnValue" value="Cancel"> </div></td> </tr> </table> </form> </body> </html> <?php } ?> Quote Link to comment Share on other sites More sharing options...
mattal999 Posted November 23, 2009 Share Posted November 23, 2009 Have you CHMODded the images/ directory to 0777? Quote Link to comment Share on other sites More sharing options...
sandbudd Posted November 23, 2009 Author Share Posted November 23, 2009 it is set to 0755 Quote Link to comment Share on other sites More sharing options...
sandbudd Posted November 23, 2009 Author Share Posted November 23, 2009 dang guys I've gone through this code line by line and can not find there error? Quote Link to comment Share on other sites More sharing options...
sandbudd Posted November 23, 2009 Author Share Posted November 23, 2009 friendly bump Quote Link to comment Share on other sites More sharing options...
sandbudd Posted November 23, 2009 Author Share Posted November 23, 2009 can anyone figure this out...lol Quote Link to comment 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.