PHP - Replacement String That Comes From Database
Similar TutorialsHi guys, I need to find a way to do the following in this string: Code: [Select] {"id":"51","value":["Lorem Ipsum","Lorem Ipsum","new"]} I need to match Code: [Select] "id":"51" If that is matched, I want to remove that ENTIRE string. How would I go about doing that? In other words, I run a preg_match or something like that and if "id":"51" is found, i need to remove the entire element in which it is contained. In this case the element is the above mentioned string. Any ideas? Perhaps with RegEx? (I have no clue how to use RegEx) Hello i have a system where leaders add members, i want to display a table of members WHERE the 'leader' field equals 'myusername' so that leaders only see members they added. when using the WHERE function i get a database empty as the result but when i remove the WHERE function i get all the data returned Code: [Select] include("system/include/session.php"); $myUname = $session->username; echo "$myUname"; function displayMembers(){ global $database; $q = "SELECT * " ."FROM ".TBL_MEMBERS." WHERE leader = '$myUname'"; $result = $database->query($q); $num_rows = mysql_numrows($result); if(!$result || ($num_rows < 0)){ echo "Error displaying info"; return; } if($num_rows == 0){ echo "Database table empty"; return; } echo "<table>\n"; echo "<thead> <tr> <th>Full Name</th> <th>Gender</th> <th>DOB</th> <th>Post Code</th> <th>Email Address</th> <th>Edit User</th> </tr> </thead>\n"; for($i=0; $i<$num_rows; $i++){ $Mprefix = mysql_result($result,$i,"prefix"); $Mfname = mysql_result($result,$i,"firstname"); $Mlname = mysql_result($result,$i,"lastname"); $Mgender = mysql_result($result,$i,"gender"); $Mdob = mysql_result($result,$i,"dob"); $Mpostcode = mysql_result($result,$i,"postcode"); $Memail = mysql_result($result,$i,"email"); echo " <tbody> <tr> <td>$Mprefix $Mfname $Mlname</td> <td>$Mgender</td> <td>$Mdob</td> <td>$Mpostcode</td> <td>$Memail</td> <td> <center> <form action=\"system/admin/adminprocess.php\" method=\"POST\"> <input type=\"hidden\" name=\"deluser\" value=\"$uname\"> <input type=\"hidden\" name=\"subdeluser\" value=\"1\"> <input type=\"submit\" class=\"button plain\" value=\"Edit Member\"> </form> </center> </td> </tr> </tbody>\n"; } echo "</table><br>\n"; } all help i am thankful for Hello all, been trying for over a day to get something to work and after searching and searching I could not find what I was looking for. I found people looking to do something similar, but the answers seemed more complicated than I would think they need to be. First off, I'm still horrible at arrays and loops so try not to laugh too hard if my code is waaaayyyy off...haha I am essentially trying to tie products and quantities to a reservation. The user has the ability to add items to the form. I'm using jquery .clone to create each new item product and quantity form fields. When the user submits the form jquery inputs the values of the cloned fields into 2 hidden text fields called "productsField" and "quantitiesField" The values of these fields look something like value="1,2,2,1,3" depending on how many products were added. What I want to do is explode both and then enter them into the database. Seems simple enough, but apparently I am not getting it. I thought something like this would work, but it is not. if (isset($_POST["reserve_button"])) { $theReservationID = $_SESSION['createdReservationID']; $items = explode (",",$_POST['productsField']); $quantities = explode (",",$_POST['quantitiesField']); // loop through array $number = count($items); for ($i=0; $i<=$number; $i++) { // store a single item number and quantity in local variables $itno = $items[$i]; $quant = $quantities[$i]; if ($items[$i] <> '') { mysql_query('INSERT INTO reservation_items (reservationID,productID,productQuantity) VALUES($theReservationID,$itno,$quant)'); } } } Any help would be greatly appreciated. Thanks in advance, Twitch Hi, I'm creating a PDF document using the FPDF library. All is fine, using a sample image that came with the library. However, I need to get the data for the PDF from a database. The data's fine - the query is performed and the data prints out as I need. However, there's one line which takes a value which is a filepath. When I put that into the following line, I get an error from FPDF: Code: $this->Image($row['imgpath'],10,10,190); Error produced: FPDF error: Image file has no extension and no type was specified: However, if I print out the value of $row['imgpath'] and manually insert it into the code as follows, it works fine: Code: $this->Image('../assets/images/products/5777A.jpg',10,10,190); I've come across this problem once before but can't for the life of me remember what the solution was. It was something to do with how I was reading the value into the code, I think. Can anyone jog my memory, please?? Hey there... I'm so stuck on this problem I was hoping someone could help... I need to query a MySQL Database using array values... Here is my array ... $query_keywords = Array ( [7] => business [10] => home [11] => depo ) Here is my php ...$query = "SELECT * FROM product WHERE product_name, product_comment IN ($query_keywords)"; $result=mysql_query($query); if(mysql_num_rows($result) > 0) { echo "results"; } else { echo "no results"; } Unfortunately, I get this ... Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in blah\blah\results.php on line 99 Please help me All comments greatly appreciated... Hi everyone, I'm new to this group and new to php. I have created a multi-part form that allows the user the option to add multiple input fields to a form to upload images. Here is the form structu Code: [Select] <form action="Scripts/processreports2.php" method="post" enctype="multipart/form-data" name="report_form" target="uploader" class="reportfrm"> <fieldset> <legend>Upload your images</legend> <ol id="add_images"> <li> <input type="file" class="input" name="files[]" /> </li> <li> <input type="file" class="input" name="files[]" /> </li> <li> <input type="file" class="input" name="files[]" /> </li> </ol> <input type="button" name="addFile" id="addFile" value="Add Another Image" onclick="window.addFile(this);"/> </fieldset> <p>* indicates a required field.</p> <input name="submit" type="submit" id="submit" value="Send Info!" /> </form> Through php a maximum of three input fields are fed into an array that checks to make sure that the uploaded files are images and not some other type of file. The uploading porition of this script works. Now I am trying to get the values of the input fields as a string and insert them into the database as one record. Let's say the user has three files they want to upload. I have managed to get the files as a string ie; file1.jpg, file2.jpg, file3.jpg but what is happening is that I am getting three separate records with file1.jpg, file2.jpg, file3.jpg in them. If the user has only two files to upload then I get two separate records with file1.jpg and file2.jpg in them. (Hope that makes sense). I want one record. I have been struggling with this since Monnday and while every day I get closer, this is as close as I can get. Below is the php code. #connect to the database mysql_connect("localhost", "root", ""); mysql_select_db("masscic"); //Upload Handler to check image types function is_image($file) { $file_types = array('jpeg', 'gif', 'bmp'); //acceptable file types if ($img = getimagesize($file)){ //echo '<pre>'; //print_r($_FILES); used for testing //print_r($img); used for testing if(in_array(str_replace('image/', '', $img['mime']), $file_types)) return $img; } return false; } //form submission handling if(isset($_POST['submit'])) { //file variables $fname = $_FILES['files']['name']; $ftype = $_FILES['files']['type']; $fsize = $_FILES['files']['size']; $tname = $_FILES['files']['tmp_name']; $ferror = $_FILES['files']['error']; $newDir = '../uploads/'; //relative to where this script file resides for($i = 0; $i < count($fname); $i++) { //echo 'File name ' . $fname[$i] . ' has size ' . $fsize[$i]; used for testing if ($ferror[$i] =='UPLOAD ERR OK' || $ferror[$i] ==0) { if(is_image($tname[$i])) { //append the tmp_name($tname) to the file name ($fname) and upload to the server move_uploaded_file($tname[$i], ($newDir.time().$fname[$i])); echo '<li><span class="success">'.$fname[$i].' -- image has been accepted<br></span></li>'; }else echo '<li><span class="error">'.$fname[$i].' -- is not an accepted file type<br></span></li>'; } if (is_array($fname)) $files = implode(', ',$fname); //else $files = $fname; $sqlInsert = mysql_query("INSERT INTO files (file_names) VALUES('$files')") or die (mysql_error()); } } Hello, I am attempting to create a script where a user puts a text string into a form and clicking on submit. Then it adds that text string to a mysql database and gives the user a link back to it. Something like mysite.com/key.php?id=4 and when going to that link it will display the text string from the database but printed in an image. I wrote something up but I cannot get it to work. I may be a little in over my head. Any help is appreciated.
The way I coded this all to work is the user inputs the information into index.php which givekey.php inserts that into the database. I have not yet figure out how to do this in one single step. After that I wanted it to navigate directly to key.php which would display the key and the link to that page with the ?key=XX attribute. getkey.php <?php require_once 'dbinfo.php'; // database connection $id = $_GET['id']; // do some validation here to ensure id is safe $link = mysql_connect($servername, $username, $password); if (!$link) { die('Could not connect: ' . mysql_error()); } @mysql_select_db($database) or die( "Unable to select database"); $sql = "SELECT ukey FROM keycode WHERE id=$id"; $result = mysql_query("$sql"); $row = mysql_fetch_assoc($result); mysql_close($link); header("Content-type: image/png"); echo $row['ukey']; ?> key.php <html> <head> <title>Your Key Is Ready</title> </head> <body> <img src="getkey.php?id=1" width="175" height="200" /> </body> </html>index.php <html> <head> <title></title> </head> <body> <section id="mid_section"> <div id="boxes"> <h1> Testing input key </h1> <br/> <form id="myform" action="givekey.php" method="post"> Key:<br /> <input type="text" value="ukey"> Source:<br /> <input type="radio" value="hb">HB<br /> <input type="radio" value="ig">IG<br /> <input type="radio" value="other">Other<br /> <button id="sub">Submit</button> </form> </body> </html>givekey.php <?php include_once('dbinfo.php'); $conn = mysql_connect($servername, $username, $password); $db= mysql_select_db($database); $ukey =$_POST['ukey']; $hb =$_POST['hb']; $ig =$_POST['ig']; $other =$_POST['other']; if(mysql_query("INSERT INTO `keycode`(`ukey`) VALUES ([$ukey]); INSERT INTO `source`(`hb`,`ig`,`other`) VALUES ([$hb],[$ig],[$other]);")) ?> Edited by chrisb302, 13 November 2014 - 12:18 AM. Hello all, I have a script uploading files on the server. I would like to send a confirmation to user if the file had been uploaded well. Code: [Select] $targetFile = str_replace('//','/',$targetPath) . $filename; move_uploaded_file($tempFile,$targetFile); if (str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile)) { mail($to, 'upload notification', "Dear Mr/Mrs $fixedname, the file entitled $filename has been successfully uploaded"); }else { mail($to, ' upload notification', "Dear Mr/Mrs $fixedname, the file entitled $filename has been NOT uploaded"); } I presume I do something wrong because it doesn't work. The successfull email is sent each time. Thank you very much for helping, Eric I want to duplicate my website but I don't want to have to edit my original files to change like the phone number on the page and upload it to my duplicate site, I just want to either insert coding on my duplicate website that would cause my original website to show up differently, like changing the phone number listed on the page. I am having trouble trying to fix my eregi call in my script. I have a reporting tool that grabs information from the user agent when they email help questions. I used eregi to find the string in an array and print out when it finds a match. I don't fully understand the PCRE functions so i don' t know if i am using them right. what i have is foreach($OSList as $CurrOS=>$Match) { // Find a match if (eregi($Match, $_SERVER['HTTP_USER_AGENT'])) { // We found the correct match break; } } I know i need to change this around for PCRE preg_match maybe but i am not sure. if anyone could send me a tutorial or help me out it would be great. EDIT: notice the foreach was not correct $out = wordwrap($data,400,"@"); $output = explode("@",$out, -1); By default a -1 no longer works properly for me so I need a similar method to get the same output. I made a game back then that reads the php file. So I could change the extension to .php4/.php5 (forget which one would work) the sockets will still link to the default .php file. So how can I fix this? My code for replacing user input is as follows: function Replace_BB($text) { $bb = array( '@\[u\](.*?)\[\/u\]@is', '@\[i\](.*?)\[\/i\]@is', '@\[b\](.*?)\[\/b\]@is', '@\[img\](.*?)\[/img\]@is', '@\[url\](.*?)\[/url\]@is', '@\[url=http://(.*?)\](.*?)\[/url\]@is' ); $html = array( '<u>$1</u>', '<em>$1</em>', '<strong>$1</strong>', '<img src="$1" />', '<a href="$1">$1</a>', '<a href="$1">$2</a>' ); return preg_replace($bb, $html, $text); } print_r (Replace_BB($_POST['data'])); How can I use an if statement to decide how to put in my replacement. So for example: Code: [Select] $html = array( 'IF XXXXXX THEN DO THIS>>>>> <u>$1</u> ELSE DO THIS >>>> <u>$1 Error</u>', Hi, I currently have some PHP scripts that verify form data using ereg() as follows: if (ereg(".",$var1)==0) { //do something } where instead of the argument "." I also use "\@" or "\....*", etc. to test whether certain form input is in the correct format (e.g. testing for a valid email address, etc.). However, I have understood that ereg() is being deprecated. What is the best/easiest function to replace ereg() with? Thanks! I am trying to update the database with isset to set the value of the variable to either null or the value entered by the user. Currently, if no value has been entered null is being set as a string causing the date field to populate with zeros. How do I code it so that it populates or sets the value as null and not as string null?
Here's my code: (yes I know sql injections it's still in development )
<?php Hi, I am trying to make some adjustments to uploadify.php which comes with the latest version of uploadify (3.0 beta), so that it works with a session variable that stores the login username and adds it to the path for uploads. Here is uploadify.php as it currently looks: Code: [Select] <?php session_name("MyLogin"); session_start(); $targetFolder = '/songs/' . $_SESSION['name']; // Relative to the root if (!empty($_FILES)) { $tempFile = $_FILES['Filedata']['tmp_name']; $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder; $targetFile = rtrim($targetPath,'/') .'/'. $_FILES['Filedata']['name']; // Validate the file type $fileTypes = array('m4a','mp3','flac','ogg'); // File extensions $fileParts = pathinfo($_FILES['Filedata']['name']); if (in_array($fileParts['extension'],$fileTypes)) { move_uploaded_file($tempFile,$targetFile); echo '1'; } else { echo 'Invalid file type.'; } } echo $targetFolder; ?> I added Code: [Select] echo $targetFolder; at the bottom so that I could make sure that the string returned was correct, and it is, i.e. '/songs/nick'. For some reason though, uploads are not going to the correct folder, i.e. the username folder, but instead are going to the parent folder 'songs'. The folder for username exists, with correct permissions, and when I manually enter Code: [Select] $targetFolder = '/songs/nick';all works fine. Which strikes me as rather strange. I have limited experience of using php, but wonder how if the correct string is returned by the session variable, the upload works differently than with the manually entered string. Any help would be much appreciated. It's the last issue with a website that was due to go live 2 days ago! Thanks, Nick Hello all, I'm trying to change the end of a javascript call based on the end of the url string. The common part of all the url strings is sobi2Id=, I'm trying to do this with strstr but am having no luck. I'm new to php so my syntax knowledge is terrible! at the moment i've got Code: [Select] <?php $url = $_SERVER['REQUEST_URI']; $tag = strstr ($url, 'sobi2Id='); echo $tag; ?> but this returns an unexpected T_STRING, expecting ',' or ';' Can anyone debug this? I may well be being really silly! This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=326004.0 Hey there, Thanks for taking the time to read my thread. My issue is that I can't think of a way to edit a XML file using PHP's XML functionality and then assign the edited contents to a string instead of saving the file. Because my issue is that I have to edit the XML file based upon a string brought from a remote location then give it back to that remote location using a string again, to be exact I am doing it via Linux command line utilizing SSH2. This is what I managed to complete on my own. function CheckIVMPConfig($ServerID) { global $Panel; if(is_numeric($ServerID) && $this->IsValidServer($ServerID)) { // We select the game server that the FTP account was created for. $Servers = mysql_query("SELECT * FROM control_servers WHERE server_id = '".mysql_real_escape_string($FTPAccount['ftp_server'])."'"); $Server = mysql_fetch_array($Servers); // Here we select the Box ID that the game server is on. $Boxs = mysql_query("SELECT * FROM control_machines WHERE machine_id = '".$Server['server_machine']."'"); $Box = mysql_fetch_array($Boxs); // Now we select the required package for the box. $Packages = mysql_query("SELECT * FROM control_packages WHERE package_id = '".$Server['server_package']."'"); $Package = mysql_fetch_array($Packages); // Retrive the file. $Config = $CProtocol->exec("cat /home/{$Server['server_id']}/{$Package['package_config']}"); $Parse = SimpleXMLElement($Config); foreach($Parse as $Entry) // loop through our books { if($Entry->port != $Server['server_port']) { // edit the value } else if($Entry->maxplayers > $Server['server_slots']) { // edit the value } } } } |