0

I want to print value in $s1 $ s2 $s3 instead of variable name. When value is sent in database then there goes only variable names $s1,$s2,$s3 etc not values in them.

<?php
echo $s1=$_POST['1'];
echo $s2=$_POST['2'];
echo $s3=$_POST['3'];
echo $s4=$_POST['4'];

$proo=$_POST['proo'];
$semm=$_POST['semm'];
$shii=$_POST['shii'];
$secc=$_POST['secc'];
$subb=$_POST['subb'];
if($btnn)
{
    $sele=mysql_query("select * from `sconfirmed` where `pro`='$proo' && `sem`='$semm' && `section`='$secc' && `shift`='$shii' ");
    $count=mysql_num_rows($sele);
    $b='1';

    do
    {

        $a='1';
        $s='$s';
        while($ro=mysql_fetch_array($sele))
        {
            $pic=$ro['pic'];
            $sname=$ro['sname'];
            $rollno=$ro['rollno'];

            $sfather=$ro['sfather'];
            $phone=$ro['phone'];
            $email=$ro['email'];

            $address=$ro['address'];
            $dob=$ro['dob'];
            $iddd=$ro['id'];
            $sess=$ro['ses'];

            $c=$s.$a;


            $insert=mysql_query("insert into `markssub1`(`sid`,`sname`,`sem`,`sec`,`pro`,`shi`,`rollno`,`ses`,`tid`,`tname`,`subject`,`marks`) values ('$iddd','$sname','$semm','$secc','$proo','$shii','$rollno','$sess','$id','$user','$subb','$c') ");
            $a=$a+1;
        }
    }
    while($count<=$b);
}
?>
ndm
  • 59,784
  • 9
  • 71
  • 110
  • 1
    try echoing out the variables within your while loop instead of inserting them first to see if they are containing the values. Also note you are using deprecated mysql connections, and hence are vulnerable to sql injection. – nomistic Jul 14 '15 at 13:45
  • instead of `$c=$s.$a;` try `$c=${"s".$a};` see http://stackoverflow.com/a/9257536/689579 Also, you could shorten `$a=$a+1;` to just `$a++;` – Sean Jul 14 '15 at 13:46
  • 1
    Your code is open to SQL injection attacks. Please see [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) and consider upgrading to PDO using prepared statements. – Oldskool Jul 14 '15 at 13:47
  • I agree with @nomistic about your code security. If you are using in a non-safe environment PDO or mysqli_real_scape_string should be taken into consideration. – Rodrigo Kravetz Jul 14 '15 at 13:48
  • Thank you so much dear..... – user3775044 Jul 14 '15 at 14:03

1 Answers1

0

I think you should change

$s='$s';

to

$s="$s";

There is a difference on using double or single quotes in PHP. Use double quotes when dealing with variables.

Added

If I understood what you're trying to do, see if this example helps you:

$s1 = "something";
$currentVar = "s"."1";
/*Then here you loop through whatever you want (in your example it will be "s".$a */

echo $$currentVar;

will return "something";

that's it?

Rodrigo Kravetz
  • 382
  • 1
  • 11
  • the issue with this is that `$s` is not defined at this point, so `"$s"` will be an empty string. OP needs to concatenate `1`/`2`/`3` to `$s` to access the `$s1`/`$s2`/`$s3` – Sean Jul 14 '15 at 13:50
  • So if I got the idea, it should be $c = $s$a (which will refer to $s1 and so on)? – Rodrigo Kravetz Jul 14 '15 at 13:57