I get the following error with the below code:
Notice: Undefined variable: error in C:\xampp\htdocs\test\projects\Learning\php\Databases\Forms and Databases\project.php on line 35
The code:
<?php
$clicked = false;
if($clicked == false && isset($_POST['submit'])) {
if ($_POST['label'] == '') {
echo "<p>You must enter in a label!</p>";
$error = true;
}
if ($_POST['url'] == '') {
echo "<p>You must enter in a url!</p>";
$error = true;
}
if ($_POST['status'] == '') {
echo "<p>You must enter in a status (0 or 1)!</p>";
$error = true;
}
}
if ($error != true) {
if (isset($_POST['submit']) && $_POST['submit'] == '1' ) {
$query = "INSERT INTO nav (label, url, target, status, position) VALUES ('$_POST[label]', '$_POST[url]', '$_POST[target]', $_POST[status], $_POST[position])";
$result = mysqli_query($dbc, $query);
if ($result) {
echo "<p>You've added a new navigation link!</p>";
}
else {
echo "<p>An error has occured!</p>";
echo mysqli_error($dbc);
echo '<p>'.$query.'</p>';
}
$clicked = true;//submit button replaced
}
}
?>
<h1>Navigation</h1>
<h5>*Required Fields</h5>
<form action="project.php" method="post">
<label>*Label:</label>
<p><input type="text" name="label" size="50" placeholder="Enter a Label" value=""/></p>
<label>Position:</label>
<p><input type="text" name="position" size="10" placeholder="Enter a Position" value=""/></p>
<label>*URL:</label>
<p><input type="text" name="url" size="50" placeholder="Enter a URL" value=""/></p>
<label>Target:</label>
<p><input type="text" name="target" size="50" placeholder="Enter a target" value=""/></p>
<label>*Status:</label>
<p><input type="text" name="status" size="10" value="" /></p>
<?php
if ($clicked == false) {
echo '<p><button type="submit" name="submit" value = "1" >Add Navigation Link</button></p>';
}
else {
echo '<p><a href "project.php" id = resetBtn>Do Another</a></p>';
}
?>
When I make the following changes:
if($clicked == false && $_POST['submit'] == "1") {
if ($_POST['label'] == '') {
echo "<p>You must enter in a label!</p>";
$error = true;
}
if ($_POST['url'] == '') {
echo "<p>You must enter in a url!</p>";
$error = true;
}
if ($_POST['status'] == '') {
echo "<p>You must enter in a status (0 or 1)!</p>";
$error = true;
}
}
I get these errors:
Notice: Undefined index: submit in C:\xampp\htdocs\test\projects\Learning\php\Databases\Forms and Databases\project.php on line 18
&
Notice: Undefined variable: error in C:\xampp\htdocs\test\projects\Learning\php\Databases\Forms and Databases\project.php on line 35
So clearly the button of name "submit" isn't being "seen" for whatever reason; I believe. This makes some sense to me, kind of... If I'm not mistaken: php reads from start to finish in a linear sort of way, and since the form is below the if statement, the index does not exist yet. This I think is further corroborated by the fact that once I click the submit button, all errors disappear, and the if statement's error (echo) statements within the if statement, are thus executed.
This is mind boggling. This doesn't work either...
if(isset($_POST['submit']) && $_POST['submit'] == '1') {
if (isset($_POST['label']) && $_POST['label'] == '') {
echo "<p>You must enter in a label!</p>";
$error = true;
}
if (isset($_POST['url']) && $_POST['url'] == '') {
echo "<p>You must enter in a url!</p>";
$error = true;
}
if (isset($_POST['status']) && $_POST['status'] == '') {
echo "<p>You must enter in a status (0 or 1)!</p>";
$error = true;
}
}
...YET, in a previous version of the code, the combo of isset and "is equal to" for an if-statement's conditions, solved the Unidentified index problem, as it pertained to the $_POST['submit'], here's that code: ps: as it pertains to this particular block of code, The fellow in the following linked tutorial, which I'm following along with, does not have any of these errors arise, despite me doing the exact same thing as him.
<?php
$clicked = false;
if (isset($_POST['submit']) && $_POST['submit'] == '1' ) {
$query = "INSERT INTO nav (label, url, target, status, position) VALUES ('$_POST[label]', '$_POST[url]', '$_POST[target]', $_POST[status], $_POST[position])";
$result = mysqli_query($dbc, $query);
if ($result) {
echo "<p>You've added a new navigation link!</p>";}
else {
echo "<p>An error has occured!</p>";
echo mysqli_error($dbc);
echo '<p>'.$query.'</p>';
}
$clicked = true;//submit button replaced
}
?>
<h1>Navigation</h1>
<h5>*Required Fields</h5>
<form action="project2.php" method="post">
<label>*Label:</label>
<p><input type="text" name="label" size="50" placeholder="Enter a Label" value=""/></p>
<label>Position:</label>
<p><input type="text" name="position" size="10" placeholder="Enter a Position" value=""/></p>
<label>*URL:</label>
<p><input type="text" name="url" size="50" placeholder="Enter a URL" value=""/></p>
<label>Target:</label>
<p><input type="text" name="target" size="50" placeholder="Enter a target" value=""/></p>
<label>*Status:</label>
<p><input type="text" name="status" size="10" value="" /></p>
<?php
if ($clicked == false) {
echo '<p><button type="submit" name="submit" value = "1" >Add Navigation Link</button></p>';
}
else {
echo '<p><a href "project2.php" id = resetBtn>Do Another</a></p>';
}
?>
again, this works just fine, no errors. So why do I get the undefine variable error in the first block of code I posted? The undefined variable is coming as a result of the subsequent if-statements failing to be executed, and that is I'm assuming a problem to do with the index issue, yet, the errors do not reflect that!
When I replace the conditions with just $clicked == false, as follows:
$clicked = false;
if($clicked == false) {
if ($_POST['label'] == '') {
echo "<p>You must enter in a label!</p>";
$error = true;
}
if ($_POST['url'] == '') {
echo "<p>You must enter in a url!</p>";
$error = true;
}
if ($_POST['status'] == '') {
echo "<p>You must enter in a status (0 or 1)!</p>";
$error = true;
}
}
I get these three undefined index errors ALONG WITH THE BLOODY CODE which obviuosly executes successfully, despite the three indexes being supposed undefined:
Notice: Undefined index: label in C:\xampp\htdocs\test\projects\Learning\php\Databases\Forms and Databases\project4.php on line 20 You must enter in a label!
Notice: Undefined index: url in C:\xampp\htdocs\test\projects\Learning\php\Databases\Forms and Databases\project4.php on line 24 You must enter in a url!
Notice: Undefined index: status in C:\xampp\htdocs\test\projects\Learning\php\Databases\Forms and Databases\project4.php on line 28 You must enter in a status (0 or 1)!
mysqliyou should be using parameterized queries andbind_paramto add user data to your query. DO NOT use string interpolation to accomplish this because you will create severe SQL injection bugs. If this is on the public internet you are at SEVERE risk.