siddparkar siddparkar - 1 month ago 16
SQL Question

How to use GLOBALS in php to post form data

This might seem like a really dumb question, but I have been told to use the GLOBALS variable in PHP and I really have no idea how it works. I tried looking it up but to no avail.

so this is my config.php

<?php
$GLOBALS['project_name'] = 'test';
?>


I require it in my index.php and use it in form text field like

<?php include 'config.php'; ?>

</?php include 'create_database.php'; ?>

</?php include 'create_table.php'; ?>

<div class = "form-group">
<label class="control-label col-sm-2" for = "file">File:</label>
<div class="col-sm-8">
<input type = "file" name = "image_file" id = "image_file" class = "form-control" accept="image/*" onChange="autoPull(this.value)";>
</div>
</div>


<!--<?php
$sql = "SELECT DISTINCT demo_name FROM demo";
mysqli_select_db($conn, 'demo_site');
$result = mysqli_query($conn, $sql);

if(!$result) {
die('Could not get what you wanted: ' . mysqli_error($conn));
}
echo '<div class = "form-group">';
echo '<label class="control-label col-sm-2" for = "project_name">';
echo 'Project:';
echo '</label>';
echo '<div class="col-sm-8">';
echo "<select name = 'project_name' id = 'project_name' class='form-control' placeholder='Enter Project Name'>";
echo "<option value=\"$value\"></option>";
while($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['demo_name'] ."'>" . $row['demo_name'] ."</option>";
}
/*echo "<option selected='selected' value='ABC'>ABC</option>";*/
echo "</select>";
echo '</div>';
echo '</div>';

?>-->


<h3 class="text-center">If Project Name does not exist, add new one below.</h3>
<div class = "form-group">
<label class="control-label col-sm-2" for="project_name">Project Name:</label>
<div class="col-sm-8">
<input type = "text" name="<?php echo($project_name) ?>" id="project_name" class="form-control" placeholder="Enter Project Name" value= "">
</div>
</div>

<div class = "form-group">
<label class="control-label col-sm-2" for = "version">Version:</label>
<div class="col-sm-8">
<input type = "text" name = "version" id = "version" class = "form-control" placeholder="Enter Version" required>
</div>
</div>

<div class = "form-group">
<label class = "control-label col-sm-2" for = "description">Description:</label>
<div class = "col-sm-8">
<textarea class = "form-control" id = "description" rows = "5" placeholder="Enter Description" name = "description"></textarea>
</div>
</div>
<div class = "button">
<div class="form-group">
<div class="col-sm-offset-2 col-sm-6">
<input type="submit" name = "submit" class="btn btn-primary" value = "Submit" id="file_upload">
</div>
</div>
</div>
<br/>
<div class="progress">
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width:0%;">
</div>
</div>
<div id = "result"></div>
</form>
</div1>
</body>




I want to send this data to upload.php like this

<?php

include_once('config.php');

$project_name = $_POST[$project_name];
$version = $_POST['version'];
$description = $_POST['description'];
$data = "INSERT INTO demo VALUES(NULL, '$project_name', '$version', '$description', '$path')";

$download_data = "SELECT demo_id, demo_name, demo_version, demo_details, file
FROM demo
WHERE demo_name = '$project_name'
AND demo_version = '$version'";

mysqli_select_db($conn, 'demo_site');

$return = mysqli_query($conn, $data);

$return_data = mysqli_query($conn, $download_data);

if(!$return) {
die('Entered form values in the database successfully:' . mysqli_error($conn));
}

echo "Entered data successfully\n";

while ($row = mysqli_fetch_array($return_data)) {

$id = $row['demo_id'];
$name = $row['demo_name'];
$version = $row['demo_version'];
$details = $row['demo_details'];
$file = $row['file'];
$new = basename( $row['file'] ); // GET FILE NAME ONLY, GET RID OF PATH.
//echo "<a href='http://{$_SERVER['SERVER_NAME']}:8080/demo_webpages_project/all/$new'>";
echo "<a href='http://{$_SERVER['SERVER_NAME']}:8080/tester/$version'>";
echo "Link To Uploaded Image";
echo '</a>';
}

mysqli_close($conn);


?>

I get undefined index:text in upload.php when the value test should be submitted at project name.

Please help

--edited

Answer
name = $GLOBALS['project_name']

This is the problem. You have included php code inside the html without telling php to process it. You can try it like this:

<input type = "text" name="<?php echo($GLOBALS['project_name']) ?>" id = "project_name" class = "form-control" placeholder="Enter Project Name"  value = "">

However, as you have it written you probably don't need to use the $GLOBALS superglobal variable at all, as everything is being executed in the same scope (you have no functions). You could simply have this:

config.php

<?php
$project_name = 'test';
?>

index.php

<?php
include_once('config.php');
?>
<form action="upload.php" method="post">
  <h3 class="text-center">If Project Name does not exist, add new one below.</h3>     
  <div class = "form-group">
    <label class="control-label col-sm-2" for="project_name">Project Name:</label>
    <div class="col-sm-8">
      <input type = "text" name="<?php echo($project_name) ?>" id="project_name" class="form-control" placeholder="Enter Project Name"  value= "">
    </div>
    <button type="submit">Submit</button>
  </div>
</form>

upload.php

<?php
include_once('config.php');

$project_name = $_POST[$project_name];
$version = $_POST['version'];
$description = $_POST['description'];
$data = "INSERT INTO demo VALUES(NULL, '$project_name', '$version', '$description', '$path')";

$download_data = "SELECT demo_id, demo_name, demo_version, demo_details, file 
                      FROM demo
                      WHERE demo_name = '$project_name'
                      AND demo_version = '$version'";

?>

note: this last section of upload.php is dangerous for a production environment, you should read about "sql injection" and "prepared statements" if you want to actually run this code on an sql server.

explanation of $GLOBALS

I'm not sure what research you have done on this superglobal but i would suggest reading the php.net page on scope: http://php.net/manual/en/language.variables.scope.php

This code snippet demonstrates a correct usage:

<?php
$a = 1;
$b = 2;

function Sum()
{
    $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
} 

Sum();
echo $b;
?>

In this case the output would be 3. If the code was written without using $GLOBALS:

<?php
$a = 1;
$b = 2;

function Sum()
{
    $b = $a + $b;
} 

Sum();
echo $b;
?>

The output would be 1. The variables $a and $b within the sum() function in this example are "local variables" - they are only in scope within the sum() function and are different memory locations (despite having the same names) to the $a and $b variables that are assigned the values of 1 and 2 respectively. So the sum operation happens, but it does not alter the value of the $b variable being output by the echo statement (as the $b within sum() is not the same as the $b outside of the function).