How do I display "input" value of form at its' box ?

Hello everyone,
Attached is a PHP code to assign data into an HTML form:
[php]

<?php // abc.php $MyHOST = 'localhost'; $MyUSER = 'user'; $MyPASS = 'pass'; $MyDB = 'test'; $MyCONNECTION = NEW MYSQLI($MyHOST,$MyUSER,$MyPASS,$MyDB); IF(!$MyCONNECTION) DIE('Gevald' .MYSQLI_CONNECT_ERROR()); MYSQLI_SET_CHARSET($MyCONNECTION,'UTF8'); IF (ISSET($_POST['myEMP'])) { $yosEmp = get_post($MyCONNECTION,'myEMP'); $MyQUE = "INSERT INTO hourShifts (employer) VALUES($yosEmp)"; $MyRESULT = $MyCONNECTION->query($MyQUE); if (!$MyRESULT) echo "INSERT failed: $MyQUE
" . $MyCONNECTION->error . "

"; } ELSE ECHO $MyCONNECTION->error; echo <<<_END Employer _END; function get_post($MyCONNECTION, $var) { return $MyCONNECTION->real_escape_string($_POST[$var]); } ?>

[/php]
Once I push the “submit” yhe value entered goes to a MySQL table and vanishs from the display.
I’d like the value entered to remain for further process like editing or delete.
How do I make an input box value to remain on the display?
Thanks !

Where in the world did you come up with that mish mash of case structure? You have all upper, some half camelCase all upper thing. Use lowercase with under_score separator and stick to it. That looks like a kindergartener wrote it.

Just echo the value you want in the value attribute of the input field.

[hr]

You don’t need this code:

[php] function get_post($MyCONNECTION, $var)
{
return $MyCONNECTION->real_escape_string($_POST[$var]);
}
?>[/php]

Escaping parameters is not necessary if you use parameterized/prepared statements (as you should).

You also don’t need to close the PHP tag at the end of a file, only when you change from PHP to HTML output.

[hr]

[php]$yosEmp = get_post($MyCONNECTION,‘myEMP’);

$MyQUE = “INSERT INTO hourShifts (employer) VALUES($yosEmp)”;
$MyRESULT = $MyCONNECTION->query($MyQUE);
if (!$MyRESULT) echo “INSERT failed: $MyQUE
” . $MyCONNECTION->error . “

”;[/php]

Should be

[php]$stmt = $MyCONNECTION->prepare(“INSERT INTO hourShifts (employer) VALUES (?)”);
$stmt->bind_param(‘s’, $_POST[‘myEmp’]);
if($stmt->execute() === false) {
echo "INSERT failed: " . $MyCONNECTION->error . “


}[/php]

[hr]

And changing the code to a more readable format, snakeCase and outputting HTML properly you would en up with something like.

[php]<?php // db.php
$config = array(
‘host’ => ‘localhost’,
‘user’ => ‘user’,
‘pass’ => ‘pass’,
‘db’ => ‘test’
);

$db = new mysqli($config[‘host’], $config[‘user’], $config[‘pass’], $config[‘db’]);

if (mysqli_connect_errno()) {
printf(“Connect failed: %s\n”, mysqli_connect_error());
exit();
}

$mysqli->set_charset(“utf8”)[/php]

[php]<?php // abc.php
require ‘db.php’;

$errors = array();

$employee = isset($_POST[‘employer’]) ? $_POST[‘employer’] : null;

if ($_SERVER[‘REQUEST_METHOD’] === ‘POST’) {
$stmt = $db->prepare(“INSERT INTO hourShifts (employer) VALUES (?)”);
$stmt->bind_param(‘s’, $employer);
if($stmt->execute() === false) {
$errors[] = "INSERT failed: " . $db->error;
}
}

require ‘abc.html.php’;[/php]

[php]<php // abc.html.php

foreach ($errors as $error) {
echo $error . ‘

’; // not supposed to use breaks for layout like this
}
?>

Employer [/php]

Thanks a lot JimL !

Sponsor our Newsletter | Privacy Policy | Terms of Service