how do I uncheck values from my database? - php

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);

Related

php-sql delete value if exists in one of my two tables

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?

How do I ensure the value only gets saved once into the table, when checked?

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, when the user clicks 'Save'.
But I want a checked value to be only saved
once into the table, the way it is now it gets saved multiple times - I mean if the user comes back to this page and the name is checked and the user clicks 'Save' again.
What can I add to the code for this to happen, so it will only be saved once in my review_shared table ? At present it looks like this, which saves it multiple times :
if(!empty($_POST['check_contacts'])) {
foreach($_POST['check_contacts'] as $check) {
//$_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);
}
//go to the main page when changes have been saved
header('Location:volleyLogin.php');
}
$con->close();
I thought adding code like this into the above would help, but it doesn't, really :
if(!empty($_POST['check_contacts'])) {
//************************* added this in **************
$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";
break;
}
//*******************************************************
foreach($_POST['check_contacts'] as $check) {
//$_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);
}
//go to the main page when changes have been saved
header('Location:volleyLogin.php');
}
$con->close();
break; is used to come out of a loop(for, foreach,while and do-while) and switch structure, not from if block. You have to create an else block corresponding to that if block, and put your entire foreach loop there.
// your code
if($num_rows) {
echo "This is already a contact";
}else{
foreach($_POST['check_contacts'] as $check) {
//$_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);
}
}
// your code
Here's the relevant reference:
http://php.net/manual/en/control-structures.break.php

How to access a column in a row using PHP

This may be a noob question. I have a table called user_thoughts. I am making a Delete button, which on click, will delete the "thought". I have two rows in my table:
Row 1:
id - 106
message - This is message 1
added_by - AliceP
Row 2:
id - 107
message - This is message 2
added_by - AliceP
Here is what I have so far (del_post.php):
<?php include("connect.php"); ?>
<?php
session_start();
if (!isset($_SESSION["user_login"])) {
header("Location: index.php");
} else {
$username = $_SESSION["user_login"];
}
/**************************/
// get id of all posts by logged in user
$get_id = "SELECT id FROM user_thoughts WHERE added_by ='$username'";
$run_query = mysqli_query($connect, $get_id);
while ($row_query = mysqli_fetch_array($run_query)) {
$tho_id = $row_query['id'];
$comment_assigned_to_id = ['message'];
$delete_query = mysqli_query ($connect, "DELETE FROM user_thoughts WHERE id = '$tho_id' '");
header ("Location: /profile_page/$username");
}
?>
If I want to delete the thought This is message 1, I click delete, and it will delete all the messages by AliceP. How can I get the id of the current message, and then delete just that message?
Step 1:
At the bottom of your script, create a form, and within the form (using results of the SELECT statement in a loop):
display the row
create a button with the name="delete" and the value=id (id of the row)
Step 2:
At the top of your script, add the code that will (when the form is submitted) extract the value of $_GET["delete"] or $_POST["delete"] (depending on whether you are using GET or POST form method).
If there is no value, skip the deleting portion.
Create & execute the delete statement based on the extracted value of the row id.

Fetching rows of data from a table that isn't in another table

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`)

Help with logic

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);

Categories