I am inserting some form data into a sql database, but i want to insert an account type label aswell which is not part of the form data.
So basically the form data gets inserted into the database fine, but i also have a column in my database called 'account_type' which i want to insert the word 'member' into as standard.
Can i do this because i have tried by adding this to the code but it doesn't insert the 'member' into the account type.
Code i have tried adding:
<? $result = mysql_query("UPDATE ptb_registrations SET account_type=Member"); ?>
Rest of insert code for form data:
<?php
session_start();
// other php code here
$_SESSION['display_name'] = $_POST['display_name'];
$_SESSION['password'] = $_POST['password'];
?>
<?php ob_start(); ?>
<?php
session_start();
// GET ACCOUNT INFORMATION FROM FORM AND ASSIGN VARIABLES
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
$email = $_SESSION['email'];
$date_of_birth = $_SESSION['date_of_birth'];
$contact_number = $_SESSION['contact_number'];
$display_name = $_SESSION['display_name'];
$password = $_SESSION['password'];
?>
<?php
/*
// ECHO ACCOUNT INFORMATION
echo "<strong> Account Information: </strong>";
echo "<br />";
echo First Name: ";
echo "<br />";
echo $first_name;
echo "<br />";
echo "<br />";
echo "Last Name: ";
echo "<br />";
echo $last_name;
echo "<br />";
echo "<br />";
echo "Email: ";
echo "<br />";
echo $email;
echo "<br />";
echo "<br />";
echo "Password: ";
echo "<br />";
echo $password;
echo "<br />";
echo "<br />";
echo "date_of_birth: ";
echo "<br />";
echo $date_of_birth;
echo "<br />";
echo "<br />";
echo "Contact_number: ";
echo "<br />";
echo $contact_number;
echo "<br />";
echo "<br />";
echo "display_name: ";
echo "<br />";
echo $display_name;
echo "<br />";
echo "<br />";
*/
?>
<?php
////// SEND TO DATABASE
/////////////////////////////////////////////////////////
// Database Constants
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "playtime");
// 1. Create a database connection
$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if (!$connection) {
die("Database connection failed: " . mysql_error());
}
// 2. Select a database to use
$db_select = mysql_select_db(DB_NAME,$connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
//////////////////////////////////////////////////////////////
$query="INSERT INTO ptb_registrations (ID,
first_name,
last_name,
email,
display_name,
date_of_birth,
contact_number,
password
)
VALUES('NULL',
'".$first_name."',
'".$last_name."',
'".$email."',
'".$display_name."',
'".$date_of_birth."',
'".$contact_number."',
'".$password."'
)";
mysql_query($query) or die ('Error updating database');
?>
<?
$result = mysql_query("UPDATE ptb_registrations SET account_type=Member");
?>
<?php
function confirm_query($result_set) {
if (!$result_set) {
die("Database query failed: " . mysql_error());
}
}
function get_user_id() {
global $connection;
global $email;
$query = "SELECT *
FROM ptb_registrations
WHERE email = \"$email\"
";
$user_id_set = mysql_query($query, $connection);
confirm_query($user_id_set);
return $user_id_set;
}
?>
<?php
$user_id_set = get_user_id();
while ($user_id = mysql_fetch_array($user_id_set)) {
$cookie1 = "{$user_id["id"]}";
setcookie("ptb_registrations", $cookie1, time()+3600); /* expire in 1 hour */
}
?>
<?php include ('includes/send_email/reg_email.php'); ?>
<? ob_flush(); ?>
Why not insert it at the same time as the registration?
$query="INSERT INTO ptb_registrations (ID,
first_name,
last_name,
email,
display_name,
date_of_birth,
contact_number,
password,
account_type
)
VALUES('NULL',
'{$first_name}',
'{$last_name}',
'{$email}',
'{$display_name}',
'{$date_of_birth}',
'{$contact_number}',
'{$password}',
'Member'
)";
The following
<? $result = mysql_query("UPDATE ptb_registrations SET account_type='Member'"); ?>
will result in making ALL account_type's equal to 'Member' when you dont have a where clause.
To answer you problem, it is most likely due to the fact you do not have single quotes around member in your update command, see my code.
Related
When I print my code it only prints the question and description of id = 1 but not the rest of the table.
here is my code.
Please show me how to print my entire table which has like 20 questions or so...and also please show me how to make it so that the questions stay on the browser (even when I refresh the page) because currently the data does not stay on the browser when i refresh the page.
Thanks So Much!
<?php
require_once "connection.php";
if(isset($_POST['submit'])) {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );
if($conn->connect_error) {
die("connection error: " . $conn->connect_error);
} else {
echo "Submit button connected to database!";
}
$question = $_POST['question'];
$description = $_POST['description'];
$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
(NULL, '{$question}', '{$description}' ) ";
if($conn->query($sql)) {
echo "it worked";
} else {
echo "error: " . $conn->error;
exit();
}
$query = "SELECT * FROM `ask` ";
if( $result = $conn->query($query)) {
$fetch = $result->fetch_assoc();
echo "<p>{$fetch['question']}</p>";
echo "<p>{$fetch['description']}</p>";
} else {
echo "failed to fetch array";
}
}
?>
You need a for each loop:
<?php
require_once "connection.php";
if(isset($_POST['submit'])) {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );
if($conn->connect_error) {
die("connection error: " . $conn->connect_error);
} else {
echo "Submit button connected to database!";
}
$question = $_POST['question'];
$description = $_POST['description'];
$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
(NULL, '{$question}', '{$description}' ) ";
if($conn->query($sql)) {
echo "it worked";
} else {
echo "error: " . $conn->error;
exit();
}
$query = "SELECT * FROM `ask` ";
if( $result = $conn->query($query)) {
$fetch = mysql_fetch_array($result, MYSQL_ASSOC);
foreach($fetch as $ques) {
echo "<p>" . $ques['question'] . "</p>";
echo "<p>" . $ques['description'] . "</p>";
}
} else {
echo "failed to fetch array";
}
}
?>
All I've done there is change:
$fetch = $result->fetch_assoc();
echo "<p>{$fetch['question']}</p>";
echo "<p>{$fetch['description']}</p>";
to:
$fetch = mysql_fetch_array($result, MYSQL_ASSOC);
foreach($fetch as $ques) {
echo "<p>" . $ques['question'] . "</p>";
echo "<p>" . $ques['description'] . "</p>";
}
fetch_assoc() — Fetch a result row as an associative array
so it gets only 1 row you need to loop through the rest of the rows check the examples reference from php docs
I am trying to search some data from a database. The search works fine, however if I click on search without entering anything into the form, it displays all the data on the database. Anyway I can fix this?
This is my php code.
$link=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$db_selected = mysqli_select_db($link,"AnimalTracker1");
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysqli_error($link));
}
$searchKeyword = $_POST['find']; // Sanitize this value first !!
$sql=mysqli_query($link, "Select * FROM Locations WHERE `Animal_Type` LIKE '%$searchKeyword%' ");
if ($sql == FALSE)
{
die($sql." Error on query: ".mysqli_error($link));
}
while($result = mysqli_fetch_array($sql))
{
echo $result ['Animal_Type'];
echo "<br>";
echo $result ['Latitude'];
echo "<br> ";
echo $result ['Longitude'];
echo " <br>";
echo $result ['Seen'];
echo " <br> ";
echo $result ['Time'];
echo "<br> ";
echo "<br> ";
}
//}
?>
Just make sure $searchKeyword has a (valid) value
$link=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$db_selected = mysqli_select_db($link,"AnimalTracker1");
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysqli_error($link));
}
// checks to see if $_POST['find'] is actually set.
if ( array_key_exists('find',$_POST) )
{
$searchKeyword = $_POST['find']; // Sanitize this value first !!
// sanitize $searchKeyword here
}
// checks to see if $searchKeyword has no value, or just contains empty space
if ( empty(trim($searchKeyword)) )
{
echo "You must enter a search term";
}
else
{
$sql=mysqli_query($link, "Select * FROM Locations WHERE `Animal_Type` LIKE '%$searchKeyword%' ");
if ($sql == FALSE)
{
die($sql." Error on query: ".mysqli_error($link));
}
while($result = mysqli_fetch_array($sql))
{
echo $result ['Animal_Type'];
echo "<br>";
echo $result ['Latitude'];
echo "<br> ";
echo $result ['Longitude'];
echo " <br>";
echo $result ['Seen'];
echo " <br> ";
echo $result ['Time'];
echo "<br> ";
echo "<br> ";
}
}
?>
Try removing the "%" from either the back or the front of the $searchKeyword in the query and I guess it should do the work.
"%" is used when match all or none. So if you send an empty string it will return the whole database.
I have been trying to fix this code for the last 4 hours and cannot seem to get it to work. The $_SESSION variables are set when the user log's in (or creates an account) and are destroyed when logging out. Yet, when I submit a certain form, suddenly $_SESSION variables which were working before are throwing an undefined variable error. My apologies for the large amount of content, but in due diligence I've come to the conclusion I cannot find it myself and must ask someone for help.
Relevant code in the order a user's actions call the code.
<?php
include 'db_connect.php';
include 'functions.php';
session_start();
// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$_SESSION['sessionInitialize'] = false;
// To protect mysqli injection (more detail about mysqli injection)
//$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$mypassword = md5(md5("SaLt".$mypassword."SaLt"));
$query = "SELECT * FROM secure_login.members WHERE username='" . $myusername . "' and password='" . $mypassword . "'";
$result = $mysqli->query($query) or die ($mysqli->error.__LINE__);
// If result matched $myusername and $mypassword, table row must be 1 row
if($result->num_rows == 1){
initializeSessionVariables();
// Register $myusername, $mypassword and redirect to file "acct.php"
$_SESSION['currentUser']=$_POST['myusername'];
//$_SESSION['mypassword']=$_POST['mypassword'];
header("location:myAcct.php");
}
else {
echo "Wrong Username or Password";
header("location:index.php");
}
$mysqli->close();
?>
The function for initializing relevant session variables for the user.
function initializeSessionVariables(){
$_SESSION['currentUser'] = $_SESSION['currentUser'];
$_SESSION['currentUserAcctId'] = "";
$_SESSION['currentUserSummonerId'] = "";
$_SESSION['currentUserLeagues'] = "";
$_SESSION['currentUserEmail'] = "";
$_SESSION['currentUserAvatarURL'] = "";
$_SESSION['currentUserSummName'] = "";
$_SESSION['currentUserRealName'] = "";
$_SESSION['currentUserBday'] = "";
$_SESSION['currentUserSecondEmail'] = "";
}
The account page.
<?php
$summonerName=$_SESSION['currentUser'];
echo "Current User: " . $_SESSION['currentUser'] . "<br>";
echo "<br>Current User \$summonerName: " . $summonerName;
//Create prepared statement.
$query = "SELECT * FROM `stats`.`summoners` WHERE `summoners`.`name`='" . $summonerName . "'";
$result = $mysqli->query($query) or die ($mysqli->error.__LINE__);
echo "<br>test<br><br>";
Print_r ($result);
//Run query if query object returned
if ($result->num_rows == 0){
echo "<h1>=0</h1>";
//Free the result so it can be used in the following functions
$result->free();
getSummonerData(); //defined in functions.php
injectSummonerData(); //defined in functions.php
$query = "SELECT * FROM `stats`.`summoners` WHERE name='" . $summonerName . "'";
$result = $mysqli->query($query) or die ($mysqli->error.__LINE__);
//Get associative array for $result
$row = $result->fetch_assoc();
$_SESSION['currentUserAcctId'] = $row['acctId'];
//Print data.
printf ("<h3>Summoner Name: %s\n <br> Summoner Level: %s\n <br> AcctID: %s\n <br> SummonerID: %s</h3>", $row['name'], $row['summonerLevel'], $row['acctId'], $row['summonerId']);
//Close result object
//$result->close();
echo "<br>";
//Close DB Connection
$mysqli->close();
}
else if($result->num_rows == 1){
echo "<h1>=1</h1>";
$row = $result->fetch_assoc();
$_SESSION['currentUserAcctId'] = $row['acctId'];
printf ("<h3>Summoner Name: %s\n <br> Summoner Level: %s\n <br> AcctID: %s\n <br> SummonerID: %s</h3>", $row['name'], $row['summonerLevel'], $row['acctId'], $row['summonerId']);
echo "<br><h4>This data is already in the database. Did nothing.</h4>";
}
echo "<br>" . $_SESSION['currentUserAcctId'];
?>
Profile.php
<p>Profile info</p>
<?php
displayProfileInformation($_SESSION['currentUser']);
?>
displayProfileInformation Function
function displayProfileInformation($currentUser){
include 'dbstat_connect.php';
$query = "SELECT * FROM `stats`.`userAccount` where `userAccount`.`profName` = '" . $currentUser ."'";
if ($result = $mysqli->query($query) or die ($mysqli->error.__LINE__)){
if ($result->num_rows == 1){
$row = $result->fetch_assoc();
echo "User Name: "; if(isset($currentUser)){echo $currentUser . "<br>";}
echo "Email: "; if(isset($row['email'])){echo $row['email'] . "<br>"; $_SESSION['currentUserEmail'] = $row['email'];}
echo "Avatar URL: "; if(isset($row['avatarURL'])){echo $row['avatarURL'] . "<br>"; $_SESSION['currentUserAvatarURL'] = $row['avatarURL'];}
echo "Summoner Name: "; if(isset($row['summName'])){echo $row['summName'] . "<br>"; $_SESSION['currentUserSummName'] = $row['summName'];}
echo "Real Name: "; if(isset($row['realName'])){echo $row['realName'] . "<br>"; $_SESSION['currentUserRealName'] = $row['realName'];}
echo "Birthdate: "; if(isset($row['bday'])){echo $row['bday'] . "<br>"; $_SESSION['currentUserBday'] = $row['bday'];}
echo "Secondary Email: "; if(isset($row['secondEmail'])){echo $row['secondEmail'] . "<br>"; $_SESSION['currentUserSecondEmail'] = $row['secondEmail'];}
echo "<br>Dafuq yo =1";
}
else if ($result->num_rows == 0){
echo "Dafuq yo =0";
echo "User Name: " . $currentUser . "<br>";
echo "Email: <br>";
echo "Avatar URL: <br>";
echo "Summoner Name: <br>";
echo "Real Name: <br>";
echo "Birthdate: <br>";
echo "Secondary Email: <br>";
echo "<h2>Enter what information you like by editing your profile below.</h2><br>";
}
else {
echo "Critical Error: Contact Admin.";
}
}
}
editProfile.php
Edit Profile
<?php editProfileInformationForm($_SESSION['currentUser']); ?>
editProfileInformationForm Function
function editProfileInformationForm($currentUser){
echo "<form action='processEditProfile.php' method='post'>";
echo "Profile Name: " . $_SESSION['currentUser'] . "<br>";
echo "Account ID: " . $_SESSION['currentUserAcctId'] . "<br>";
if (isset($_SESSION['currentUserEmail'])){
echo "Email: <input name='email' type='text' id='email' value='" . $_SESSION['currentUserEmail'] . "'/><br />";
}
else {
echo "Email: <input name='email' type='text' id='email' value=''/><br />";
}
if (isset($_SESSION['currentUserEmail'])){
echo "Secondary Email: <input name='secEmail' type='text' id='secEmail' value='" . $_SESSION['currentUserSecondEmail'] . "' /><br />";
}
else {
echo "Secondary Email: <input name=secEmail type='text' id=secEmail value=''/><br />";
}
if (isset($_SESSION['currentUserEmail'])){
echo "Real Name: <input name='realName' type='text' id='realName' value='" . $_SESSION['currentUserRealName'] . "' /><br />";
}
else {
echo "Real Name: <input name='realName' type='text' id='realName' value=''/><br />";
}
if (isset($_SESSION['currentUserAvatar'])){
echo "Avatar: <input name='avatar' type='text' id='avatar' value='" . $_SESSION['currentUserAvatarURL'] . "'/><br />";
}
else {
echo "Avatar: <input name='avatar' type='text' id='avatar' value=''/><br />";
}
if (isset($_SESSION['currentUserSummName'])){
echo "Summoner Name: <input name='summName' type='text' id='summName' value='" . $_SESSION['currentUserSummName'] . "'/><br />";
}
else {
echo "Summoner Name: <input name='summName' type='text' id='summName' value=''/><br />";
}
if (isset($_SESSION['currentUserBday'])){
echo "Birthday: <input name='bday' type='text' id='bday' value='" . $_SESSION['currentUserBday'] . "'/><br />";
}
else {
echo "Birthday: <input name='bday' type='text' id='bday' value=''/><br />";
}
echo "<small>(Bday Format~ YYYY-MM-DD)<small>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "</form>";
}
Finally, this is where the error is thrown with the 2 $_SESSION variables. There doesn't seem to be any reason for this, thus why I've come to you all.
<?php
include 'dbstat_connect.php';
//include 'functions.php';
//echo $_SESSION['currentUser'] . "<br>";
//If stmt valid, prepare to insert profile info into `userAccount`
if ($stmt = $mysqli->prepare("INSERT INTO `stats`.`userAccount` values (". $_SESSION['currentUserAcctId'] . ", "
. $_SESSION['currentUser'] . ", ?, ?, ?, ?, ?, ?, NULL, NULL, NULL, NULL, NULL)")){
//Bind paramaters
if($stmt->bind_param('ssssss', $_POST['email'], $_POST['avatar'], $_POST['summName'],
$_POST['realName'], $_POST['bday'], $_POST['secEmail'])){
//Execute the query. If true, show proof. If false, display error.
if($stmt->execute()){
//Show proof of insertion
echo "<h4>Your data has been inserted.</h4>";
}
//Check if stmt returned an error.
else{
Print_r ($stmt->get_warnings());
}
//Close statement
$stmt->close();
}
//If $stmt statement returns an error, say so
else if(!$stmt){
printf ("Error: %s", $mysqli->error);
}
//Close DB Connection
$mysqli->close();
}
echo $_POST['email'];
?>
I really hope someone can help me. I've been stuck here for quite some time now.
Seems you forgot to call session_start(); in your last file ;)
$query = "SELECT * FROM secure_login.members WHERE username='" . $myusername . "' and password='" . $mypassword . "'";
this query is btw. unsafe and not protected against mysql-injections.
you should use the following:
$query = "SELECT * FROM secure_login.members WHERE username='" . $mysqli->real_escape_string($myusername) . "' and password='" . $mysqli->real_escape_string($mypassword) . "'";
On $myspassword the escape-function is probably unnecessary because this value is hashed.
Are you sure you have
session_start();
in all your files, related to this situation?
You MUST have it if you want to use $_SESSION variables through these pages.
i have a registration form for users to sign up to my site. all there details go to a mysql database like name etc.
When the information is recorded in the database i want on submit of the form for it to create a sequential user_id and store this along with the other details like name and age in the database.
What code would i need for this? sorry i am new to php.
I have tried using a cookie script like this but it won't create a user_id. instead all users in the database have a user_id of 0.
<?php
$user_id_set = get_user_id();
while ($user_id = mysql_fetch_array($user_id_set)) {
$cookie1 = "{$user_id["id"]}";
setcookie("ptb_registrations", $cookie1, time()+3600); /* expire in 1 hour */
}
?>
Heres my complete code:
<? ob_start(); ?>
<?php
// GET ACCOUNT INFORMATION FROM FORM AND ASSIGN VARIABLES
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$display_name = $_POST['display_name'];
$date_of_birth = $_POST['date_of_birth'];
$contact_number = $_POST['contact_number'];
$station = $_POST['station'];
$hobbies = $_POST['hobbies'];
$age = $_POST['age'];
$password = $_POST['password'];
?>
<?php
/*
// ECHO ACCOUNT INFORMATION
echo "<strong> Account Information: </strong>";
echo "<br />";
echo First Name: ";
echo "<br />";
echo $first_name;
echo "<br />";
echo "<br />";
echo "Last Name: ";
echo "<br />";
echo $last_name;
echo "<br />";
echo "<br />";
echo "Email: ";
echo "<br />";
echo $email;
echo "<br />";
echo "<br />";
echo "Password: ";
echo "<br />";
echo $password;
echo "<br />";
echo "<br />";
echo "date_of_birth: ";
echo "<br />";
echo $date_of_birth;
echo "<br />";
echo "<br />";
echo "Contact_number: ";
echo "<br />";
echo $contact_number;
echo "<br />";
echo "<br />";
echo "display_name: ";
echo "<br />";
echo $display_name;
echo "<br />";
echo "<br />";
echo "station: ";
echo "<br />";
echo $station;
echo "<br />";
echo "<br />";
echo "hobbies: ";
echo "<br />";
echo $hobbies;
echo "<br />";
echo "<br />";
echo "age: ";
echo "<br />";
echo $age;
echo "<br />";
echo "<br />";
*/
?>
<?php
////// SEND TO DATABASE
/////////////////////////////////////////////////////////
// Database Constants
define("DB_SERVER", "");
define("DB_USER", "");
define("DB_PASS", "");
define("DB_NAME", "");
// 1. Create a database connection
$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if (!$connection) {
die("Database connection failed: " . mysql_error());
}
// 2. Select a database to use
$db_select = mysql_select_db(DB_NAME,$connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
//////////////////////////////////////////////////////////////
$query="INSERT INTO ptb_registrations (ID,
first_name,
last_name,
email,
display_name,
date_of_birth,
contact_number,
station,
hobbies,
age,
password
)
VALUES('NULL',
'".$first_name."',
'".$last_name."',
'".$email."',
'".$display_name."',
'".$date_of_birth."',
'".$contact_number."',
'".$station."',
'".$hobbies."',
'".$age."',
'".$password."'
)";
mysql_query($query) or die ('Error updating database');
?>
<?php
function confirm_query($result_set) {
if (!$result_set) {
die("Database query failed: " . mysql_error());
}
}
function get_user_id() {
global $connection;
global $email;
$query = "SELECT *
FROM ptb_registrations
WHERE email = \"$email\"
";
$user_id_set = mysql_query($query, $connection);
confirm_query($user_id_set);
return $user_id_set;
}
?>
<?php
$user_id_set = get_user_id();
while ($user_id = mysql_fetch_array($user_id_set)) {
$cookie1 = "{$user_id["id"]}";
setcookie("ptb_registrations", $cookie1, time()+3600); /* expire in 1 hour */
}
?>
<?php include ('send_email/reg_email.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PlaytimeBoys Registration</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="container">
<h1>Thanks for Registering!</h1>
<h2>Please check your email to confirm your account has been set-up.</h2>
<p> </p>
<p> </p>
<p> </p>
</div>
</body>
</html>
<? ob_flush(); ?>
You don't need to handle this on PHP.
It's better to do it on the MySQL side.
So, on your Users table, field user_id must be defined with property AUTO_INCREMENT.
This way, you don't need to specify user_id parameter when inserting to this table.
Instead, user_id will be generated by the database, as integer, increasing each time by 1.
Try this:
CREATE TABLE Users (
user_id int(11) NOT NULL auto_increment,
first_name varchar(50),
last_name varchar(50)
PRIMARY KEY (user_id)
) ENGINE=InnoDB
You just need to update your database to do so.
you can modify the user_id to be auto increment by running this query
ALTER TABLE `tablename` MODIFY `user_id` INT( 10 ) NOT NULL AUTO_INCREMENT
and when ever you insert entry into your database then specify user_id as null such as this
INSERT INTO 'tablename' (user_id, name, address) VALUES (null, name_value, address_value)
I've been working on code for student registration. I finished coding for my log in.
What I'm having a problem with is showing the user's profile: student fname, lname, program, gender and year level in textboxes. I'm using MySQL as my back end.
My code is like this:
<?php
// connects to the database
$mysqli = new mysqli("localhost", "root", "");
$query = 'SELECT fname, lname, program, gender, year FROM students WHERE fname = '.$_SESSION['myusername'];
$mysqli->query($query);
echo "<div align=\"center\">";
echo "<br />Your <b><i>Profile</i></b> is as follows:<br />";
echo "<b>First name:</b> ". $_POST['fname'];
echo "<br /><b>Last name:</b> ".$_POST['lname'];
echo "<br /><b>Program:</b> ".$_POST['program'];
echo "<br /><b>Year:</b> ".$_POST['year'];
echo "<br /><b>Gender:</b> ".$_POST['gender'];
echo "</div>";
?>
This is my code for mainstudent.php and checklogin.php.
Here you go:
<?php
session_start();
// connects to the database
$mysqli = new mysqli("localhost", "root", "");
$query = "SELECT fname, lname, program, gender, year FROM students WHERE fname = '".$_SESSION['myusername']."'";
if($result = $mysqli->query($query))
{
while($row = $result->fetch_assoc())
{
echo "<div align=\"center\">";
echo "<br />Your <b><i>Profile</i></b> is as follows:<br />";
echo "<b>First name:</b> ". $row['fname'];
echo "<br /><b>Last name:</b> ".$row['lname'];
echo "<br /><b>Program:</b> ".$row['program'];
echo "<br /><b>Year:</b> ".$row['year'];
echo "<br /><b>Gender:</b> ".$row['gender'];
echo "</div>"
}
$result->free();
}
else
{
echo "No results found";
}
?>
please add your password in session as well