Can't Insert data

I am trying to program a query to insert new data into a database. I was able to select from the same database,but for some reason the insert eludes me. Please help, I’m loosing my mind. I also don’t see where to attach code so I guess I’ll just post it here.

[php]

Staff Info

Insert Staff Info

Staff Information
First Name:
Last Name:
Email:
Phone:
[/php] [php]<?php
$connect=mysqli_connect ("localhost","root","");

if (!$connect) {
	echo "Not Connected to Server";
}

if(!mysqli_select_db($connect,"osticket")){
	echo "No db Found";
}

if(isset($_POST['submit'])){
	$firstname=$_POST["firstname"];
	$lastname=$_POST["lastname"];
	$email=$_POST["email"];
	$phone=$_POST["phone"];
	
	$insert = "INSERT INTO ost_staff(firstname, lastname, email, phone) VALUES ('$firstname','$lastname','$email','$phone')";
	if(!mysqli_query($connect,$insert)){
		echo "Records Not Inserted.";

  } else{

        echo "Inserted Successfully";

  }
}

?>[/php]

I personally use PDO for I find it easier to understand and it only uses OOP not procedural style coding which is an added plus in my opinion.

My suggest is slow down, take a deep breath and learn PDO (or mysqli) correctly, by that I mean using prepared statements. In my opinion you’re just going to muddy the issues if you take shortcuts and you really don’t save that much time or typing.

Here’s an example done using PDO that I wrote a couple of months ago (Just for the fun of it).
[php]/*

  • Save data to database table after validation is done.
    */
    function saveRegistration(array $data, $pdo) {
    $password = password_hash($data[‘password’], PASSWORD_DEFAULT);

    $query = ‘INSERT INTO users(username, email, password, dateCreated) VALUES ( :username, :email, :password, NOW())’;
    $stmt = $pdo->prepare($query);
    $result = $stmt->execute([’:username’ => $data[‘username’], ‘:email’ => $data[‘email’], ‘:password’ => $password]);
    if ($result) {
    return TRUE;
    } else {
    return FALSE;
    }
    }[/php]

and for god sakes don’t use a table inside of a form this isn’t tabulated data that is being spit out.

[php]

Registration Form

username:

password:

verify password:

email address:

verify email:



[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service