I have been trying to insert a row into my table in my database, I couldnt accomplished insert query also I cant get any errors so I dont know what is the problem
my table:
CREATE TABLE IF NOT EXISTS `general_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`First Name` varchar(30) NOT NULL,
`Last Name` varchar(30) NOT NULL,
`University` varchar(80) NOT NULL,
`Major` varchar(50) NOT NULL,
`Current_Level` varchar(50) NOT NULL,
`Date_Joined` varchar(15) NOT NULL,
`Current_Team` varchar(50) NOT NULL,
`added_by` int(11) NOT NULL,
`Phone` varchar(25) NOT NULL,
`Email` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
KEY `member_id` (`added_by`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
my html :
<body>
<div class="memberInfo">
<h1>Add A Member</h1>
<form id=add_member name="add_member" method="post" action="addmember.php" >
<fieldset>
<legend>Member Details</legend>
<ol>
<li>
<label for=first_name>First Name</label>
<input id=first_name name=first_name type=text placeholder="First Name" required autofocus>
</li>
<li>
<label for=last_name>Last Name</label>
<input id=last_name name=last_name type=text placeholder="Last Name" required autofocus>
</li>
<li>
<label for=email>Email</label>
<input id=email name=email type=email placeholder="example@domain.com" required>
</li>
<li>
<label for=university>University</label>
<input id=university name=university type=text placeholder="Eg.Istanbul University" required>
</li>
<li>
<label for=major>Major</label>
<input id=major name=major type=text placeholder="Eg. Business" >
</li>
<li>
<label for=phone>Phone</label>
<input id=phone name=phone type=tel placeholder="Eg. +447500000000" >
</li>
<li>
<label for=current_stage>Current Stage</label>
<input id=current_stage name=current_stage type=text placeholder="Eg.Newbie" required>
</li>
<li>
<label for=current_team>Current Team</label>
<input id=current_team name=current_team type=text placeholder="Eg.External Relations">
</li>
<li>
<label for=date_joined>Date Joined</label>
<input id=date_joined name=date_joined type=text placeholder="Eg. 08/24/2012" required>
</li>
</ol>
</fieldset>
<fieldset>
<button type=submit >Save Member</button>
</fieldset>
</form>
</div>
</body>
How i retrieve $_SESSION[‘user_id’] in my login php
if($stmt->num_rows==1){
echo $myusername . " ";
session_register("myusername");
session_register("mypassword");
$_SESSION['user_id']=$user_id;
$_SESSION['username'] = $myusername;
session_write_close();
header("location:login_success.php");
}
addmember.php:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
if(isset( $_SESSION['user_id'])){
$mysqli = new mysqli('localhost', 'root', 'password', 'aiesec');
if(mysqli_connect_errno()) {
echo "Connection Failed: " . mysqli_connect_errno();
exit();
}
$stmt = $mysqli -> prepare("INSERT INTO general_info (First Name, Last Name, University, Major, Current_Level, Date_Joined, Current_Team, added_by, Phone, Email) VALUES (?,?,?,?,?,?,?,?,?,?)")
$stmt -> bind_param("sssssssiss", $First_Name, $Last_Name,$University,$Major, $Current_Level, $Date_Joined, $Current_Team, $added_by, $Phone, $Email);
$First_Name=$_POST['first_name'];
$Last_Name=$_POST['last_name'];
$University=$_POST['university'];
$Major=$_POST['major'];
$Current_Level=$_POST['current_stage'];
$Date_Joined=$_POST['date_joined'];
$Current_Team=$_POST['current_team'];
$added_by=$_SESSION['user_id'];
$Phone=$_POST['phone'];
$Email=$_POST['email'];
$stmt -> execute();
printf("%d Row inserted.\n", $stmt->affected_rows);
printf("Errormessage: %s\n", $stmt->error);
$stmt->close();
$mysqli->close();
}
else{
printf("Nope\n");
header("location:main_login.php");
}
?>
If I remove all the code and put echo to see $_SESSION['user_id'] output is 1 so if statement works.
I guess problem line might be $added_by=$_SESSION['user_id']; but I am not sure.
How can I fix insert or How can I see the error?
Thanks in advance for any suggestions.
—–EDIT SOLUTION——-
Simplest mistake of all times
I have changed First Name and Last Name to First_Name and Last_Name
I forgot to put semicolon at end of the line
$stmt = $mysqli -> prepare("INSERT INTO general_info (First Name, Last Name, University, Major, Current_Level, Date_Joined, Current_Team, added_by, Phone, Email) VALUES (?,?,?,?,?,?,?,?,?,?)")
after putting semicolon script added the row to the database
you have an errors in your code
look on
you need to have an underscore in variables name like:
look in the manual