Add info to sql from user

i got “else” error`s an no i haf trying to fix it the last 3 dayes at i can figur out the error, need help

// Initialize the session
session_start();
 
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
    header("location: login.php");
    exit;
}
$con = mysqli_connect("localhost", "root", ""); // Establishing Connection with Server
if(!con)
{
	echo 'not connecte';
}

if(mysqli_select_db($con,'dhl'))
{
	echo 'databace not selected';
}

$Chipnummer = $_POST['Chipnummer'];
$DHLnummer = $_POST['DHLnummer'];
$navn = $_POST['navn'];
$Race = $_POST['Race'];

$sql = "insert into users(email, Chipnummer, DHLnummer, navn, Race) values ('$email', '$Chipnummer', '$DHLnummer', '$navn', '$Race')";

if(!mysqli_query ($con, $sql));
 {
	echo 'not incerted';
}else{
	echo 'inserted';
 } 

header("refresh:2 url=index.html");

Need to know what the error is, before anyone can actually help you…

i got the ELSE error
i try to remove the ; from… if(!mysqli_query ($con, $sql))
the the page go to index.html.
the i remove the …header(“refresh:2 url=index.html”);
i got this error
Warning : Use of undefined constant con - assumed ‘con’ (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\insert.php on line 134
databace not selected
Notice : Undefined index: Chipnummer in C:\xampp\htdocs\insert.php on line 144

Notice : Undefined index: DHLnummer in C:\xampp\htdocs\insert.php on line 145

Notice : Undefined index: navn in C:\xampp\htdocs\insert.php on line 146

Notice : Undefined index: Race in C:\xampp\htdocs\insert.php on line 147

Notice : Undefined variable: email in C:\xampp\htdocs\insert.php on line 149
not incerted

So, your variable is $con, not con. And your form is not passing in the names you are looking for.

what do you mean ???

i changed the (!con) to ($con) but it is til the same error with “else”

Because you have changed the condition you are looking for as well.

if(not true) vs if(true)

There is a semicolon after the condition, that’s why you’re getting the else error.

And a header will not work after echoing anything, just FYI.

try to build it at a nother vay:

<?php
// Initialize the session
session_start();
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
    header("location: login.php");
    exit;
}
if(isset($_POST['save']))
{
$sql = "insert into users(email, Chipnummer, DHLnummer, navn, Race) values (?,?,?,?,?)";
$stmt = mysqli_prepare($sql);
$stmt->bind_param("ssss", $_POST['Chipnummer'], $_POST['DHLnummer'], $_POST['navn'], $_POST['Race']);
$stmt->execute();
if (mysqli_query($conn, $sql)) {
      echo "New record created successfully";
} else {
      echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
mysqli_close($con);
?>

but got thos error:
Notice : Undefined variable: con in C:\xampp\htdocs\insert.php on line 142

Warning : mysqli_close() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\insert.php on line 142

and it is NOT addinf it to my sql

Where is the code that connects to the database?

I think you left out the $ in your test for connection. Try if(!$con)

Nice catch @calgeorge.

$con = mysqli_connect(“localhost”, “root”, “”);
replace to
$con = mysqli_connect(“localhost”, “root”, “”, ‘dhl’);

/* Removed those lines
if(mysqli_select_db($con,‘dhl’))
{
echo ‘databace not selected’;
}
*/

you keep making the same mistakes. slow down. try to understand what you are doing.
if you name the database connection as $con, then you need to use this same variable name and spelling throughout the script.

so far you have changed it like so: $con, con, $conn.
you don’t have to use the same variable names specified in tutorials or books.
try to name variables in such a way that you, the author, can understand.
something more explanatory might help you out:

$DBconnection instead of $con in all of its forms.
$DBcommand instead of $sql.
$DBprocedure instead of $stmt

then, perhaps, you can better follow your code.
have you since corrected all of the errors?

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service