PHP - Php To Mysql Database Back To Site
Ok so I have this site where I'm posting a message to my Database. But when I go to pull the message from the MYSQL DB it's one long sentence. Any suggestions?
Similar TutorialsSay I have the following text stored in a MySQL database... Code: [Select] <b>Classic Quote from movie</b> and I retrieve it into a variable called $text, how do I properly echo that so that it keeps the bold tags and actually display the text "Classic quote from movie" in BOLD? I'm doing something wrong somewhere along the line (simply doing "echo $text;") because it displays on the page as... Code: [Select] <b>Classic Quote from movie</b> Instead of... Classic Quote from movie Any info on properly storing and echoing back HTML would be very appreciated. Hello everyone! I am a new user and I have a simple question. How to download the database and create a backup. I have a profile page where the user answers a list of about 20 questions. These questions are then put into a mysql table with username, questionid, and answer. I can store the answers to the table, but I cannot figure out how to get them back to view and edit. My form is built from basic HTML. I know how to pull answers from a db table with only 1 row of results, where each field is a different question, but this one is different, as it will pull 20 rows, and each row is for a different question. Here is how I populate the questions and then fill in the answers. Code: [Select] // If this user has never set their profile, insert empty questions into the database $query1 = "SELECT * FROM u_profile WHERE username = '" . $_SESSION['username'] . "'"; $data = mysqli_query($dbc, $query1); if (mysqli_num_rows($data) == 0) { // First grab the list of questionids $query2 = "SELECT questionid FROM questions ORDER BY q_order"; $data = mysqli_query($dbc, $query2); $questionids = array(); while ($row = mysqli_fetch_array($data)) { array_push($questionids, $row['questionid']); } // Insert empty question rows into the u_profile table, one per question foreach ($questionids as $questionid) { $query3 = "INSERT INTO u_profile (username, questionid) VALUES ('" . $_SESSION['username']. "', '$questionid')"; mysqli_query($dbc, $query3); } } // If the questionnaire form has been submitted, write the responses to the database if (isset($_POST['submit'])) { // Write the questionnaire response rows to the response table foreach ($_POST as $answer_id => $answer) { $query4 = "UPDATE u_profile SET answer = '$answer' WHERE username = '" . $_SESSION['username'] . "' AND questionid = '$answer_id'"; $uprofile_set = "CALL uprofile_set('" . $_SESSION['username'] . "')"; mysqli_query($dbc, $query4) or die( "Connection Error1" . mysqli_error($dbc) ) ; mysqli_query($dbc, $uprofile_set) or die( "Connection Error2" . mysqli_error($dbc) ) ; } $races = "SELECT * FROM u_raceschedule WHERE username = '" . $_SESSION['username'] . "'"; $data = mysqli_query($dbc, $races); if (mysqli_num_rows($data) > 0) { set_time_limit(30); $buildplan = "CALL tplan('" . $_SESSION['username'] . "')"; mysqli_query($dbc,$buildplan) or die("Connection Error2" . mysqli_error($dbc) ) ; Would LOVE any help. I am really new to this whole coding thing. Hi.. I've seen code for back up database but when I run the code the database was backup outside the folder. I want to put the back up database inside the folder here is the code: Code: [Select] <?php include 'config.php'; backup_tables('localhost','root','','payroll'); /* backup the db OR just a table */ function backup_tables($host,$user,$pass,$name,$tables = '*') { $link = mysql_connect($host,$user,$pass); mysql_select_db($name,$link); //get all of the tables if($tables == '*') { $tables = array(); $result = mysql_query('SHOW TABLES'); while($row = mysql_fetch_row($result)) { $tables[] = $row[0]; } } else { $tables = is_array($tables) ? $tables : explode(',',$tables); } //cycle through foreach($tables as $table) { $result = mysql_query('SELECT * FROM '.$table); $num_fields = mysql_num_fields($result); $return.= 'DROP TABLE '.$table.';'; $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table)); $return.= "\n\n".$row2[1].";\n\n"; for ($i = 0; $i < $num_fields; $i++) { while($row = mysql_fetch_row($result)) { $return.= 'INSERT INTO '.$table.' VALUES('; for($j=0; $j<$num_fields; $j++) { $row[$j] = addslashes($row[$j]); $row[$j] = ereg_replace("\n","\\n",$row[$j]); if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; } if ($j<($num_fields-1)) { $return.= ','; } } $return.= ");\n"; } } $return.="\n\n\n"; } //save file $myfoldername = "backup_DBPayroll";//your folders name $handle = fopen(getcwd().$myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+'); // $handle = fopen('db-backup-'.date('m-d-Y').'-'.(md5(implode(',',$tables))).'.sql','w+'); fwrite($handle,$return); fclose($handle); } $smarty->display('header_cat.tpl'); $smarty->display('backup.tpl'); $smarty->display('footer.tpl'); ?> Thank you in advance Hi All, I have a table (with an ID of example) that can be seen he http://tinyurl.com/cfla7wp The following script calls jeditable.js and makes the Telephone & Mobile columns editable. Once edited, the table shows the new value plus a * symbol. Code: [Select] <script type="text/javascript"> $(document).ready(function() { /* Init DataTables */ var oTable = $('#example').dataTable(); /* Apply the jEditable handlers to the table */ $('td:eq(4), td:eq(5)', oTable.fnGetNodes()).editable( 'update_table_tenants.php', { "callback": function( sValue, y ) { var aPos = oTable.fnGetPosition( this ); oTable.fnUpdate( sValue, aPos[0], aPos[1] ); }, "submitdata": function ( value, settings ) { return { "row_id": this.parentNode.getAttribute("1"), "column": oTable.fnGetPosition( this )[2] }; }, "height": "14px" } ); } ); </script> What I need to do however is add the value entered into my database, but I do not know how to do this. I believe I need to do something like: UPDATE my_table VALUE whatever column has been edited in the table WHERE tenID is the same as the row selected in the table i.e. if I update the MOBILE NUMBER of the person called BILL GATES it would find his tenID, update numMobile in the table called my_table All help appreciated Dave I have a table that has a cell that I've made editable:
echo "<td contenteditable='true'>" . $row['Comments'] . "</td>";Which is cool I guess, but it doesn't go anywhere from there. I'd like to make it so that upon changing, it links back to an SQL UPDATE query that I define. Is this possible through some sort of onChange function? Hi am in processes of making a bespoke CMS for a project in uni am having a problem with my scrip basically i have three php files i have content which echos out the database tables i wish to edit with a link to an update_content page when u click on that it bring u too update_content.php which allows the user to modifier the content in the tables but when i hit update am getting a Parse error: syntax error, unexpected T_IF in line 4 of my update_ac file any help would be great cheers richie <?php require("includes/connection.php") // If form button has been pressed then do the following if(isset($_POST['update'])){ // Get id of post $id = $_GET['id']; $header = $_POST['header']; $content = $_POST['content']; // Update database table $query = "UPDATE pages SET header = '$header', content = '$content' WHERE id = '$id'"; $result = mysql_query($query); if ($result){ echo "Successfully edited entry"; } else { echo "There was error editing entry"; } } ?> I have these two tables...
schedule (gameid, homeid, awayid, weekno, seasonno)
teams (teamid, location, nickname)
This mysql query below gets me schedule info for ALL 32 teams in an array...
$sql = "SELECT h.nickname AS home, a.nickname AS away, h.teamid AS homeid, a.teamid AS awayid, s.weekno FROM schedule s INNER JOIN teams h ON s.homeid = h.teamid LEFT JOIN teams a ON s.awayid = a.teamid WHERE s.seasonno =2014"; $schedule= mysqli_query($connection, $sql); if (!$schedule) { die("Database query failed: " . mysqli_error($connection)); } else { // Placeholder for data $data = array(); while($row = mysqli_fetch_assoc($schedule)) { if ($row['away'] == "") {$row['away']="BYE";} $data[$row['homeid']][$row['weekno']] = $row['away']; $data[$row['awayid']][$row['weekno']] = '@ '.$row['home']; } }However, I only want to get info for one specific team, which is stored in the $teamid variable. This should be very easy, right? I have tried multiple things, including this one below (where I added an AND statement of "AND (h.teamid=$teamid OR a.teamid=$teamid)"), but this one still outputs too much... $sql = "SELECT h.nickname AS home, a.nickname AS away, h.teamid AS homeid, a.teamid AS awayid, s.weekno FROM schedule s INNER JOIN teams h ON s.homeid = h.teamid LEFT JOIN teams a ON s.awayid = a.teamid WHERE s.seasonno =2014 AND (h.teamid=$teamid OR a.teamid=$teamid)"; $schedule= mysqli_query($connection, $sql); if (!$schedule) { die("Database query failed: " . mysqli_error($connection)); } else { // Placeholder for data $data = array(); while($row = mysqli_fetch_assoc($schedule)) { if ($row['away'] == "") {$row['away']="BYE";} $data[$row['homeid']][$row['weekno']] = $row['away']; $data[$row['awayid']][$row['weekno']] = '@ '.$row['home']; } }Below is the array that the above outputs. In a nutshell, all I want is that 1st array ([1]) which has, in this example, the Eagles full schedule. It's not giving me too much else and I guess I could live with it and just ignore the other stuff, but I'd rather be as efficient as possible and only get what I need... Array ( [1] => Array ( [1] => Jaguars [2] => @ Colts [3] => Redskins [4] => @ 49ers [5] => Rams [6] => Giants [7] => BYE [8] => @ Cardinals [9] => @ Texans [10] => Panthers [11] => @ Packers [12] => Titans [13] => @ Cowboys [14] => Seahawks [15] => Cowboys [16] => @ Redskins [17] => @ Giants ) [27] => Array ( [1] => @ Eagles ) [28] => Array ( [2] => Eagles ) [4] => Array ( [3] => @ Eagles [16] => Eagles ) [14] => Array ( [4] => Eagles ) [15] => Array ( [5] => @ Eagles ) [3] => Array ( [6] => @ Eagles [17] => Eagles ) [] => Array ( [7] => @ Eagles ) [16] => Array ( [8] => Eagles ) [25] => Array ( [9] => Eagles ) [11] => Array ( [10] => @ Eagles ) [7] => Array ( [11] => Eagles ) [26] => Array ( [12] => @ Eagles ) [2] => Array ( [13] => Eagles [15] => @ Eagles ) [13] => Array ( [14] => @ Eagles ) ) Lets start out by saying I'm a nube to sql/php things so I am learning as I go. I try to read all that I can before I post, and only post when I cant figure it out on my own. That being said. What I want to do in simplest terms is be able to assign a variable to each item in an sql table. So say I have an mysql table that has ID, username, fontcolor. I want to be able to pull those out so say.... while($row = mysql_fetch_array($users)) { $username[$i] = $row[username]; $fontcolor[$i] = $row[fontcolor]; } Then on the page I can just call to $username[1] type thing. I have tried mixing this several different ways with for and while and I keep getting errors on the page. I realize the code isn't showing everything but its just there to show you the idea of what I'm trying to do. I just want it to generate a list(array) that will make it easier for me to call back just the items I need on parts of the page with out having to have extra coding everywhere. Thanks in advance. Jim At the moment I am creating a search function for my website. The approach I have in mind is a pseudo-PHP database. To give an example: A HTML form will submit the results to a PHP file. HTML FORM - Colour: Black PHP RESULT PAGE - if ($_POST['color'] == 'Black') {readfile("./products/black/*.html");} HTML FORM - Price: <$50 PHP RESULT PAGE - if ($_POST['Price'] == '<$50') {readfile("./products/less50/*.html");} The problem here is if there is an item that is black and costs less than $50, then its going to be listed twice. There is probably some code I can write to ommit the listing of duplicate entries, but it is probably going to be messy, so I am wondering if its better to use a centralized MySQL database, rather than a pseudo-PHP database? I've never used MySQL and don't know much about it and this is my first real attempt at using PHP. "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? Please, take a look to the following code.After clicking Next it goes to overview.php.Why when I click back on my browser to return to this page again, it is not returning back? When I click back I receive "Confirm Form Resubmission" message. After refreshing page it loads page. I guess problem is in "session_start();" part. Something to do with cookies. Please, help me it is very urgent for me. <?php session_start(); echo "<html> <head> <title>Hello World</title> <meta http-equiv='Content-Type' content='text/html; charset=Windows-1252'/> </head>"; require_once ('functions.inc'); if(!isset($_POST['userid'])) { echo "<script type='text/javascript'>"; echo "window.location = 'index.php'"; echo "</script>"; exit; }else{ session_register("userid", "userpassword"); $username = auth_user($_POST['userid'], $_POST['userpassword']); if(!$username) { $PHP_SELF = $_SERVER['PHP_SELF']; session_unregister("userid"); session_unregister("userpassword"); echo "Authentication failed " . "Please, write correct username or password. " . "try again "; echo "<A HREF=\"index.php\">Login</A><BR>"; exit; } } function auth_user($userid, $userpassword){ global $default_dbname, $user_tablename; $user_tablename = 'user'; $link_id = db_connect($default_dbname); mysql_select_db("d12826", $link_id); $query = "SELECT username FROM $user_tablename WHERE username = '$userid' AND password = '$userpassword'"; $result = mysql_query($query) or die(mysql_error()); if(!mysql_num_rows($result)){ return 0; }else{ $query_data = mysql_fetch_row($result); return $query_data[0]; } } echo "hello"; echo "<form method='POST' name='myform' action='overview.php'>"; echo "<input type='submit' value='Next'>"; echo "</form>"; ?> If the script is not uploaded onto the server you cant use $dbc = mysql_connect('localhost', 'username', 'pass'); so how could I connect in cli? this did not work $dbc = mysql_connect('http://123.456.789.10', 'username', 'pass'); So what can I do? Thanks I am building an e-commerce site and I am aiming to create a front end displaying my products with an option for customers to buy them, and have a content management system as a back end for an admin to edit product information.
Currently I am storing information about my products on a mysql database. I access and display the product info using a while loop. Below is a simplified version of what I am doing without any html to style it. This code will go through the database and each iteration will go the to the next row and display the info about the next product.
$query = mysql_query("SELECT * FROM truffleProducts"); while ($row = mysql_fetch_array($query)) { $id = $row['id']; $name = $row{'Name'}; $price = $row{'Price'}; $desc = $row{'Description'}; echo $id; echo $name; echo $price; echo $desc; }I have began to implement a 'buy' button so that customers are able to click on a button next to the product info and purchase it. However I have come across a problem which is where my program won't be able to tell which product you have selected as the number stored in the $id variable will just be the last product it has fetched from the database. I can't differentiate between all the product's buy buttons, so it will impossible to place an order for a customer with the current system I have. Can any one tell me how to get the id number of the specific product that a user has selected? I only started learning PHP a month or two ago so assume I know nothing Having a major problem connecting to a live site. I have set up the user and database on Cpanel like normal. Then proceeded to insert those details into my mysql connection script Code: [Select] <?php $db_link = mysql_connect("***","****","***"); if (!$db_link) { die('Could not connect: ' . mysql_error()); } $db_selected = mysql_select_db('amenshar_calender', $db_link); if (!$db_selected) { die ('Can\'t use database : ' . mysql_error()); } ?> I am connecting to domain erban.co.uk, yet looking at the cpanel the master domain is amensharma.co.uk. Is this what the problem could be. This is the error messege it is throwing up Warning: mysql_connect(): Access denied for user '_admin'@'localhost' (using password: YES) in d:\Domains\erban.co.uk\wwwroot\functions\database.php on line 4 Could not connect: Access denied for user '_admin'@'localhost' (using password: YES) Hello and good day to all of you I have passed my midterms project(Online Shopping) with a score of 94 in PHP ( Click here to see file ) ( Sorry if the design is too ugly, I'm just new in programming XD ) Now our finals project will be a PHP site again but now with a database in it.. So instead of making a new project/design for a site, why not just add a database to my existing midterms? I would like to ask for some help here in helping me on connecting to the database, adding a database/tables and anything that would be of help like suggesting Databases would be like, - Users ( Admin and Members ) - Upper Clothing ( Shirts, Jackets, Long Sleeves, Raglan and etc ) - Lower Clothing ( Pants, Shorts, Boxers and etc ) - Stock on a current item - Single member's Transaction History - All user's Transaction History Is there a way that I could add those database named above to my project? Thank you in advance! this is on amensharma.co.uk/testing Code: [Select] <?php $db_link = mysql_connect("amensharma.co.uk","amenshar_admin","amen001"); if (!$db_link) { die('Could not connect: ' . mysql_error()); } $db_selected = mysql_select_db('amenshar_calender', $db_link); if (!$db_selected) { die ('Can\'t use database : ' . mysql_error()); } ?> this is the error it returns Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'amenshar_admin'@'neptune.servers.rbl-mer.misp.co.uk' (using password: YES) in /home/amenshar/public_html/testing/functions/database.php on line 4 Could not connect: Access denied for user 'amenshar_admin'@'neptune.servers.rbl-mer.misp.co.uk' (using password: YES) bit stumped here any help would be great all the details are correct, just confused why im having these errors hiii all, i want to show the database field (alise name) as the url after index.php when page or site loads first time. how i can achieve it? please help.... thanks in advance |