HELP: form action method=post and isset submit

[php]<!doctype html>

Pilot Codex Recording <?php if (isset($_POST['codexsubmit'])) { $run = "running"; print_r($run); // initial database stuff $host = "localhost"; $user = "root"; $pass = "pwd"; $db = "codexdb"; $connection = mysqli_connect($host, $user, $pass); mysqli_select_db($db); // Assign Variables // Pilot id name $pilot_id = $_POST['Pilot']; // consider getting as forum user id or name print_r($pilot_id); // debug if (!(empty($pilot_id))) { // Banner id $banner_id = $_POST['Banner']; // reference date $ref_date = date("Y-m-d"); // MECHS $mech_text = $_POST['mech_textarea']; print_r($mech_text); // debug $lines_count = count(explode("\n", $mech_text)); $lines_array = explode("\n", $mech_text); for ( $l=0; $l<=$lines_count; $l++ ) { $mech_string_raw = ""; $mech_string_raw = $lines_array[$l]; $mech_string = str_replace("\n", "", $mech_raw_string); if (empty($mech_string)) { continue; } if (stripos($mech_string, "Statistics")) { continue; } if (stripos($mech_string, "Matches")) { continue; } // parsing mech stuff $count = 0; $mech_info = ""; $mech_desc = ""; $matches = 0; $win = 0; $loss = 0; $kills = 0; $deaths = 0; $damage = 0; $xp = 0; $time_string = ""; $tmp_string = str_replace(",", "", $mech_string); $tmp_string2 = str_replace("/", "", $tmp_string); $tmp_string3 = str_replace("-", "", $tmp_string2); $tmp_string4 = str_replace("%", "", $tmp_string3); $tmp_string5 = str_replace(" day ", ":", $tmp_string4); $tmp_string6 = str_replace(" days ", ":", $tmp_string5); $count = count(explode(" ", $tmp_string6)); $mech_info = explode(" ", $tmp_string6); $matches = $mech_info[$count-10]; $win = $mech_info[$count-9]; $loss = $mech_info[$count-8]; $kills = $mech_info[$count-6]; $deaths = $mech_info[$count-5]; $damage = $mech_info[$count-3]; $xp = $mech_info[$count-2]; $time_string = $mech_info[$count-1]; $time_count = 0; $time_count = count(explode(":", $time_string)); $time_info = explode(":", $time_string); $time_second = 0; $time_second = $time_info[$time_count-1]; $time_minute = 0; $time_minute = $time_info[$time_count-2]; $time_hour = 0; $time_hour = $time_info[$time_count-3]; $time_day = 0; $time_day=$time_info[$time_count-4]; $total_time = (( $time_second ) + ( $time_minute * 60 ) + ( $time_hour * 3600 ) + ( $time_day * 86400 )); // mech description $text_count = $count - 11; $mech_desc = ""; for ( $x=0; $x<=$text_count; $x++ ) { $mech_desc = $mech_desc + $mech_info[$x] + " "; } $mech_desc = trim($mech_desc); // insert mech stuff $sql_qry = "IF EXISTS (SELECT * FROM pilot_stats_mech WHERE pilot_id = '$pilot_id' AND ref_date = '$ref_date' AND mech_desc = '$mech_desc') " + " THEN " + " UPDATE pilot_stats_mech SET banner_id = '$banner_id', matches = '$matches', win = '$win', loss = '$loss', kills = '$kills', deaths = '$deaths', damage = '$damage', xp = '$xp', total_time = '$total_time' " + " WHERE pilot_id = '$pilot_id' AND ref_date = '$ref_date' AND mech_desc = '$mech_desc' " + " ELSE " + " INSERT INTO pilot_stats_mech (pilot_id, banner_id, ref_date, mech_desc, matches, win, loss, kills, deaths, damage, xp, total_time) " + " VALUES ('$pilot_id', '$banner_id', '$ref_date', '$mech_desc', '$matches', '$win', '$loss', '$kills', '$deaths', '$damage', '$xp', '$total_time') "; print_r($sql_qry); // debug $result = mysqli_query($connection, $sql_qry); } // for $l } // if !empty $pilot_id } else { $not_run = "Not running"; print_r($not_run); } ?>




Pilot name:

Banner:

NoneAlphaBravo DeltaEchoHotel IndiaJulietteKilo 1st EG2nd EG3rd EG

Mech Stats:


[/php]

The above code is intended to enter a few text boxes with various data and then on pressing the submit key to do the various processing.

I have put some print_r statements in to help debug as events occur but either without the submit button being pressed or with being pressed (else statement within) it never seems to process the PHP?

I’m relatively new to PHP and HTML but not to programming.

I really could do with some help on this one to avoid me buying too many wigs.

Change your beginning to this and you will see everything that is posted. It will also show all errors, which are a few.

[php]<!doctype html>

Pilot Codex Recording
  <?php
  error_reporting(E_ALL);
  ini_set("display_errors", 1);
  echo '<pre>';
  var_dump($_POST);
  if (isset($_POST['codexsubmit'])) {[/php]

Running this (with posted data) I get these errors

Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in /srv/www/test/public/tw79/index.php on line 28
Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /srv/www/test/public/tw79/index.php on line 43
Notice: Undefined variable: mech_raw_string in /srv/www/test/public/tw79/index.php on line 61
Notice: Undefined offset: 1 in /srv/www/test/public/tw79/index.php on line 60
Notice: Undefined variable: mech_raw_string in /srv/www/test/public/tw79/index.php on line 61

Please post back if you have trouble figuring any of them out :slight_smile:

Protip: in html forms you don’t need the action param, if it is not set it will submit to itself :slight_smile:

+1 code review suggestion: try to separate your logic and view

[php]<?php

// start your files with any logic first,
// processing forms, getting data from db, etc

// then stop php
?>

and output your html afterwards[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service