In our system they asked us to add research interest but when we add new user, we should assign an interest to him/her. It's adding successfully to the database table, we have problem which is we get zero record in the interest_id database table, why?
Here all research interest added successfully to database:
As you see in picture below when admin add a new user he chooses multiple interests and assign them to a user:
Here is screenshot of user profile as you can see interests that admin assigned to new user:
The problem is that it don't write interest id to database?
Below you can see the codes and SQL table:
<p>
<label>Research Areas</label>
<select name="interest_id[]" class="small-input" multiple>
<?php
$query = "SELECT * FROM research_interest ORDER BY name ASC";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_object($result))
{
?>
<option value="<?php echo $row->id; ?>"><?php echo $row->name; ?></option>
<?php
}
?>
</select>
</p>
Here is also codes for MySQL:
if(isset($_POST['action'])) {
switch($_POST['action']) {
case 'user_add':
$user_id = mysqli_real_escape_string($link,$_POST['user_id']);
$email = mysqli_real_escape_string($link,$_POST['email']);
$password = rand_pass(8);
$title_id = intval($_POST['title_id']);
$user_type_id = intval($_POST['usertype_id']);
$department_id = intval($_POST['department_id']);
$view_publication = mysqli_real_escape_string($link,$_POST['publication']);
$referee = mysqli_real_escape_string($link,$_POST['referee']);
$gender = mysqli_real_escape_string($link,$_POST['gender']);
$creation_date = date('Y.m.d');
//Interest
if(!empty($_POST['interest_id']) && is_array($_POST['interest_id'])) {
foreach ($_POST['interest_id'] as $interest)
{
$query = "INSERT INTO user_interest (user_id, interest_id)
VALUES ('$user_id', '$interest')";
$result = mysqli_query($link,$query);
}
}
//SQL Query
$query = "INSERT INTO user (user_id, email, title, user_type_id, department_id, referee, gender, password, view_publication, creation_date)
VALUES ('$user_id', '$email', '$title_id', '$user_type_id', '$department_id', '$referee', '$gender', '$password', '$view_publication', '$creation_date')";
$result = mysqli_query($link,$query);
echo '<script type="text/javascript">';
echo 'window.location = "mail.php?action=user_mail&user_id='.$user_id.'"';
echo '</script>';
break;
}
}
And this is a user_interest table shows that these three research IDs assign to user 1
but here it show that interest_id is zero , it don't shows ids of research interest that added to user 1
You are not including interest_id in your Insert query to the users table. So the table is assigning a default value of 0.
You're not inserting anything into the "interest_id" column in the second query so it's defaulting to zero. It looks like you're trying to set up a many to many relationship (one user can have many interests and one interest can belong to many users). In that case you'll need to set up a junction table (http://en.wikipedia.org/wiki/Junction_table) to handle the relationship.
Related
So I am trying to write a php script that records one vote increment per user. Voters a are presented with a list of prospective candidates then clicks once on their name.
Now on attempt of a second time i hope to get a notification allowing this action. Please help.
<?php
if(isset($_POST['vote']))
{
$sql3='INSERT INTO sessions (memberID, postid, email, voted) VALUES ("","", "", 1;)';
$result3 = mysqli_query($con, $sql3);
}
// $count = mysqli_num_rows($result2);
$candidate_name = null;
$vote = $_POST['vote'];
mysqli_query($con, "UPDATE tbCandidates SET candidate_votes=candidate_votes+1 WHERE position_id='$vote'");
// $count = mysqli_num_rows($result2);
if ( $count<1) {
//$sql3='INSERT INTO sessions (memberID, postid, voted) VALUES ("", memberbers.memberID,"1")';
//$result = mysqlI_query("select id from Users where username ='".$_SESSION['email']."'");
//$result = mysqli_query($con, $sql3);
// $count = mysqli_num_rows($result2);
} else {
echo"You have voted already";
}
Put a "unique" flag on your memberID column in your SQL table. In that case when you try to insert a new row with the same memberID, it will not work.
Then catch the SQL write fail, and display a message if it gets to it.
I'm trying to insert multiple records from a text box into a mysql table.
If I enter more than 2 records, it's inserting duplicates.
Here's the code.
What am I doing wrong?
Some more info
Table info
id (int) primary auto_increment
email (varchar)
imei (varchar)
date_ordered (datetime)
status(varchar)
Since it's only the beginning, I have no problems with changing the table structure.
$email = $_SESSION['email'];
$now = time():
$status = "1";
$imeis = $_POST['imeis'];
$row = explode("\n", $imeis);
foreach ($row as $key => $imei)
{
$imei = mysql_real_escape_string($imei);
$query = "SELECT * FROM orders WHERE imei = '$imei'";
$result = mysqli_query($link, $query);
if (mysqli_num_rows($result) == 0)
{
$query1 = "INSERT IGNORE INTO orders (email, imei, date_ordered, status) VALUES ('$email', '$imei', '$now', '$status')";
$result1 = mysqli_query($link, $query1) OR die("fml");
if ($result1)
{
echo "Added $imei<br>";
}
}
else
{
echo "<B>$imei</B> already there<br>";
}
}
It looks like you want each value of imei to be unique in your table. It;s a guess, but it looks like what you're doing.
You do that in SQL by defining the imei column to be unique: that is, by creating a unique index on it. Your present table has an id as a unique (primary) key. You can easily create another one.
I have created two tables, employee and department. Now, in the department table, I assigned a foreign key and a PK is assigned for the employee table. If view data, Ii can join the table.
How to write the query in PHP for insert and update?
My query:
?php
include "connect.php";
if(isset($_POST["submit"]))
{
$fname = $_POST["fname"];
$dept = $_POST["dept"];
$result = mysqli_query($mysqli, "INSERT INTO employee ( fname, department)
(SELECT fname, department FROM employee LEFT JOIN department ON employee.id=department.dept_id) values ( '$fname', '$dept')");
if($result)
{
echo "<script>alert('New employee register successfully!')</script>";
echo "<script>window.open('register.php','_self')</script>";
}
else
{
echo "<script>alert('something went wrong!')</script>";
}
}
?>
$result = mysqli_query($mysqli, "INSERT INTO employee (`fname`, `dept`)
VALUES ('$fname','$dept')");
if($result)
{
echo "<script>alert('New employee register successfully!')</script>";
echo "<script>window.open('register.php','_self')</script>";
}
// You Forget ( `` ) sign in values
I was wondering how you can decrease a seating value using php/mysqli?
I have setup a basic sessions timetable with customer registration and I wish to display the number seats left (or maximum seating) and have it decrease with each registration of a customer.
Thanks!
I have created a seating row in topics and have a page that displays speakers with topics and session times.
below is the registration php code currently.
if (isset($_POST['Name'])) {
$Name = mysqli_real_escape_string($con,$_POST['Name']);
$Address = $_POST['Address'];
$Phone = $_POST['Email'];
$Email = $_POST['Phone'];
$sql = "insert into registration (Name, Address, Email, Phone) values ('$Name','$Address','$Phone','$Email')";
//echo $sql;
$result2 = mysqli_query($con,$sql);
// get id from last query insert statement auto increment
$RegistrationID = mysqli_insert_id($con);
foreach($_POST['time'] as $sessionsID){
echo $sessionsID;
$sql = "insert into bookings (sessionsID, RegistrationID) values ($sessionsID, $RegistrationID)";
echo $sql;
mysqli_query($con, $sql);
}
<?php
$sql3 = "select Time, SessionID FROM sessions LIMIT 0, 30 ";
$result = mysqli_query($con,$sql3);
while ($row = mysqli_fetch_assoc($result)) {
// $i++;
echo '<input type="checkbox" value='.$row['SessionID'].' name="time[]">'.$row['Time'].'</label>';
}
?>
Here are the steps I'd take:
Create a table (or add to an existing one, depending your DB structure) with a column that contains the number of seats.
Call it from the db into a variable, eg. $seat, at every registration.
Make it $seat--; (decrement by one), or just $seat = $seat - 1;.
Update the DB with the new value.
I do not see you using seat count anywhere. When you have inserted an entry for a ticket confirmed at the same time you have to update the seat count available by subtracting the no of seat booked.
For easy implementation do like this:
Select count of ticket booked.
Update the remaining seat count by total-booked. Do it for each insert of a ticket booked.
Good day does anyone can help me about my problem?
i have a signup_form all the data inputted will insert to my registrationtbl, my problem is the code below just delete the data from registrationtbl. how can i move the data to the userstbl and remove it from the registrationtbl? thank you . hope someone may help me..
the code is also came from one of the topics here..
<?php
$con=mysqli_connect("localhost","root","","scouts");
if ( isset($_GET['id']) ) {`enter code here`
$id = $_GET['id'];
$result = mysqli_query($con,"SELECT * FROM registration WHERE id='$id'");
while($row = mysqli_fetch_array($result))
{
echo $row['idno'] . " " . $row['surname'];
echo "<br>";
}
$row = mysqli_query($con,"INSERT INTO users (surname, firstname, middlename, gender, idno, password, signup_date) 'SELECT * from registration WHERE id =$id");
$sql = mysqli_query($con,"DELETE FROM registration WHERE id='$id'");
echo "its deleted";
}
?>
why are you using 2 tables:
simply use users table only, and add a status field to the table, when user registered the status may be 0, when registration approved change status to 1... so there is no need for 2 tables...
or if u have to use both tables, my suggession is the use of a database trigger to insert value to the second table from 1st.