Jony Rato Posted February 3, 2016 at 02:26 PM Report Share #593045 Posted February 3, 2016 at 02:26 PM Boas pessoal Estou a tentar inserir uns dados na base de dados, só que o código não está a funcionar <?php require('database/database_connection.php'); if (isset($_POST['name']) && isset($_FILES['image']) && $_FILES['image']['size'] > 0) { $name = $_POST['name']; // Temporary file name stored on the server $tmpName = $_FILES['image']['tmp_name']; // Read the file $fp = fopen($tmpName, 'r'); $data = fread($fp, filesize($tmpName)); $data = addslashes($data); fclose($fp); $query2 = mysql_query("SELECT name FROM recipes WHERE name='$name'"); if (mysql_num_rows($query2) > 0) { echo "<script language=\"Javascript\">\n"; echo "alert('Category already exists!');\n"; echo "window.location='add_recipes.php'"; echo "</script>"; } else { $sql = "INSERT INTO recipes (name, image) VALUES ('$name','$data')"; $query = mysql_query($sql); if (($query) != 1) { echo "<script language=\"Javascript\">\n"; echo "alert('Something went wrong. Please try again!');\n"; echo "window.location='add_recipe.php'"; echo "</script>"; } else { echo "<script language=\"Javascript\">\n"; echo "alert('Category added with success!');\n"; echo "window.location='user_recipes.php'"; echo "</script>"; } } } ?> Só que eu tenho este código (só com alguns nomes alterados) para outra parte do site e está a funcionar Link to comment Share on other sites More sharing options...
tiago.f Posted February 3, 2016 at 02:33 PM Report Share #593047 Posted February 3, 2016 at 02:33 PM olá, define melhor "não está a funcionar" sff. Dá erro? Entra dentro de algum if que não devia? .... Link to comment Share on other sites More sharing options...
Jony Rato Posted February 3, 2016 at 02:40 PM Author Report Share #593049 Posted February 3, 2016 at 02:40 PM olá, define melhor "não está a funcionar" sff. Dá erro? Entra dentro de algum if que não devia? .... Simplesmente não faz nada, clico no botão para criar e só faz refresh na página Link to comment Share on other sites More sharing options...
tiago.f Posted February 3, 2016 at 02:44 PM Report Share #593050 Posted February 3, 2016 at 02:44 PM podes mostar o codigo html? Link to comment Share on other sites More sharing options...
Jony Rato Posted February 3, 2016 at 02:46 PM Author Report Share #593051 Posted February 3, 2016 at 02:46 PM podes mostar o codigo html? <?php include('session.php');?> <!DOCTYPE html> <html lang="en"> <head> <title>Recipe Book</title> <meta charset="utf-8"> <meta name = "format-detection" content = "telephone=no" /> <link rel="icon" href="images/favicon.ico"> <link rel="shortcut icon" href="images/favicon.ico" /> <link rel="stylesheet" href="css/stuck.css"> <link rel="stylesheet" href="css/style.css"> <link rel="stylesheet" href="css/touchTouch.css"> <script src="js/jquery.js"></script> <script src="js/jquery-migrate-1.1.1.js"></script> <script src="js/script.js"></script> <script src="js/superfish.js"></script> <script src="js/jquery.equalheights.js"></script> <script src="js/jquery.mobilemenu.js"></script> <script src="js/jquery.easing.1.3.js"></script> <script src="js/tmStickUp.js"></script> <script src="js/jquery.ui.totop.js"></script> <script src="js/touchTouch.jquery.js"></script> <!--[if lt IE 9]> <div style=' clear: both; text-align:center; position: relative;'> <a href="http://windows.microsoft.com/en-US/internet-explorer/products/ie/home?ocid=ie6_countdown_bannercode"> <img src="http://storage.ie6countdown.com/assets/100/images/banners/warning_bar_0000_us.jpg" border="0" height="42" width="820" alt="You are using an outdated browser. For a faster, safer browsing experience, upgrade for free today." /> </a> </div> <script src="js/html5shiv.js"></script> <link rel="stylesheet" media="screen" href="css/ie.css"> <![endif]--> </head> <body class="page1" id="top"> <!--============================== header =================================--> <header> <!--============================== Stuck menu =================================--> <section id="stuck_container"> <div class="container"> <div class="row"> <div class="grid_12"> <h1> <div class="bann_capt "> <div class="title">Recipe Book</div> </div> </h1> </div> </div> </div> </section> <div class="navigation"> <nav> <ul class="sf-menu"> <li><a href="index_user.php">Home</a></li> <li><a href="logout.php">Logout</a></li> </ul> </nav> <div class="clear"></div> </div> </header> <!--===================== Content ======================--> <?php require('database/database_connection.php'); if (isset($_POST['name']) && isset($_FILES['image']) && $_FILES['image']['size'] > 0) { $name = $_POST['name']; // Temporary file name stored on the server $tmpName = $_FILES['image']['tmp_name']; // Read the file $fp = fopen($tmpName, 'r'); $data = fread($fp, filesize($tmpName)); $data = addslashes($data); fclose($fp); $query2 = mysql_query("SELECT name FROM recipes WHERE name='$name'"); if (mysql_num_rows($query2) > 0) { echo "<script language=\"Javascript\">\n"; echo "alert('Category already exists!');\n"; echo "window.location='add_category.php'"; echo "</script>"; } else { $sql = "INSERT INTO recipes (name, image) VALUES ('$name','$data')"; $query = mysql_query($sql); if (($query) != 1) { echo "<script language=\"Javascript\">\n"; echo "alert('Something went wrong. Please try again!');\n"; echo "window.location='add_recipe.php'"; echo "</script>"; } else { echo "<script language=\"Javascript\">\n"; echo "alert('Category added with success!');\n"; echo "window.location='user_recipes.php'"; echo "</script>"; } } } ?> <script type="text/javascript"> function validate() { if(( document.recipeForm.name.value == "" )&&( document.recipeForm.description.value == "" )&&( document.recipeForm.ingredients.value == "" )&& ( document.recipeForm.preparation.value == "" )&&( document.recipeForm.image.value == "" )) { alert( "Please fill out all the fields!" ); document.recipeForm.name.focus() ; return false; } if( document.recipeForm.name.value == "" ) { alert( "Please fill the name!" ); document.recipeForm.name.focus() ; return false; } if( document.recipeForm.description.value == "" ) { alert( "Please fill out the description!" ); document.recipeForm.description.focus() ; return false; } if( document.recipeForm.ingredients.value == "" ) { alert( "Please fill out the ingredients!" ); document.recipeForm.ingredients.focus() ; return false; } if( document.recipeForm.preparation.value == "" ) { alert( "Please fill out the preparation!" ); document.recipeForm.preparation.focus() ; return false; } if( document.recipeForm.image.value == "" ) { alert( "Please choose a image!" ); document.recipeForm.image.focus() ; return false; } return( true ); } </script> <script src="jquery-1.8.0.min.js"></script> <section class="content"><div class="ic"></div> <div class="container"> <div class="row"> <div class="grid_4"> </br> </br> <table> <form name="recipeForm" action="add_recipe.php" onsubmit="return(validate());" method="post"> <div class="tmInput"> <tr><td><strong class="dt">Name</strong></td></tr> <tr><td><input name="name" id='name' type="text" size="50" >*</td></tr> </div> <div class="tmInput"> <tr><td><strong class="dt">Image</strong></td></tr> <input name="MAX_FILE_SIZE" value="4194304" type="hidden"> <tr><td><input name="image" type="file">*</td></tr> </div> </br> <tr><td>*Obligatory fields</td></tr> <div class="clear height1"></div> </div> <div class="clear"></div> <div class="ta__right"> <table> <td><button name="create" class="btn" type="submit">Create</a></td> <td><button class="btn1" type="reset">Clear</a></td> </table> </div> </form> </table> </div> </div> </div> </section> <section class="content"><div class="ic"></div> <div class="container"> </div> </section> <!--============================== footer =================================--> <footer id="footer"> <div class="container"> <div class="row"> <div class="grid_12"> <div class="copyright"><span class="brand">Recipe Book </span> © <span id="copyright-year"></span><div>Website designed by <a href="http://www.templatemonster.com/" rel="nofollow">TemplateMonster.com</a></div> </div> </div> </div> </div> </footer> </body> </html> Link to comment Share on other sites More sharing options...
tiago.f Posted February 3, 2016 at 03:04 PM Report Share #593052 Posted February 3, 2016 at 03:04 PM Assim à partida não vejo nada de muito estranho mas: - está página que mostras é a add_recipe.php ? - se o teu formulario faz submit para a mesma página, entao podes usar action="" em vez de action="add_recipe.php" - tens a certeza que a tua foto tem menos de 4194304 bytes ? - quando fazes submit ele devolve-te para a mesma página? ou vais parar a user_recipes.php ? é que tens echo "window.location='XXX.php'"; e isto vai redirecionar-te para outras paginas, fazendo com que não vejas o resultado. Se nada disto ajudar, aconselho-te a abrir o dev tools (F12 no chrome) e verificares a actividade de rede/network, bem como a consola. Link to comment Share on other sites More sharing options...
Jony Rato Posted February 3, 2016 at 03:10 PM Author Report Share #593053 Posted February 3, 2016 at 03:10 PM Assim à partida não vejo nada de muito estranho mas: - está página que mostras é a add_recipe.php ? - se o teu formulario faz submit para a mesma página, entao podes usar action="" em vez de action="add_recipe.php" - tens a certeza que a tua foto tem menos de 4194304 bytes ? - quando fazes submit ele devolve-te para a mesma página? ou vais parar a user_recipes.php ? é que tens echo "window.location='XXX.php'"; e isto vai redirecionar-te para outras paginas, fazendo com que não vejas o resultado. Se nada disto ajudar, aconselho-te a abrir o dev tools (F12 no chrome) e verificares a actividade de rede/network, bem como a consola. Em resposta às tuas perguntas - está página que mostras é a add_recipe.php ? Sim - tens a certeza que a tua foto tem menos de 4194304 bytes ? Sim - quando fazes submit ele devolve-te para a mesma página? ou vais parar a user_recipes.php ? é que tens echo "window.location='XXX.php'"; e isto vai redirecionar-te para outras paginas, fazendo com que não vejas o resultado. Fico na mesma página E obrigado pelas dicas Link to comment Share on other sites More sharing options...
tiago.f Posted February 3, 2016 at 03:20 PM Report Share #593054 Posted February 3, 2016 at 03:20 PM Acrescenta lá estes var_dumps: if (isset($_POST['name']) && isset($_FILES['image']) && $_FILES['image']['size'] > 0) { var_dump($_POST); var_dump($_FILES); e diz o que aparece depois de clicares em submit e ele fazer o refresh da página. Poe tambem enctype="multipart/form-data" no form <form name="recipeForm" action="add_recipe.php" enctype="multipart/form-data" onsubmit="return(validate());" method="post"> Link to comment Share on other sites More sharing options...
Jony Rato Posted February 3, 2016 at 03:34 PM Author Report Share #593056 Posted February 3, 2016 at 03:34 PM Já deu faltava o enctype="multipart/form-data" Muito obrigado 😁 1 Report Link to comment Share on other sites More sharing options...
chbcl Posted February 18, 2016 at 03:26 AM Report Share #593639 Posted February 18, 2016 at 03:26 AM Em 2016 ainda se vê mysql_query e não mysqli_query, isto não avança. developer @ filipealves.net filipealvesbcl [a] gmail.com github.com/filipealvesbcl Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now