PHP to Mysqli in PHP 5

I have been working on the form to database for sometime now, ofcourse I am a student and things are not working as desired. However, I have made some progress. I have a form page that gathers member info Firstname, Lastname, State, Email, username, passoword and verfiy password. The page is account.php.

That info is sent to login.php and gathers the info into variables, connects to server (localhost). To make sure the information is getting passed I echo the Firstname to the page and it prints fine, the problem is this, it does not insert info into the database. I also have the database echo the results to the page for now to see if the info is entering the database, which it is not. It does create the ID number (auto_increment) and the column names, but no member info. Before I wasn’t even getting that, so I have made some improvement.

Below is what I have to connect, gather and insert member info. Could you please review and advise why information is not inserting into the database.

NOTE: Member names, state and email is set to one table and username, password and verfiy password to another.

[code]

<?php $mysqli = mysqli_connect("localhost", "walter", "******", "db_family_members"); if (mysqli_connect_errno()) { printf("Connect failed: %sn", mysqli_connect_error()); exit(); } if ($mysqli) { $first = empty($_POST['FirstName']) ? die ("ERROR: Enter First Name") : mysqli_connect_error($_POST['FirstName']); $last = empty($_POST['LastName']) ? die ("ERROR: Enter Last Name") : mysqli_connect_error($_POST['LastName']); $state = empty($_POST['ST']) ? die ("ERROR: Enter your State") : mysqli_connect_error($_POST['ST']); $email = empty($_POST['EmailAdd']) ? die ("ERROR: Enter your State") : mysqli_connect_error($_POST['EmailAdd']); $query = "INSERT INTO tbl_member_info VALUES (NULL, '".$first."', '".$last."', '".$state."', '".$email."')"; mysqli_query($mysqli, $query); } if ($mysqli) { $user = empty($_POST['Username']) ? die ("ERROR: Enter Username") : mysqli_connect_error($_POST['Username']); $pass = empty($_POST['Password']) ? die ("ERROR: Enter Password") : mysqli_connect_error($_POST['Password']); $vpass = empty($_POST['VPassword']) ? die ("ERROR: Verify Password") : mysqli_connect_error($_POST['VPassword']); $query = "INSERT INTO tbl_users VALUES (NULL, '".$user."', '".$pass."', '".$vpass."')"; mysqli_query($mysqli, $query); } if ($mysqli) { $result = $mysqli->query("SELECT * FROM tbl_member_info"); while($row = $result->fetch_array()) { foreach($row as $key => $value){ echo "$key = $value
n"; } } } $result->close(); $mysqli->close(); ?>[/code]

MOD EDIT: Added code tags, removed password

Hi there,

I am a beginner myself, so apologies for the simplicity of this answer!

This is (a shortened version of) code I used myself recently, which may be of use to you.

[code]<?
//load database connection info
include(“dbinfo.inc.php”);
$connection = mysql_connect($server,$username,$password);

//check database is connectable
if (!$connection){
echo mysql_errno().": “.mysql_error().”
";
exit;
}

if(!mysql_select_db($database)){
echo(“Database not found
”);
}

//retrieve post data
$id = $_POST[“ID”];
$FirstName = $_POST[“FirstName”];
$MiddleName = $_POST[“MiddleName”];
$LastName = $_POST[“LastName”];

$query = “INSERT INTO persons(FirstName,MiddleName,LastName)
VALUES
(
'” . $FirstName . “’,
'” . $MiddleName . “’,
'” . $LastName . “’,
)”;

$result = mysql_query ($query) or die("SQL Error: " . mysql_error());
mysql_close();
?>[/code]

As for the form, obviously the inputs have names such as FirstName etc.

I can’t see anything wildly different from your code (other than the empty stuff, and I have to admit I don’t know what that does!), but it may be of help to you.

Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service