magcr23 Posted July 10, 2015 Share Posted July 10, 2015 Hi guys, i have that code (pag1.php): if(isset($_POST['btnQuantos'])){ $quant = limpa($_POST["quantos"]); $quantidade = $quant; echo '<form action="add.php" method="POST" ><br/>'; echo '<input type="hidden" name="txtID" id="txtID" value="'.$ln1['id'].'">'; for($i = 0; $i < $quantidade; $i++){ echo 'Premio ' . ($i + 1) . ' '; echo '<input type="text" name="premio" Placeholder="Prémio" /> <br/><br/>'; } echo '<input type="submit" name="btnPremio" id="btnPremio" value="Adicionar"><br/>'; echo '</form>'; } and that (add.php): $id = limpa($_POST['txtID']); $premio = limpa($_POST['premio']); echo $id; echo '<br/>'; echo '$premio'; The problem is that when i do echo '$premio' it only echo the last input, let's image that the user chosed 10 textbox, it will only echo the the last one. How can i make it echo all of them? Quote Link to comment https://forums.phpfreaks.com/topic/297250-echo/ Share on other sites More sharing options...
fastsol Posted July 10, 2015 Share Posted July 10, 2015 You need to make the input an array name like this name="premio[]". Then use a foreach loop in the processing part to echo each value. Quote Link to comment https://forums.phpfreaks.com/topic/297250-echo/#findComment-1516043 Share on other sites More sharing options...
Ch0cu3r Posted July 10, 2015 Share Posted July 10, 2015 You have given the text fields the same name. When you do this only the last fields value will be submitted. In order for all the values for the fields that have the same name you need to add square brackets to the end of the name, eg name="premio[]" what will happen now is the values will be submitted as an array meaning $_POST['premio'] will contain a multi-dimension array of values. So $_POST['premio'][0] will contain the value for the first field, $_POST['premio'][1] will contain the value for the second field etc. To output all values you can use a simple foreach loop echo "All premico values: <br />"; foreach( $_POST['premio'] as $fieldValue) { echo $fieldValue . '<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/297250-echo/#findComment-1516044 Share on other sites More sharing options...
magcr23 Posted July 10, 2015 Author Share Posted July 10, 2015 (edited) Thx, you helped a lot. But how can i protect that? because if i use foreach( mysqli_real_escape_string($_POST['premio']) as $premio){ echo $premio . '<br />'; $query1 = mysqli_query($con, "INSERT INTO premio (idPremio, premio, idArtesao) VALUES (DEFAULT, '$premio', '$id')"); } The insert won't work Edited July 10, 2015 by magcr23 Quote Link to comment https://forums.phpfreaks.com/topic/297250-echo/#findComment-1516045 Share on other sites More sharing options...
Solution fastsol Posted July 10, 2015 Solution Share Posted July 10, 2015 You need to put the mysqli_real_escape_string on the $premio not on the $_POST in the foreach call. foreach($_POST['premio'] as $premio){ echo $premio . '<br />'; $query1 = mysqli_query($con, "INSERT INTO premio (idPremio, premio, idArtesao) VALUES (DEFAULT, '".mysqli_real_escape_string($premio)."', '$id')"); } Quote Link to comment https://forums.phpfreaks.com/topic/297250-echo/#findComment-1516046 Share on other sites More sharing options...
magcr23 Posted July 10, 2015 Author Share Posted July 10, 2015 (edited) You need to put the mysqli_real_escape_string on the $premio not on the $_POST in the foreach call. foreach($_POST['premio'] as $premio){ echo $premio . '<br />'; $query1 = mysqli_query($con, "INSERT INTO premio (idPremio, premio, idArtesao) VALUES (DEFAULT, '".mysqli_real_escape_string($premio)."', '$id')"); } That works fine, thx for the help mate Edited July 10, 2015 by magcr23 Quote Link to comment https://forums.phpfreaks.com/topic/297250-echo/#findComment-1516047 Share on other sites More sharing options...
Ch0cu3r Posted July 10, 2015 Share Posted July 10, 2015 (edited) mysqli_real_escape_string takes two arguments, the mysqli link resource ($con) as the first argument, and for the second argument being the value to be escaped ($premio). Your call to mysqli_real_escape_string should be mysqli_real_escape_string($con, $premio) Also if you are not going to be inserting a value into idPremio field then you do not need to include that field in your query it can written as just $query1 = mysqli_query($con, "INSERT INTO premio (premio, idArtesao) VALUES ('".mysqli_real_escape_string($con, $premio)."', '$id')"); Edited July 10, 2015 by Ch0cu3r 1 Quote Link to comment https://forums.phpfreaks.com/topic/297250-echo/#findComment-1516050 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.