2

I am pretty new to php. I have a php page that contains a form that uses GET to send the data. When the form is submitted I have some PHP that queries a database. My issue: I set a variable named '$id3' that when I echo the var_dump right away shows the correct value. If I echo the var_dump after my database query, it returns a 0. What could be causing this variable to lose its value? Thanks.

EDIT: I think I discovered that the id3 is being set once when the page is loaded, then again when the submit button of the form is pressed. The second time it was rewriting the variable. I have now added an else on the if (isset($_GET('1'])){, but when i navigate to the next page my $id3 is not set.

<?php  require_once('includes/connection.php'); ?>
<?php   



$pos = strpos($_SERVER['REQUEST_URI'],"=");
$pos2 = $pos - strlen($_SERVER['REQUEST_URI'])+1;

$fin = substr($_SERVER['REQUEST_URI'], $pos2);

$id3 = intval($fin);


echo var_dump($id3);


$error = array();

if ( isset($_GET['1']) ) {

    if ( sizeof($error) == 0 ) {
$q1 = mysqli_real_escape_string($mysqli, $_GET['1']);
$q2 = mysqli_real_escape_string($mysqli, $_GET['2']);
$q3 = mysqli_real_escape_string($mysqli, $_GET['3']);
$q4 = mysqli_real_escape_string($mysqli, $_GET['4']);
$q5 = mysqli_real_escape_string($mysqli, $_GET['5']);
$q6 = mysqli_real_escape_string($mysqli, $_GET['6']);
$q7 = mysqli_real_escape_string($mysqli, $_GET['7']);
$q8 = mysqli_real_escape_string($mysqli, $_GET['8']);
$q9 = mysqli_real_escape_string($mysqli, $_GET['9']);
$q10 = mysqli_real_escape_string($mysqli, $_GET['10']);
$q11 = mysqli_real_escape_string($mysqli, $_GET['11']);
$q12 = mysqli_real_escape_string($mysqli, $_GET['12']);
$q13 = mysqli_real_escape_string($mysqli, $_GET['13']);
$q14 = mysqli_real_escape_string($mysqli, $_GET['14']);
$q15 = mysqli_real_escape_string($mysqli, $_GET['15']);
$q16 = mysqli_real_escape_string($mysqli, $_GET['16']);
$q17 = mysqli_real_escape_string($mysqli, $_GET['17']);
$q18 = mysqli_real_escape_string($mysqli, $_GET['18']);
$q19 = mysqli_real_escape_string($mysqli, $_GET['19']);
$q20 = mysqli_real_escape_string($mysqli, $_GET['20']);
$q21 = mysqli_real_escape_string($mysqli, $_GET['21']);
$q22 = mysqli_real_escape_string($mysqli, $_GET['22']);
$q23 = mysqli_real_escape_string($mysqli, $_GET['23']);
$q24 = mysqli_real_escape_string($mysqli, $_GET['24']);
$q25 = mysqli_real_escape_string($mysqli, $_GET['25']);

$sql = "UPDATE ms2 SET 
q1='$q1',
q2='$q2',
q3='$q3',
q4='$q4',
q5='$q5',
q6='$q6',
q7='$q7',
q8='$q8',
q9='$q9',
q10='$q10',
q11='$q11',
q12='$q12',
q13='$q13',
q14='$q14',
q15='$q15',
q16='$q16',
q17='$q17',
q18='$q18',
q19='$q19',
q20='$q20',
q21='$q21',
q22='$q22',
q23='$q23',
q24='$q24',
q25='$q25' WHERE id=$id3";

$mysqli->query($sql);

//Check here of $id3 returns 0

header('Location: game.php?id='.$id3);        

        }
    }
?>

<!DOCTYPE HTML>
<html>
    <head>
        <title>Mobile Security Notifications</title>
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0" />
    <link rel="stylesheet" href="css/bootstrap.min.css" />
    <link rel="stylesheet" href="css/bootstrap-responsive.min.css" />
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="span12">
                <form class="form-horizontal" action="" method="GET">
                    <!--25 question form here-->    
                    <div class="form-actions">
                        <button type="submit" class="btn btn-success">Agree</button>
                            <a href="javascript:window.history.back();"class="btn">Cancel</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>

<?php require_once('includes/connection_end.php');?>
  • is `$id3` also used in connection.php. It could be being overwritten there. – skribe Feb 24 '16 at 05:02
  • No, $id3 is only in this file. I can post that file if it would help, but i can assure you that variable is not used there. – Mitchell Jeppson Feb 24 '16 at 05:03
  • `intval()` in php return `0` on failure, try putting a conditional phrase before setting the value of `$id3` – Wajih Feb 24 '16 at 05:30
  • what are you getting in $_SERVER['REQUEST_URI'] ??????? – Monty Feb 24 '16 at 05:31
  • @Monty don't upbraid me, but the previous page passes the id through GET just like this page is attempting. So i am grabbing the id number using $_SERVER['REQUEST_URI']. I understand that is not a recommended method but this is for a personal project so it will work for this. I get the same result if do `$id3=intval($_GET['id']);`. – Mitchell Jeppson Feb 24 '16 at 05:51
  • @MitchellJeppson I just ask, what you getting in $_SERVER['REQUEST_URI']; if you tell me i will try to solve it. – Monty Feb 24 '16 at 06:06
  • @Monty i appologize, i thought you meant you wanted to know why i was using that. i did a var_dump for all variables involved. Here is what i got $_SERVER['REQUEST_URI'] -> string(24) "myUrl.com/Dezhi/survey.php?id=233" $pos=int(20) $pos2=int(-3) $fin=string(3) "233" $id3=int(223) – Mitchell Jeppson Feb 24 '16 at 06:13
  • There is any other error. try to enable your error report : isset your $id3 – Monty Feb 24 '16 at 06:34
  • @Monty i pasted those two lines of code into my php page but its not showing anything. where would i see the errors if there were some? the isset returned true. – Mitchell Jeppson Feb 24 '16 at 06:42
  • at the top of your page write those lines, you will see error in your browser – Monty Feb 24 '16 at 06:57

1 Answers1

0

when i navigate to the next page my $id3 is not set.

PHP does not hold data between different requests. The only data you can access here to are GET (from url) and POST (hidden) variables (+ external sources like user and server variables, cookies, database, file etc). Your "$id3" variable is in $_GET['id'] after you redirected user to game.php?id=$id3 location.

Moreover, you should use POST method to send that amount of data.

Daniel Kucal
  • 6,651
  • 3
  • 33
  • 52
  • Thanks for the answer. I may have been a little unclear in my edit. When i said the '$id3 is not set', i meant the url shown on the next page is "game.php?id=". So the id is not getting passed. – Mitchell Jeppson Feb 24 '16 at 05:32
  • It's not possible if it shows correct value in var_dump() and you don't edit this variable in other files. Did you turn on reporting all warnings and errors? Maybe it's something like lack of memory issue. – Daniel Kucal Feb 24 '16 at 05:39
  • I am new to php so i am not sure how to 'turn on reporting all warnings and erros'. But i doubt it is memory, this is a pretty small file. – Mitchell Jeppson Feb 24 '16 at 05:42
  • Check out: http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display Where do you run it? Localhost, vps, free hosting? – Daniel Kucal Feb 24 '16 at 05:45
  • I am running it at ipage.com – Mitchell Jeppson Feb 24 '16 at 05:54