[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Hey, /g/. I'm trying to create an SQL function that makes
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 12
Thread images: 1
File: php-code-debugging.jpg (84 KB, 930x428) Image search: [Google]
php-code-debugging.jpg
84 KB, 930x428
Hey, /g/. I'm trying to create an SQL function that makes it so you can insert information into a table but only when you are admin. The problem is, my admin 'userid' is in my 'users' table, and the information I want to be able to insert is in the 'comments' table, so when I try to post something it says I cannot because of a foreign key confliction. tldr - is there a way to reference multiple tables in one SQL statement? My latest unsuccessful attempt at this is as follows - ""INSERT INTO `comments` (`title`, `post`, `postdate`) VALUES ('$title', '$post', CURRENT_TIMESTAMP) REFERENCES `users` (`userid`)";"
>>
>>54353386
what is running the SQL transaction? php? who is php connecting as? is the admin user a mysql server user or an arbitrary user you created for your application?
>>
>>54353470

PHP. The first user I created I named "Admin" with an ID of 2 and I'm writing the rest of my code so that certain things can only be done if the admin with an ID of 2 is logged in (eg, ($_SESSION['userid'] == 2)).
>>
>>54353495
well you could create a VIEW. and then update data by calling your view
https://dev.mysql.com/doc/refman/5.7/en/view-updatability.html
>>
>>54353530

Hmm i'll look into that but it seems like there could be a different way
>>
>>54353386
You should create a user specifically with read and write permissions for that table (create update, delete, etc)
Then just login with that user and perform operations. Other users will get permission errors.
>>
>>54353573
i still dont understand what you are asking
>>
>>54353621

I am creating a project where one user (admin) can create posts, delete posts, and update posts. Other users can register for this site and leave comments on posts but they cannot create posts, delete posts, or update posts. The first user I registered on the site is called "admin" and I am using sessions to make it so only the admin user with a certain userid can post/update/delete. The problem I am having is the posts go to my 'posts' table, but my admin userid is stored in my 'users' table. So when I try to post with the admin userid there is a foreign key confliction because 'userid' is primary key in the 'users' table, and I am trying to submit information using that id to the 'posts' table. I'm trying to figure out a function to reference the users table, check if the userid is the admin userid, and if it is, allow posts into the 'posts' table.
>>
>>54353712

I don't quite get what you are saying, but I highly recommend thinking about your database design..

Generally you should FIRST check the user and THEN make him doing things..

 <?php

if (!isset$_SESSION["nickname"]) {

echo "Please login first!";

} else {

$nick = $_SESSION["nickname"];
$sql = "SELECT usergroup FROM user WHERE nickname = '$nick'";
$myquery = mysql_query($sql);
$row = mysql_fetch_assoc($abfrage);

if ($row["usergroup"] == "1") {
include "adminarea.php";
} else {
echo "You can't delete posts: access denied!";
}
}
?>
>>
>>54355158

Sorry, this code has some minor errors, I copied it from two differnt places.

But you should see what I mean..
>>
Check the user permissions from the script, not the statement, and hard coding things to only work when you have a certain userID is shit design.
>>
>>54355220
Also >>>/reddit/
Thread replies: 12
Thread images: 1

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.