Search code examples
phpvariablesfwrite

In function fwrite() in php I want to write "$" character?


<?php

     if(isset($_POST['import'])){
         
           $index = fopen("index.php", "w");

           $indextxt="
<?php 
           

$d='".$domain."'; $u='".$user."';
           
           
           ?>";
          if(fwrite($index, $indextxt)){
             echo"<script> alert('Downloaded!');</script>";
          }        
     } 
?>

In the above code when the file is imported the variable $d & $u is not appearing(nothing is appearing) in index.php I want $u and $d to appear as it is but php is considering it as a variable and as there is no value assigned to any of the variables nothing is displayed but i want to display $d and $u as simple text and the value of $domain and $user is fetched from database.

<?php

     if(isset($_POST['import'])){
         
           $index = fopen("index.php", "w");

           $indextxt="
<?php 
           


$user='email@email.com';

$sql='SELECT * from content WHERE email='.$user.'';

$run= mysqli_query($con, $sql);

if (mysqli_num_rows($run) > 0) {
    while($row = mysqli_fetch_array($run)) {
        $id=$row['user_id'];
        $name=$row['c_name'];
        $phone=$row['c_number'];
    $whatsapp=$row['w_number'];
 }
}
$d='".$domain."'; $u='".$user."';
           
           
           ?>";
          if(fwrite($index, $indextxt)){
             echo"<script> alert('Downloaded!');</script>";
          }        
     } 
?>

if i want to write a sql query in php again the error starts at line line $id=$row['user_id']; because of this single quote'.

is there any way in php to make php understand that I need $u and $d as simple text and not as a value of php in php file in fwrite function and any way to import sql query in php file without any error?

Thanks in advance.


Solution

  • your problem is about using double-quotation

    in double quotations you should use \$ instead of $ because when use it, php think you wanna put an exists variable in your string. try this:

    <?php
    
    if(isset($_POST['import'])){
        $index = fopen('index.php', 'w');
        $indextxt = "
        <?php 
        \$user='email@email.com';
        \$sql='SELECT * from content WHERE email='.\$user.'';
        \$run= mysqli_query(\$con, \$sql);
        if(mysqli_num_rows(\$run) > 0){
            while(\$row = mysqli_fetch_array(\$run)){
                \$id = \$row['user_id'];
                \$name = \$row['c_name'];
                \$phone = \$row['c_number'];
                \$whatsapp = \$row['w_number'];
            }
        }
        \$d = '\" . \$domain . \"';
        \$u = '\" . \$user . \"';
        ?>";
        if(fwrite($index, $indextxt)){
            echo"<script> alert('Downloaded!');</script>";
        }
    } 
    ?>