I have created a chat system where students and academic staff can communicate between them. I have developed a form were I can get the name of the club were the conversation exists, and the value of the staff_id for another reason.
The problem is that I can not delete the records from 3 tables. I know that first I have to check if a specific id exists in staff_table or stu_table but I cannot figure out the query that I have to use.
The first table is the staff_message (columns: staff_id, m_id), the second is the stu_message (columns: stu_id, mid) and the third is the message table (columns: id, text, date, type).
Here you can see my code:
<?php
include_once('connect.php');
$name=mysqli_real_escape_string($con,$_POST['clubname']);
$staff_id=mysqli_real_escape_string($con,$_POST['staff_id']);
if(empty($name)) {
header("Location: ../options.php?index==empty");
exit();
}
else {
$quer = mysqli_query($con,"SELECT id FROM message WHERE type='$name'");
while($row = mysqli_fetch_array($quer)) {
$message_id = $row['id'];
}
$query4 = mysqli_query($con,"SELECT * FROM staff_message WHERE m_id='$message_id'");
if(mysqli_num_rows($query4) >= 1)
{
$query5 = mysqli_query($con,"DELETE FROM staff_message WHERE m_id='$message_id'");
} else {
echo "not here staff"; //see if finds the value
}
$query8 = mysqli_query($con,"SELECT * FROM stu_message WHERE mid='$message_id'");
if(mysqli_num_rows($query8) >= 1)
{
$query5 = mysqli_query($con,"DELETE FROM stu_message WHERE mid='$message_id'");
} else {
echo "not here stu";//see if finds the value
}
$query= mysqli_query($con,"DELETE FROM message WHERE type = '$name'");
header("Location: ../options.php?delete==success");
}
?>
Thank you in advance!
EDIT: I tested each code separately and I saw that they both work. What I mean is that when there is only one row of student message then the message is deleted as well as the row in reference table (stu_message). That also applies for the academic staff table.
May Be
You Have 3 Tables
1.Staff_Message
2.Stu_Message
3.Message
can you tell me which is the main id and which id refrence in tables then i can make query according your requirement
According To Your Image Your Base Table Is Message Then Mid Refer In Two Table(stu_message and staff_message)--- Right?
Related
With my code here, if the user checks a check box beside one of the names
the name gets saved into my review_shared table.
I'm trying to have my code so that if a box is unchecked, the name will be REMOVED from the review_shared table.
Does you know how I can do this, or where I should start ? So, If Paul Thompson and/or Dan Frain are unchecked, they will be removed from my review_shared table.
<?php
//here we want to save the checked contacts to the review_shared table ; that is,
//who the user wants to share reviews with
if(!empty($_POST['check_contacts'])) {
foreach($_POST['check_contacts'] as $check) {
//$check="";
//if the contact in review_shared is already checked, we don't want to save it multiple times
$already_checked = "SELECT * from review_shared WHERE user_id = '$user_id' AND contact_id = '$check' AND review_id = " .$_GET['id'];
$already_checked_result=mysqli_query($con,$already_checked);
$num_rows = mysqli_num_rows($already_checked_result);
if($num_rows >= 1) {
echo "This is already a contact";
}
else if ($num_rows < 1) {
//$_GET['id'] is the current review for which contacts are being edited, we are checking a contact to share that review with
$insert_review_shared_command = "INSERT INTO review_shared VALUES(NULL," .$_GET['id']. ", '$user_id','$check')";
//we want to save the checked contacts into the review_shared table
$insert_into_review_shared_table = mysqli_query($con,$insert_review_shared_command);
}
}
}
$con->close();
?>
The unique way to achieve what you need is to
delete all the entries
insert the checked options
While saving your form data.
To delete all the previous entries do something like this
$sql = "DELETE FROM review_shared WHERE your-conditions";
This because when you are posting checkboxes, you will receive ONLY the checked ones on the server side. Those ones are the data you need to store.
Thanks to Simone Cabrino above, I simply added this into my code :
$already_checked = "DELETE from review_shared WHERE user_id = '$user_id' AND review_id = " .$_GET['id'];
mysqli_query($con,$already_checked);
It's been long that I've been encountering this problem and I'll be glad if you guys can help me out on what to do.
This is the illustration.
Take for instance, you have a table called matrics and the table has a column called 'matric_num'.
Now, you create a registration page for users where one of the field is to select any matric num and after the user might have selected a matric number, filled the rest of the form and click on the submit button, it should insert the details into database.
If another user want to register, the matric number that the previous user selected should not be listed in the option menu.
HERE is what I did.
$query = mysqli_query($con, "SELECT *FROM `users`");
$rows = mysqli_num_rows($query);
echo "<select>";
for($i=1;$i<=$rows;$i++)
{
$fetch_user_matric = mysqli_fetch_array($query);
$matric_num = $fetch_user_matric['matric_num'];
$matric_query = mysqli_query($con, "SELECT *FROM `matrics` WHERE `matric_no` <> '$matric_num'");
$matric_rows = mysqli_num_rows($matric_query);
for($j=1;$j<=$matric_rows;$j++)
{
$fetch_matric_num = mysqli_fetch_array($matric_query);
$matric_no = $fetch_matric_num['matric_no'];
echo "<option value='$matric_no'>$matric_no</option>";
}
}
echo "</select>";
It displayed only the first matric number from the database whereas all matric numbers that hasn't been used by any user should be displayed.
Please what do you think is wrong with the sql query.
THANKS.
Just try this query:
SELECT * FROM `matrics` WHERE `matric_no` not in (select `matric_num` from `users`)
i am trying to learn PHP and MYSQL. I have created a database in phpmyadmin which has the following attributes:
database name = my_database
table name = users
The table has:
attributes (id,Name, Password, Phone Number)
A pictorial example of what I have looks like:
The problem is I wrote a query in php that would select all the data from the table and display it using a loop. For some reason no matter how much data I add in my users table, the code always omits the first row of data and displays the rest of the users data.According to my code, the output should be each user name displayed with their id number, but the information of the first user is never shown, and the rest of the users are shown perfectly.Can someone please help me regarding this.
my code:
<?php
$counter=0;
$mysqli=mysqli_connect('localhost', 'root','','my_database');
if(mysqli_connect_errno())
{
echo'connection failed'; echo"<br>";
}
else{
echo"connection SUCCESSFUL";echo"<br>";
}
$sq1="select * from users";
//$sq1="SELECT `Name`, `Password` FROM `users` ORDER BY `id`";
$res= mysqli_query($mysqli,$sq1);
if($res)
{
echo"Database Query Successful";echo"<br>";
$user_array= mysqli_fetch_array($res);
//print_r($user_array);
while($user_array=mysqli_fetch_assoc($res))
{
$user_id=$user_array["id"];
$user_name=$user_array["Name"];
//$user_phonenumber=$user_array["Phone Number"];
echo'The user name is '.$user_name.' The id is '.$user_id;
}
}
else
{
echo"Databse unsuccessful";
}
?>
That's because you called mysqli_fetch_array before entering the loop , remove the following line:
$user_array= mysqli_fetch_array($res);
Quite a complex question:
At the moment I have a table called "timetable". This is updated when someone books a slot (this is for a radio booking system). What I would like is a PHP file that I can have run every 15 minutes as a cron job. In PHP script what I'd like it to do is count how many slots a radio presenter has booked via the "username" field in the "timetable" table. Then I would like it to update a field in another table called "users" in a field called "slot_count" with the amount of slots which were found in the "timetable" table under their "username".
At the moment I have a script which pulls all the booked slots with their presenter "username"'s into a table:
<?php
include("../config.php");
include("functions.php");
if($logged["level"] == "HDJ" OR $logged["level"] == "SA") {
echo "<table width=\"580px\" class=\"board\" border=\>";
$order = "SELECT * FROM timetable WHERE username <> 'No DJ'";
$result = mysql_query($order);
// Error checking
if (!$result) {
// output error, take other action
}
else {
while ($row=mysql_fetch_array($result)){
// Append all results onto an array
$rowset[] = $row;
}
}
foreach ($rowset as $row) {
echo "<tr><td>" . htmlspecialchars($row['username']) . "</td></tr>";
}
} else {
echo ("<div class=\"caution\">Access is denied.</div>");
}
?>
Any ideas?
Storing this redundant data in your users table is unnecessary. As long as the tables are indexed appropriately the join and count are trivial -
SELECT users.*, COUNT(timetable.username) AS num_slots
FROM users
LEFT JOIN timetable
ON users.username = timetable.username
GROUP BY users.id
Can't it be done using one sql statement doing both COUNT() and UPDATE:
UPDATE users
SET slot_count = (SELECT COUNT(*) FROM timetable WHERE timetable.username = users.username)
Assumption: the username field contains the same value for the same radio presenter in both users and timetable tables. Otherwise they wouldn't match. You should be able to run this query directly against MySQL from the cron job (instead of doing the PHP script).
I have created one field unique in my database. Consider the album name is unique. I have successfully added constraints that if user try to add other album with that name he will get error message that record already present. So user may not add any other record with the same name.
But now I have problem that if their are 3 records exists in database with name a,b,c. and user is updating any of field than he can update record b as name of a so their would be two records in database with a. & I want to create it unique. For that I am using script..
if($name!="") {
$sql = "select a_name from t_a_list where a_name = '$name'";
$result = mysql_query($sql);
if(mysql_num_rows($result) != 0) {
$msg = "<font color=red>Record Not Saved. Album already Exist!</font>";
}
} else {
$msg = "<font color=red>Enter Name of Album!</font>";
}
if($msg=="") {
//Update Query
}
But I have problem that with each pageload for updation I need to choose different name for album. because it will show album exist msg. Sometimes it might be possible that I want to change rest of information other than name of album like its year of release, total track. but with this script If I want to update rst of field I need to change album name first. Please help with this problem. So that album name may not repeat in DB and I can update other information also other than name.
You could save the oldname in a variable and when updating first check for if($name!=$oldname) { ... .
this way you won't update the name unless it changed and the updates for the other fields will be made
$oldname = THE NAME OF THE ALBUM BEING EDITED
....
if($name!=$oldname) {
if($name!="") {
$sql = "select a_name from t_a_list where a_name = '$name'";
$result = mysql_query($sql);
if(mysql_num_rows($result) != 0) {
$msg = "Record Not Saved. Album already Exist!";
}
} else {
$msg = "Enter Name of Album!";
}
if($msg=="") {
//Update Query
}
}
if($description!="") {
....
}
if($location!="") {
...
}
...
You need to enforce the uniqueness constraint at the database level. This way, the database will reject any operation that will create records with identical keys.
One way to do this for an existing table is using the ALTER TABLE command:
ALTER TABLE myalbums ADD UNIQUE (albumname);