PHP - Need Help With Some Mysql Interaction
I'm trying to recreate a sort of image based ratings system. Example:
What I want to display is on the left. I've got a MySQL table with two fields: postid and rating. Each rating is a number between 1 and 15, they correspond to the rating icons. What I'm trying to do is go through all the entries with a specific postid, and list the amount of each rating, but I don't how to do so. Google hasn't been much help Similar Tutorialsso i work on wowroster.net making upgrades to roster i have created a user lib for the sit and im now adding sessions but im getten some odd issues.... this is one of the inserts and sent to mysql_query example Code: [Select] UPDATE `roster_sessions` SET `session_user_id` = '0', `session_last_visit` = '1331544818', `session_browser` = '', `session_ip` = '127.0.0.1', `session_time` = '1331545718', `session_page` = 'p=guild-main&a=g:1' WHERE `session_id` = '6m7js82r848kk2s90sjfmuj325' YET.. this is what i get in my database sql dump from my admin Code: [Select] INSERT INTO `roster_sessions` (`sess_id`, `session_id`, `session_user_id`, `session_last_visit`, `session_start`, `session_time`, `session_ip`, `session_browser`, `session_forwarded_for`, `session_page`, `session_viewonline`, `session_autologin`, `session_admin`) VALUES ('5764d5713a7f24c82b30d271460bf68c', '6m7js82r848kk2s90sjfmuj325', '3', 0, 1331544818, 1331545718, '127.0.0.1', '', '', 'addons-main-images-shadow', 0, 0, 0); any clue at all... I have tried for ages but can't get a LEFT JOIN to work on this. I want ALL table 1 records listed and done so any records in table 2 (that apply) are taken into account (and the deduction amount removed from wages). If month is 32 now, then the query would return: Dave Smith / 2000 Robert Brown / 2200 Table 1 "Employees" employeeDept / deptEmployeeRef / name / wage A / 2 / Dave Smith / 2000 B / 2 / Robert Brown / 2500 Table 2 "Deductions" monthNo / employeeDept / deptEmployeeRef / deduction 32 / B / 2 / 300 32 / C / 2 / 300 32 / A / 3 / 300 33 / B / 2 / 500 I have 2 tables and am trying to conduct an SQL query. I simply need to know how to use a JOIN when two fields in each table are needed to match the records up. I would rather not create another field in each table. I have put my query below which would work other than for what is obviously not possible ("ON xyz=xyz && abc=abc" etc.) Table 1 "Employees" employeeDept / deptEmployeeRef / name / wage A / 2 / Dave Smith / 2000 B / 2 / Robert Brown / 2500 Table 2 "Deductions" monthNo / employeeDept / deptEmployeeRef / deduction 32 / B / 2 / 300 32 / C / 2 / 300 32 / A / 3 / 300 33 / B / 2 / 500 Each department has employees starting from number 1 so two fields are needed in each table to match the records up. SELECT t1.name, (t1.wage - t2.deduction) FROM $tableName1 AS t1 LEFT JOIN $tableName2 AS t2 ON t1.employeeDept=t2.employeeDept && t1.deptEmployeeRef=t2.deptEmployeeRef WHERE t2.monthNo = '32' So this should return: Dave Smith 2000 Robert Brown 2200 "BACK" or REFRESH: Preventing database interaction / code execution how to prevent database interaction / code execution when user presses back or refresh button? can i detect? can i disable back/refresh? create table mimi (mimiId int(11) not null, mimiBody varchar(255) ); <?php //connecting to database include_once ('conn.php'); $sql ="SELECT mimiId, mimiBody FROM mimi"; $result = mysqli_query($conn, $sql ); $mimi = mysqli_fetch_assoc($result); $mimiId ='<span>No: '.$mimi['mimiId'].'</span>'; $mimiBody ='<p class="leading text-justify">'.$mimi['mimiBody'].'</p>'; ?> //what is next? i want to download pdf or text document after clicking button or link how to do that Hello everyone, Sorry if this has been answered but if it has I can't find it anywhere. So, from the begining then. Lets say I had a member table and in it I wanted to store what their top 3 interests are. Their$ row has all the usual things to identify them userID and password etc.. and I had a further 3 columns which were labled top3_1 top3_2 & top3_3 to put each of their interests in from a post form. If instead I wanted to store this data as a PHP Array instead (using 1 column instead of 3) is there a way to store it as readable data when you open the PHPmyadmin? At the moment all it says is array and when I call it back to the browser (say on a page where they could review and update their interests) it displays 'a' as top3_01 'r' as top3_02 and 'r' as top3_03 (in each putting what would be 'array' as it appears in the table if there were 5 results. Does anyone know what I mean? For example - If we had a form which collected the top 3 interests to put in a table called users, Code: [Select] <form action="back_to_same_page_for_processing.php" method="post" enctype="multipart/form-data"> <input name="top3_01" type="text" value="enter interest number 1 here" /> <input name="top3_02" type="text" value="enter interest number 2 here" /> <input name="top3_03" type="text" value="enter interest number 3 here" /> <input type="submit" name="update_button" value=" Save and Update! " /> </form> // If my quick code example for this form is not correct dont worry its not the point im getting at :) And they put 'bowling' in top3_01, 'running' in top3_02 and 'diving' in top3_03 and we catch that on the same page with some PHP at the top --> Code: [Select] if (isset($_POST)['update_button']) { $top3_01 = $_POST['top3_01']; // i.e, 'bowling' changing POST vars to local vars $top3_02 = $_POST['top3_02']; // i.e, 'running' $top3_03 = $_POST['top3_03']; // i.e, 'diving' With me so far? If I had a table which had 3 columns (1 for each interest) I could put something like - Code: [Select] include('connect_msql.php'); mysql_query("Select * FROM users WHERE id='$id' AND blah blah blah"); mysql_query("UPDATE users SET top3_01='$top3_01', top3_02='$top3_02', top3_03='$top3_03' WHERE id='$id'"); And hopefully if ive got it right, it will put them each in their own little column. Easy enough huh? But heres the thing, I want to put all these into an array to be stored in the 1 column (say called 'top3') and whats more have them clearly readable in PHPmyadmin and editable from there yet still be able to be called back an rendered on page when requested. Continuing the example then, assuming ive changed the table for the 'top3' column instead of individual colums, I could put something like this - Code: [Select] if (isset($_POST)['update_button']) { $top3_01 = $_POST['top3_01']; // i.e, 'bowling' changing POST vars to local vars $top3_02 = $_POST['top3_02']; // i.e, 'running' $top3_03 = $_POST['top3_03']; // i.e, 'diving' $top3_array = array($top3_01,$top3_02,$top3_03); include('connect_msql.php'); mysql_query("UPDATE members SET top3='$top3_array' WHERE id='$id' AND blah blah blah"); But it will appear in the column as 'Array' and when its called for using a query it will render the literal string. a r r in each field instead. Now I know you can use the 'serialize()' & 'unserialize()' funtcions but it makes the entry in the database practically unreadable. Is there a way to make it readable and editable without having to create a content management system? If so please let me know and I'll be your friend forever, lol, ok maybe not but I'd really appreciate the help anyways. The other thing is, If you can do this or something like it, how am I to add entries to that array to go back into the data base? I hope ive explained myself enough here, but if not say so and I'll have another go. Thanks very much people, L-PLate (P.s if I sort this out on my own ill post it all here) I have following piece of code below, and I would like to be able to express it pure SQL, something that could go into a .sql file :
$request_string='SELECT topic_forum_id FROM topic_table WHERE topic_id= 2014'; $query=database->prepare($request_string); $query->execute(); $data=$query->fetch(); $query->closeCursor(); $forum=$data['topic_forum_id']; $request_string='INSERT INTO post_table (post_topic,post_forum) VALUES (2014,:forum)'; $query=database->prepare($request_string); $query->bindValue(':forum',$forum,PDO::PARAM_INT); $query->execute(); $data=$query->fetch(); $query->closeCursor();Is it possible ? So i have this php as shown below. It should make a list of comments with comment replies below their comment respectively. The problem is that it only goes through and shows 1 comment and all the comment replies for that one comment. It should be showing all comments i have in the db for that article. If i remove the second while then it shows all the comments correctly but no comment replies then... How do i get this script to loop through the db for every comment but also loop through every comment reply for that $row[id]? If anyone has a better / more efficient way of what I am trying to do, please explain or show example (i am open to anything)... Code: [Select] // what article are we showing? $article_to_show_id = $_GET['article_id']; $active_is_set_text = "1"; // Active Column text that makes it okay to show // Finding the article $search_for_article = mysql_query("SELECT * FROM articles WHERE id = '$article_to_show_id' AND active = '$active_is_set_text'"); while($row = mysql_fetch_array($search_for_article)) { // format the last updated date right $update_date_edit = $row[update_date]; $update_date_edit = date('F j, Y \a\t h:ia', $update_date_edit); $row[update_date] = $update_date_edit; // format the submit updat date right $submit_date_edit = $row[submit_date]; $submit_date_edit = date('F j, Y \a\t h:ia', $submit_date_edit); $row[submit_date] = $submit_date_edit; echo ' <div> ', $row[title] ,' </div> <div> by: ', $row[author] ,' on ', $row[submit_date] ,' </div> <div> ', $row[content] ,' </div> <div> Last Updated: ', $row[update_date] ,' </div> <form action="article_reply.php" method="post"> <input type="hidden" name="article_id" value="', $row[id] ,'" /> <button name="article_reply" type="submit" value="submit">Reply</button> </form> '; } $comment_count = 0; $comment_reply_count = 0; // Finding all of the comments $search_for_article = mysql_query("SELECT * FROM article_comments WHERE article_id = '$article_to_show_id' AND reply_id = '0'"); while($row_comment = mysql_fetch_array($search_for_article)) { // format the submit updat date right $comment_date_edit = $row_comment[comment_date]; $comment_date_edit = date('F j, Y \a\t h:ia', $comment_date_edit); $row_comment[comment_date] = $comment_date_edit; echo ' <br> <br> COMMENT:<br> <div> By: ', $row_comment[username] ,' on ', $row_comment[comment_date] ,' </div> <div> ', $row_comment[comment] ,' </div> '; $comment_count++; // Finding all of the comment replies if any $search_for_article = mysql_query("SELECT * FROM article_comments WHERE article_id = '$article_to_show_id' AND reply_id = '$row_comment[id]'"); while($row_two = mysql_fetch_array($search_for_article)) { // format the submit updat date right $comment_date_edit = $row_two[comment_date]; $comment_date_edit = date('F j, Y \a\t h:ia', $comment_date_edit); $row_two[comment_date] = $comment_date_edit; echo ' <br> <br> COMMENT REPLY:<br> <div> By: ', $row_two[username] ,' on ', $row_two[comment_date] ,' </div> <div> ', $row_two[comment] ,' </div> '; $comment_reply_count++; } } Need some help I have 2 tables in a database and I need to search the first table and use the results from that search, to search another table, can this be done? and if it can how would you recommend that I go about it? Thanks For Your Help Guys! I have the code snippet below trying to check my database if a variable exists, it seems to work for the username but not for the email. The database name is "communitycouch_users" table is "reg_vars" to check the row: email. the variable $email is coming from my form. Upon form submission it will submit the data into the database regardless it exists. Code: [Select] $email= $_POST['email']; $username= $_POST['username']; // Check if the email and username exist already if(MysqlVarExists("communitycouch_users", "reg_vars", "email", $email) == true) { $errors_code["existing_email"] = "That email is already being used."; } if(MysqlVarExists("communitycouch_users", "reg_vars", "username", $username) == true) { $error_code["existing_username"] = "That username is already in use."; } Alrite, i can successfully connect to my server an db, open a connection, but when i retrieve data using Code: [Select] $sqlNAME = "select name from `".$table."` where id = '".$id."' "; $scriptName = mysql_query($sqlNAME) or die ("Failed to get Script Name because ".mysql_error()); the outcome is always something like 'Resource ID #5' (where #5 is a different number for each item) My main question is how would i retrieve the actual Name, and later on i will be retrieving the url and ID, and displaying both. My Database was set up using this SQL command: Code: [Select] CREATE TABLE `scripts` (`id` INT( 8 ) NOT NULL AUTO_INCREMENT , `name` VARCHAR( 12 ) NOT NULL , `url` VARCHAR( 100 ) NOT NULL , PRIMARY KEY ( `name` ) , INDEX ( `id` ) ); If you need any more info/code i'll be happy to give it. I am creating a website for university coursework.
<input name="submit" type="submit" class="submit_btn float_l" id="submit" formaction="add-contactme.php" value="Send" />
The above code is from my html for contact.html if that is correct. I am using a form and then a submit button.
I keep getting
Notice: Undefined index: name in C:\xampp\htdocs\hometown\hometown\add-contactme.php on line 11 Notice: Undefined index: email in C:\xampp\htdocs\hometown\hometown\add-contactme.php on line 12 Notice: Undefined index: phoneno in C:\xampp\htdocs\hometown\hometown\add-contactme.php on line 13 Notice: Undefined index: comments in C:\xampp\htdocs\hometown\hometown\add-contactme.php on line 14 Also if anyone could help me I would like to create a delete button after a person has added the values into the sql table, basically to the last row inputted into the database. I want to avoid using delete from contactme where name ='chris'; $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="contactme"; // Table name mysql_connect ("$host", "$username", "$password") or die ("cannot connect server "); mysql_select_db ("$db_name") or die ("cannot select DB"); $name = $_POST["name"]; $email = $_POST["email"]; $phoneno = $_POST["phoneno"]; $comments = $_POST["comments"]; $sql="INSERT INTO $tbl_name VALUES ('$name', '$email', '$phoneno''$comments')"; $result=mysql_query($sql); if($result){ echo "Successful" . " "; echo "view-contactme.php"; // link to view contact me page } mysql_close(); ?> Edited by G3NERALCHRIS, 20 November 2014 - 09:04 AM. Hi all! I am very new to php and mysql world so I could need some help. I found this UCP php code to control a database, where we could find various userinfo. I edited it and for unknown reasons everything else works except the change password. Here is the NewPassword.php: Code: [Select] <html> <body> <form action="Newpass.php" method="post"> <b><font size="4" color="#000080">Change your password! </font></b> <p>Old password <input type="password" name="P1" size="20" /> </p> <p>New Password <input type="password" name="P2" size="20" /></p> <p>Confirm New Password <input type="password" name="P3" size="20" /></p> <p> <input type="submit" /> <input type="reset" /> </p> <a href="Logged.php"">Go back</a> </form> </body> </html>And here is the Newpass.php Code: [Select] <?php session_start(); $pass1 = $_POST["P1"]; $pass2 = $_POST["P2"]; $pass3 = $_POST["P3"]; $escpass1 = mysql_real_escape_string($pass3); $escpass2 = mysql_real_escape_string($pass3); $escpass3 = mysql_real_escape_string($pass3); $user = $_SESSION['Username']; if(!isset($_SESSION['Username'])) { echo('You are not logged in!'); echo '<a href="index.php"">Go back</a>'; die; } $escuser = mysql_real_escape_string($user); include("database.php"); $query = "SELECT * FROM playerinfo WHERE user = '$escuser'"; $result = mysql_query($query); $row = mysql_fetch_row($result); if (!$con) { die('Could not connect: ' . mysql_error()); } $username_exist = mysql_num_rows($result); if($username_exist == 0) { echo('That username does not exist'); echo '<a href="NewPassword.php"">Go back</a>'; die; } if($escpass1 !== $row[1]) { echo("Wrong old password!"); echo '<a href="New Password.php"">Go back</a>'; die; } if($escpass2 !== $escpass3) { echo("Your new passwords do not match!"); echo '<a href="NewPassword.php"">Go back</a>'; die; } $query = "UPDATE playerinfo SET password = '$escpass3' WHERE user= '$escuser'"; $result = mysql_query($query); echo 'Password Changed!'; $_SESSION['Password'] = $escpass3; echo '<a href="Logged.php"">Go back</a>'; ?>And database.php Code: [Select] <?php $con = mysql_connect("edit","edit","edit"); mysql_select_db("edit"); //Remember that if you are using a external source, change the login info (mysql_connect(server[],user[],pass[]); ?>I edited the user/pw for obvious reasons. So the problem itself: Whatever I insert in password box I get this in return: Code: [Select] Warning: mysql_real_escape_string(): Access denied for user ''@'localhost' (using password: NO) in /home/crpnet/domains/c-rp.net/public_html/blankUCP/Newpass.php on line 6 Warning: mysql_real_escape_string(): A link to the server could not be established in /home/crpnet/domains/c-rp.net/public_html/blankUCP/Newpass.php on line 6 Warning: mysql_real_escape_string(): Access denied for user 'crpnet'@'localhost' (using password: NO) in /home/crpnet/domains/c-rp.net/public_html/blankUCP/Newpass.php on line 7 Warning: mysql_real_escape_string(): A link to the server could not be established in /home/crpnet/domains/c-rp.net/public_html/blankUCP/Newpass.php on line 7 Warning: mysql_real_escape_string(): Access denied for user 'crpnet'@'localhost' (using password: NO) in /home/crpnet/domains/c-rp.net/public_html/blankUCP/Newpass.php on line 8 Warning: mysql_real_escape_string(): A link to the server could not be established in /home/crpnet/domains/c-rp.net/public_html/blankUCP/Newpass.php on line 8 Warning: mysql_real_escape_string(): Access denied for user 'crpnet'@'localhost' (using password: NO) in /home/crpnet/domains/c-rp.net/public_html/blankUCP/Newpass.php on line 16 Warning: mysql_real_escape_string(): A link to the server could not be established in /home/crpnet/domains/c-rp.net/public_html/blankUCP/Newpass.php on line 16 That username does not existGo back in php you have the following mysql API fetch commands: ■mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both ■mysql_fetch_assoc — Fetch a result row as an associative array ■mysql_fetch_field — Get column information from a result and return as an object ■mysql_fetch_lengths — Get the length of each output in a result ■mysql_fetch_object — Fetch a result row as an object ■mysql_fetch_row — Get a result row as an enumerated array If u use one of them for example i use mysql_fetch_object. Code: [Select] $run = mysql_query(SELECT `status` FROM `[settings]` WHERE `name`= *); $display = mysql_fetch_object($run); other example Code: [Select] $run = mysql_query(SELECT `status` FROM `[settings]` WHERE `name`= 'abusing' AND `name`= 'hacking' AND `name`= 'cracking'); $display = mysql_fetch_object($run); When i do Code: [Select] $display->somethingIt wil assign the SELECT condition i want to assign the WHERE condition. the value of it. like i want to do this Code: [Select] $display->WHERE->hackinghow to do this? Hi all! I have racked my brain trying to figure this one out. I'm typically a smart cookie, but since I am kinda new to php and mysql, I'm stumped. I'm just trying to program a Joomla search plugin that queries my HDFLV player database, so videos are searchable, but I keep getting an invalid "foreach(" command. It's supposed to be there to interpret the database query, right? I'll post my code here, can someone give me some insight? Code: [Select] <?php /** * @version $Id: contacts.php 10381 2008-06-01 03:35:53Z pasamio $ * @package Joomla * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved. * @license GNU/GPL, see LICENSE.php * Joomla! is free software. This version may have been modified pursuant * to the GNU General Public License, and as distributed it includes or * is derivative of works licensed under the GNU General Public License or * other free or open source software licenses. * See COPYRIGHT.php for copyright notices and details. */ // no direct access defined( '_JEXEC' ) or die( 'Restricted access' ); $mainframe->registerEvent( 'onSearch', 'plgSearchhdflvplayer' ); $mainframe->registerEvent( 'onSearchAreas', 'plgSearchhdflvplayerAreas' ); JPlugin::loadLanguage( 'plg_search_hdflvplayer' ); /** * @return array An array of search areas */ function &plgSearchhdflvplayerAreas() { static $areas = array( 'hdflvplayer' => 'Videos' ); return $areas; } /** * Contacts Search method * * The sql must return the following fields that are used in a common display * routine: href, title, section, created, text, browsernav * @param string Target search string * @param string mathcing option, exact|any|all * @param string ordering option, newest|oldest|popular|alpha|category */ function plgSearchhdflvplayer( $text, $phrase='', $ordering='', $areas=null ) { $db =& JFactory::getDBO(); $user =& JFactory::getUser(); if (is_array( $areas )) { if (!array_intersect( $areas, array_keys( plgSearchhdflvplayerAreas() ) )) { return array(); } } // load plugin params info $plugin =& JPluginHelper::getPlugin('search', 'hdflvplayer'); $pluginParams = new JParameter( $plugin->params ); $limit = $pluginParams->def( 'search_limit', 50 ); $text = trim( $text ); if ($text == '') { return array(); } $section = JText::_( 'hdflvplayer' ); switch ( $ordering ) { case 'oldest': $order = 'a.id ASC'; break; case 'popular': $order = 'a.times_viewed DESC'; break; case 'alpha': $order = 'a.title ASC'; break; case 'category': $order = 'b.title ASC, a.title ASC'; break; case 'newest': default: $order = 'a.id DESC'; } $text = $db->Quote( '%'.$db->getEscaped( $text, true ).'%', false ); $query = 'SELECT a.title AS title, "" AS created,' . ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(\':\', a.id, a.alias) ELSE a.id END as slug, ' . ' CASE WHEN CHAR_LENGTH(b.alias) THEN CONCAT_WS(\':\', b.id, b.alias) ELSE b.id END AS catslug, ' . ' CONCAT_WS( ", ", a.title, a.description ) AS text,' . ' CONCAT_WS( " / ", '.$db->Quote($section).', b.title ) AS section,' . ' "2" AS browsernav' . ' FROM #__hdflvplayerupload AS a' . ' INNER JOIN #__hdflvplayername AS b ON b.id = a.catid' . ' WHERE ( a.title LIKE '.$text . ' OR a.description LIKE '.$text.' )' . ' AND a.published = 1' . ' AND b.published = 1' . ' AND a.access <= '.(int) $user->get( 'aid' ) . ' AND b.access <= '.(int) $user->get( 'aid' ) . ' GROUP BY a.id' . ' ORDER BY '. $order ; $db->setQuery( $query, 0, $limit ); $rows = $db->loadObjectList(); foreach($rows as $key => $row) { $rows[$key]->href = '?pid='.$row->slug; } return $rows; } ANY help is more than what I got from HDFLV Player support. Thank you so much in advance! Hi im trying to count the number of rows in my notifications My code So far list($total_server) = mysql_fetch_row(mysql_query("SELECT COUNT(id) FROM users WHERE notifications")); I'm having trouble working out a mysql query. I have two tables called "content" and "providers". Content Table content_id content_provider 1 Provider 1 2 Provider 2 3 Provider 1 Providers Table provider_name provider_url Provider 1 www.provider1.com Provider 2 www.provider2.com What I want to do is to select all of the information in the "providers" table but also to perform a count on the "content" table to get the number of content for each provider. So in this case it would show all of the information for provider 1 and also show that provider 1 has 2 pieces of content because they have 2 entries in the "content" table. I am able to get these results as two separate queries but I was wondering if it was possible to do it in one query. Thanks for any help. the code below isn't populating the mysql fields... i believe it's not running correctly at the while and for loop areas. also, would like to know of a way to have the 1000 to have the ability to look at the end of the row and then look at the next row of data, because there are sometimes +/-1000 characters in a row. can someone please assist? many thanks. <?php //connect to the database include "opendb.php"; #if the first row of csv file contains column headings: $columnheadings = 1; if (($handle = fopen("/my/file/destination/name.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); # echo "<p> $num fields in line $columnheadings: <br /></p>\n"; $columnheadings++; for ($i=0; $i<$num; $i++) { $line = trim($data[$i],'",'); $arr = explode(',',$line); //mysql insert of values $sqlData = ("INSERT INTO thisTable(name1, name2, name3, name4, name5) VALUES('".$arr[0]."', '".$arr[1]."', '".$arr[2]."', '".$arr[3]."', '".$arr[4]."')"); } } fclose($handle); } else { mysql_query($sqlData) or die(mysql_error("The specified file does not exist. Thanks.")); } mysql_close($con); ?> I got a new laptop running Windows 7 and wanted to develop some PHP coding for test purposes.
I want the data I test to go into MySQL.
What do I need to do and where can I get the appropriate downloads to get started with the most basic of set-ups?
|