Jump to content

echo


magcr23
Go to solution Solved by fastsol,

Recommended Posts

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?

Link to comment
Share on other sites

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 />';
}

Link to comment
Share on other sites

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 by magcr23
Link to comment
Share on other sites

  • Solution

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')");
}
Link to comment
Share on other sites

 

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 by magcr23
Link to comment
Share on other sites

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 by Ch0cu3r
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.