Passing a variable in URL in a http request when a user succesfully complets a mysql query

StackOverflow https://stackoverflow.com/questions/21093851

  •  27-09-2022
  •  | 
  •  

Question

:)

Ok, so I am something of a newbie programmer, still using procedural php & mysqli(i know, i know, I will learn oop & PDO, everything in good time.)

So...I...have...this friend...and he has a problem.

You see, he has this sql query

<?php
require_once('header.php');
require_once('connectvars.php');
require_once('startsession.php');
     if (!isset($_SESSION['user_id'])) {
     echo '<p class="login">Please <a href="login.php">log in</a> to access this       page.</p>';
exit();
}

$newnotecaseid = $_GET['newnote'];
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

if (isset($_POST['submit'])) {
// Grab the profile data from the POST
$note_subject = mysqli_real_escape_string($dbc, trim($_POST['note_subject']));
$new_note = mysqli_real_escape_string($dbc, trim($_POST['new_note']));
$query = "INSERT INTO case_notes (`note_subject`, `note_text`, `note_date_time`, `case_id`) VALUES ('$note_subject', '$new_note', NOW(),'$newnotecaseid')";
mysqli_query($dbc, $query);

//Confirm success with the user
 $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/case.php?userid=$newnotecaseid';
 header('Location: ' . $home_url);
 }
 mysqli_close($dbc);

 ?>

The problem is that he knows how to pass a variable in an HREF in an element, but when he uses this code when the sql query is successfully completed, instead of passing the info stored in the variable it just passes the variable name itself, which of course the page it is now on has no idea what to do with and I....er...I mean he, gets a whole bunch of undefined index warnings. Is he not doing this right? can you even send a variable like this? He wants to make sure that when the sql statement is complete the user is automatically rerouted to this page.

$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) .        '/case.php?userid=$newnotecaseid';
 header('Location: ' . $home_url);

He thanks anyone who can point him in the right direction :)

Was it helpful?

Solution

You have an error in passing variable into string:\

$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/case.php?userid=$newnotecaseid';

Use concatenation:

$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/case.php?userid=' . $newnotecaseid;

OTHER TIPS

you are doing this

$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/case.php?userid=$newnotecaseid';

but you must try this

$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/case.php?userid='.$newnotecaseid;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top