Inserting Checkbox value into mysql db from a form

Last issue and I promise to leave everyone alone :smile: Now that everything is working the way it should, thank you all again for your assistance it has been invaluable, I have 1 last issue. I’m trying to run an fpdf report from the record that’s selected. I tried a simple ‘Hello World’ report but when I click on the report link I created nothing happened. No error on the page or anything. I put the code in my data.php file (see below) after my delete record button. Do I need to add something to my webapp.js? Thanks again,

} elseif ($job == ‘report_record’){

// Run Report
  if ($id == ''){
  $result  = 'error';
  $message = 'id missing';
} else {

require(‘fpdf/fpdf.php’);

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont(‘Arial’,‘B’,16);
$pdf->Cell(40,10,‘Hello World!’);
$pdf->Output();

The code for generating the pdf is working on my side

make sure you bind the click event correctly for the report button
do you see any error message on your browser’s console?
are you sure your php can reaches the place where you put this code?

I was actually able to get it to open ‘Hello World’ now I just need to get it to get it to create a report with a chosen record. Thanks.

did you include report_record to the job checking if statement?

And while you’re checking that, you may also want to use a better way to check for valid jobs using in_array() function:

if(in_array($job, array('get_records', 'get_record', 'add_record', 'edit_record', 'report_record')){
// Do something
}

see http://php.net/manual/en/function.in-array.php

also, you can make use of the switch statement to group your job tasks instead of the complexity of chained elseif blocks

switch($job){
  case 'get_records':
    // Get records code here
  break;

  case 'get_record':
    // Get record code here
  break;
  .
  .
  .
}

see http://php.net/manual/en/control-structures.switch.php

Regards

1 Like

I did add the report_record and it works fine. I’ll play around with your ‘switch’ solution and see how that goes. I didn’t know about that statement. I just need to use the selected record to make the fpdf report now. Thanks.

I personally prefer switch() as soon as there’s more than a couple of choices involved. A lot of it is personal choice, but that seems to be clearer to me. You also have the option to run the same code (or some of the same code) for more than one value by carefully laying them out.

I’m trying to pass the variable HEADID (the primary id) to a page called report.php using sessions but its not working correctly. I basically need to send the correct HEADID # of the record that the user chooses but it doesn’t seem to pass anything. I think I’m just putting it in the wrong place as when I put it outside of the if/then statement in data.php with a variable like ‘Purple’ $_SESSION[‘SESSION_HEADID’] = ‘Purple’; and echo it from the report.php page it displays Purple. Any thoughts? Thanks.

Show the code. In general what you’re trying to do should work, so there must be something you’re missing. Did you put your session_start() code at the top of each PHP script, before any output?

I do have session_start() at the top of each page. On the data.php page I have the following $_SESSION[‘SESSION_HEADID’] = $id; at the end of the ($job == ‘get_record’){ query just before the elseif statement (all of this is above in post 19). And in report.php I have session_start() at the top then echo $_SESSION[‘SESSION_HEADID’]; after I connect to the DB.

It’d be easier to see it in context, can’t you re-post the code as it stands now? I only ask as I’ve been in situations where I can’t see a problem that is staring me in the face, yet someone else spots it in a second, and none of the session variable handling you describe is in your code in post #19. It’d be much easier to see the code, than try to imagine where you’ve added the various parts.

Also

How are you calling the report.php page? Using a header redirect, a link or an Ajax call? I suspect that’s not relevant as you said it works if you hard-code some stuff as a test.

Of course here’s the code, sorry about that. (I haven’t had a chance to play with the switch() statement yet, I’m waiting until I have everything else working first) I highlighted what code I added. Thank you.

<?php

**session_start();**

// Database details
$db_server   = 'localhost';
$db_username = 'root';
$db_password = '#######';
$db_name     = 'mydb';
// Get job (and id)
$job = '';
$id  = '';
if (isset($_GET['job'])){
  $job = $_GET['job'];
  if ($job == 'get_records' 	||
      $job == 'get_record'   	||
      $job == 'add_record'   	||
      **$job == 'report_record' 	||**
      $job == 'edit_record' 
	  )	{
    if (isset($_GET['id'])){
      $id = $_GET['id'];
      if (!is_numeric($id)){
        $id = '';
      }
    }
  } else {
    $job = '';
  }
}
// Prepare array
$mysql_data = array();
// Valid job found
if ($job != ''){
  
  // Connect to database
  $db_connection = mysqli_connect($db_server, $db_username, $db_password, $db_name);
  if (mysqli_connect_errno()){
    $result  = 'error';
    $message = 'Failed to connect to database: ' . mysqli_connect_error();
    $job     = '';
  }
  // Execute job
  if ($job == 'get_records'){
    // Get records
    $query = "SELECT * FROM DB_Table1 ORDER BY var_1";
    $query = mysqli_query($db_connection, $query);
    if (!$query){
      $result  = 'error';
      $message = 'query error';
    } else {
      $result  = 'success';
      $message = 'query success';
      while ($record = mysqli_fetch_array($query)){
      $functions  = '<div class="function_buttons"><ul>';
      $functions .= '<li class="function_edit"><a data-id="'  . $record['HEADID'] . '" data-name="' . $record['var_1'] . '"><span>Edit</span></a></li>';
      **$functions .= '<li class="function_report"><a href="http://apcweb03/test/report.php" target=_blank></a></li>';**
      $functions .= '</ul></div>';
      $mysql_data[] = array(
          "var_1"      		=> $record['var_1'],
          "var_2"  			=> $record['var_2'],
          "variable_check " => $record['variable_check'],		  		  
          "functions"     	=> $functions
        );
      }
    }
  } elseif ($job == 'get_record'){
    // Get record
    if ($id == ''){
      $result  = 'error';
      $message = 'id missing';
    } else {
      $query = "SELECT * FROM DB_Table1  WHERE HEADID = '" . mysqli_real_escape_string($db_connection, $id) . "'";
      $query = mysqli_query($db_connection, $query); 
      if (!$query){
        $result  = 'error';
        $message = 'query error';
      } else {
        $result  = 'success';
        $message = 'query success';
        while ($record = mysqli_fetch_array($query)){
          $mysql_data[] = array(
          	"var_1"  			=> $record['var_1'],	
            "var_2"          	=> $record['var_2'],
			"variable_check"  	=> $record['variable_check'],
			"variable_check2"  	=> $record['variable_check2'],
			"variable_check3"  	=> $record['variable_check3'],											
          );
        }
      } **$_SESSION['SESSION_HEADID'] = $id;**	
    }
  } elseif ($job == 'add_record'){
    // Add record
    $query = "INSERT INTO DB_Table1  SET ";		
    if (isset($_GET['var_1'])) 				{ $query .= "var_1 = '" 	. mysqli_real_escape_string($db_connection, $_GET['var_1'])	. "', "; }
    if (isset($_GET['var_2']))         		{ $query .= "var_2 = '" 	. mysqli_real_escape_string($db_connection, $_GET['var_2']) . "', "; }
	
//----------------------Checkboxes---------------------	
	
	if (isset($_GET['variable_check']) 	&& $_GET['variable_check'] == 1) 	{ $query .= " variable_check = 1, "; } 	else 	{ $query .= " variable_check = 0, "; }
	if (isset($_GET['variable_check2']) 	&& $_GET['variable_check2'] == 1) 		{ $query .= " variable_check2 = 1, "; } 	else 	{ $query .= " variable_check2 = 0, "; }
	if (isset($_GET['variable_check3']) 	&& $_GET['variable_check3'] == 1) 		{ $query .= "variable_check3 = 1 "; } 		else 	{ $query .= "variable_check3 = 0 "; }

    $query = mysqli_query($db_connection, $query);
    if (!$query){
      $result  = 'error';
      $message = 'query error';
    } else {
      $result  = 'success';
      $message = 'query success';
    }
  } elseif ($job == 'edit_record'){
    // Edit record
    if ($id == ''){
      $result  = 'error';
      $message = 'id missing';
    } else {
      $query = "UPDATE DB_Table1  SET ";	
	      
    if (isset($_GET['var_1'])) 				{ $query .= "var_1 = '" 	. mysqli_real_escape_string($db_connection, $_GET['var_1'])	. "', "; }
    if (isset($_GET['var_2']))         		{ $query .= "var_2 = '" 	. mysqli_real_escape_string($db_connection, $_GET['var_2']) . "', "; }				
	
//----------------------Checkboxes---------------------							

	if (isset($_GET['variable_check']) 	&& $_GET['variable_check'] == 1) 	{ $query .= " variable_check = 1, "; } 	else 	{ $query .= " variable_check = 0, "; }
	if (isset($_GET['variable_check2']) 	&& $_GET['variable_check2'] == 1) 		{ $query .= " variable_check2 = 1, "; } 	else 	{ $query .= " variable_check2 = 0, "; }
	if (isset($_GET['variable_check3']) 	&& $_GET['variable_check3'] == 1) 		{ $query .= "variable_check3 = 1 "; } 		else 	{ $query .= "variable_check3 = 0 "; }
	
      $query .= "WHERE HEADID = '" . mysqli_real_escape_string($db_connection, $id) . "'";
      $query  = mysqli_query($db_connection, $query);
      if (!$query){
        $result  = 'error';
        $message = 'query error';
      } else {
        $result  = 'success';
        $message = 'query success';
      }
    }
  } 
  // Close database connection
  mysqli_close($db_connection);
}
// Prepare data
$data = array(
  "result"  => $result,
  "message" => $message,
  "data"    => $mysql_data
);
// Convert PHP array to JSON array
ini_set('memory_limit', '500M');
$json_data = json_encode($data);
print $json_data;
?>

And the report.php is pretty straight forward, it looks like this:

<?php
session_start();

$db_server   = 'localhost';
$db_username = 'root';
$db_password = '#######';
$db_name     = 'mydb';

// Make the connection

  $db_connection = mysqli_connect($db_server, $db_username, $db_password, $db_name);
  if (mysqli_connect_errno()){
    $result  = 'error';
    $message = 'Failed to connect to database: ' . mysqli_connect_error();
  }

echo $_SESSION['SESSION_HEADID'];

?>

Off Topic

@toddsimonds: when you post code on the forums, you need to format it so it will display correctly.

You can highlight your code, then use the </> button in the editor window, or you can place three backticks ``` (top left key on US/UK keyboards) on a line above your code, and three on a line below your code. I find this approach easier, but unfortunately some European and other keyboards don’t have that character.

1 Like

Thanks for the heads up, I noticed the code wouldn’t always come out right.

1 Like

Unless I’m reading it wrong, you have added the link to report.php inside the code for where your job = get_records, but you don’t set the session variable in there, you only set it in the code for where your job = get_record. You’re adding the link into $functions, but no session var.

Hello again,

I noticed that you store the HEADID only when you request get_record job,
so, if it’s not called before you load the report.php then session will not be there.

anyways, please let me know if I’m wrong,
what you need to do is to click one of the report links and then generate its report right?

if so, you can include the HEADID value in the report link as a query string variable (i.e. headid)

$functions .= '<li class="function_report"><a href="http://apcweb03/test/report.php?headid='. $record['HEADID'].'" target=_blank></a></li>';

then from the report.php use $_GET array to retrieve the HEADID instead of session

$headID=$_GET['headid']

If this is not the case, please let us know how are you calling the report.php (from the browser or from the server)

Regards

That seems to have worked perfectly! Thank you so much.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.