PHP - Probably Easy Solution, But Its Got Me Stumped!
Hi guys,
I have a simple INSERT code. It does not throw any errors and all seems ok but the data is just not appearing in the DB. I've spent ages looking over it but perhaps a 2nd set of eyes could help? Thanks! Code: [Select] $con = mysql_connect("localhost","posturep",""); $tablename = 'product'; if (!$con) { die('Could not connect: ' . mysql_error()); } //Form data sent $product_name = $_POST['product_name']; $product_active = $_POST['product_active']; $product_cat = $_POST['product_cat']; $product_desc = $_POST['product_desc']; $prod_inoutdoor = $_POST['prod_inoutdoor']; $prod_stackable = $_POST['prod_stackable']; $prod_discounted = $_POST['prod_discounted']; echo $product_name; $sql = "INSERT INTO $tablename (name) VALUES ($product_name)"; mysql_query($sql); echo "<p>Product Saved!</p>"; echo $sql; mysql_close($con); Similar Tutorialshello, I'm a beginner when it comes to PHP: I'm working on a log in system but i keep on getting the same errors which makes my system unreliable. this is the function I'm trying to use: if(pg_numrows($q) == 1){ echo "<p>Someone took that username</p>"; include "signup.php"; exit; } --> I'm using a postgresql database this is the warning I keep on getting: Code: [Select] Warning: pg_numrows(): supplied argument is not a valid PostgreSQL result resource in /var/ftpdirs/512544/PHP/login/adduser.php on line 13 I have the same problem when I try to look if an username is taken or not... when people try to register this is my adduser.php file: <?php session_start(); include "connectie_db.php"; $User_name = $_POST['user_name'] ; $User_pass = $_POST['user_pass'] ; $User_pass2 = $_POST['user_pass2'] ; $checkUsername= "SELECT user_name from tovanu.users where user_name = '$User_name';"; $q = $db->exec($checkUsername); if(MDB2::isError($q)){ echo "code: ".$q->getUserInfo(); exit(); } if(pg_numrows($q) == 1){ echo "<p>Someone took that username</p>"; include "signup.php"; exit; } If(strlen($User_name > 32)){ echo "<p>The username is too long</p>"; include "signup.php"; exit; } if($User_pass != $User_pass2){ echo "<p>Both passwords must be the same</p>"; include "signup.php"; exit; } $password = md5($User_pass); $add = "INSERT INTO tovanu.users (user_name,user_pass,user_email,user_date,user_level,naam,adres,plaats,postcode) VALUES ('$User_name','$password','email',current_date,1,'jef','jonhstraat','maaseik','3687')"; $execute = $db->exec($add) ; if(MDB2::isError($execute)){ echo "code: ".$q->getUserInfo(); exit(); } $_SESSION['user'] = $username; include "index.php"; ?> any help would be appreciated!!!!!!!!!!! I can't get this darn thing to update correctly. Probly a stupid (.) outa place. HELP! Code: [Select] <?php include 'connection.php'; if(!isset($_POST['submit'])) { $query = "SELECT * FROM news WHERE id = $_GET[id]"; $result = mysql_query($query); $news = mysql_fetch_array($result); $title = $_POST['title']; $body = $_POST['body']; } ?> <h1>Edit Article</h1> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> Title:<input type="text" name="title" value="<?php echo $news['title']; ?>" /><br /> Article:<textarea cols="35" rows="6" name="body"><?php echo $news['body']; ?></textarea> <br /> <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" /> <input type="image" src="/images/news/modify.png" name="submit" /> </form> <a href="admin.php"><img src="/images/news/cancel.png" border="0" /></a> <?php if(isset($_POST['submit'])) { $update = "UPDATE news SET `title`='$_POST[title]',`body`='$_POST[body]' WHERE id='$_POST[id]'" or die(mysql_error()); mysql_query($update) or die(mysql_error()); echo "The news article has been Modified!"; header('Location: /News/admin.php'); } ?> Code: [Select] $color = ' class="pin"'; $pin++; echo $pin; long story short, let's just say, this is inside a while loop echoing out my forum Topics. $pin echo's out 1 2 3, because they're 3 stickied Topics. I only want class="pin" to display on the very last <tr> tag. (which would be number 3, how?) Code: [Select] <tr<?php echo $color ?>> OK i do Code: [Select] if ($pin == 4){ $color = ' class="pin"'; }works if I have 4 Topics, stickied, but how would i change 4 to be dynamic? what if only 2 topics stickied? I tried Code: [Select] if ($pin == $pin){ $color = ' class="pin"'; } not work.. wouldn't $pin = 4? bcz $pin++ ? my help topics seem so noob, but i just cant comprehend lol If I have code that looks something like this: <?php $t = array('one','two','three','_four','_five','_six'); $u = 'five'; foreach ($t as $v) { if (substr($v,0,1) != '_') { // if begins with an underscore, remove it, unless it is equal to _$u echo $v.'<br />'; } } ?> I am trying to take out all array items that begin with an underscore except the one that is equal to this.. _five something like $v == '_'.$u So what should be echoed is this: one two three _five Is there any way to do this with only an if, and not an if/else?? Thanks.. OK, I am officially stumped. I am trying to match a '[]' at the end of a string. Using $ to anchor doesn't work, nor does \z. $name = "cities[]"; if (preg_match("/\[\]\z/", $name)) { // why do I net get in here?? } Thanks. SQL error2.Cannot add or update a child row: a foreign key constraint fails (`facility`.`fapplication`, CONSTRAINT `fapplication_ibfk_1` FOREIGN KEY (`fstaffid`) REFERENCES `fstaff` (`fstaffid`)) is there any solution for this problem cause my deadline is next week aaaaa... I'm really fond of your coding and need some help too I'm also trying to make a tree and save it in an xml file such that parent_id of one node is inserted in as pages key to the father node.By running this query $sql = "Select * from category order by parent_id ASC"; I'm getting the following result. Array ( => Array ( [category_id] => 2 [parent_id] => 0 [category_name] => Clothes ) [1] => Array ( [category_id] => 5 [parent_id] => 1 [category_name] => T-shirts ) [2] => Array ( [category_id] => 6 [parent_id] => 1 [category_name] => Cotton-Shirts ) [3] => Array ( [category_id] => 1 [parent_id] => 2 [category_name] => Upper ) [4] => Array ( [category_id] => 4 [parent_id] => 2 [category_name] => Lower ) [5] => Array ( [category_id] => 3 [parent_id] => 4 [category_name] => Pants ) ) My code is as follows but not executing correctly function searchTree($arr,$node,$i){ if(!in_array($node['parent_id'],$arr[$i])) { $stmt = '<pages_'.$i.'>'; $stmt .=$node; $stmt .='</pages_'.$i.'>'; return $stmt;} else{ //print_r($returnTree); print_r ($arr[$i]);//['category_id']; $returnTree = ($arr[$i]['category_id']==$node['parent_id'])? searchTree($arr,$node,$i++): searchTree($arr,'',$i++); } } $i=0; foreach($links as $link){ $myArr = searchTree($links,$link,$i);} The result should be as follows: <page_1> <label>Clothes</label> <uri>#</uri> <pages> <page_1_1> <label>Upper</label> <uri>#</uri> <pages> <label>T-Shirt</label> <uri>#</uri> <page_1_1> <page_1_1_1> <label>Short Sleaves</label> <uri>#</uri> </page_1_1_1> <page_1_1_2><label>Long Sleaves</label><uri>#</uri> </page_1_1_2></page_1_1></page_1> <page_1_2> <label>Lower</label> <uri>#</uri> <pages>....</page_1_2> Hopefully you can unsderstand this problem. Please help me. Ok I have a form that will include some text boxes and two sliders where numbers can be selected. When a user clicks submit I want it to take them to a solutions page based on what numbers they have selected. I'm finding it hard to explain what I'm trying to do so heres an example of what I would like. http://www.cleardebt.co.uk/#help It will be a form like that which links to a solution. If someone could please tell me what I need to do or have a tutorial of something similar it would be a great help for me to start. Hi, I am having trouble with some GIF, PNG images the error I am receiving these errors imagecreatefromgif() [function.imagecreatefromgif]: '/tmp/phpNAhLPI' is not a valid GIF and a similar one for PNG Is there a solution to this? and How can I hide these errors? So they never display to the user and Instead I echo a user friendly error. Please note again this is happening with some GIF and PNGs not all of them. Appreciate any help and thanks in advance! My friend has this problem, it needs a solution. I have no PHP experience, I don't know how to work with MD5 hash. A programming unit was compulsory on her course, but she completely fails on it. So let's be unethical and help her survive it. thanks in advance ===================== You are the website administrator for a website that requires a low level password for entry. The previous owner of the website forget to code in a password recovery feature and as such you now have to recover the passwords for a number of users of the website. You do have acess to the website databse BUT you are NOT allowed to manually change the passswords for users in the database as those passwords are heavily used in defining what permissions a user of the website has. e.g Moderators for the website forum all have a password begining with the lower case letter "m" in lower case. ALL letters are lowercase. Passwords were allocated to all users and the method of choice was to create a password which was built on two parts - a lowercase letter "m" and a number between 1 and 10000. An md5 hash was then created to form a 32 digit string. e.g the user with password "m1234" is represented in the database as the md5 hash: 77c12394ef7d4f23a8fa07d87309afd9 You have five users who need their orignal password. You know that the passwords all start with the letter "m". You decide that the simplest thing to do is to write some PHP code that uses a while loop to increment through the numbers 0 to 10000, concatenate that number with the "m" and then compare the md5 hash to that you recovered from the database. If the two hashes are the same then you have "cracked" the password. This technique is known as a "brute force" method of cracking passwords. You have a number of hashes to "crack" and you must enter the unique password into the answer box for each. As a starting point, the following PHP snippet will produce all of the lowercase letters: ___________________________________ Code: [Select] for ($i=97; $i<=122; $i++){ $x = chr($i); echo $x; } ?> hey guys i wrote an quick and dirty anagram solver for a challenge on hts which works great but i am more interested on feedback of how I could have written it better / more efficiently. anyone have there own version of writing this? thanks Code: [Select] $scramwords = array("htssboe","rtefcelh","vteevl","edsiyn","awloddno","dtey1d","kuedci","seellhy","sernin","acianro"); $unscramwords = array(); $filename = '../wordlist.txt'; // loop through scrambled words foreach($scramwords as $scramword) { // open the file if($fh = fopen($filename,'r')) do { // read line $line = fgets($fh); // see if line and scrambled word are same length if((strlen(trim($line))) == (strlen(trim($scramword)))) { //if they are the same length split the line into and array and the scrambled word into an array $l = array(); for($i=0;$i<strlen(trim($line));$i++){ $l[$i] = substr(trim($line),$i,1); } $s = array(); for($i=0;$i<strlen(trim($scramword));$i++){ $s[$i] = substr(trim($scramword),$i,1); } $errors = 0; // see if the arrays are different $chk = array_diff($l,$s); if(!empty($chk)){ $errors = 1; } // see if arrays are different in reverse order $chk2 = array_diff($s,$l); if(!empty($chk2)){ $errors = 1; } // found scrambled word if(!$errors) { array_push($unscramwords,$line); } } }while(!feof($fh)); fclose($fh); } // print out comma seperated un scrambled words echo implode(",",$unscramwords); Ok, well basically i'm not sure if this can be done or not. Basically I have a IM type bar for my website similar to facebook. It's from CometChat.com. Anyway the place to edit the links to all the modules for the bar are done in the config.php file. What I'm trying to do is put a link for users to click to go directly to their profile on the site. To do this i get the user's ID, and reference it to the mysql database, get the nickname, pass it to a varaiable and plug the variable into the url in the config file. The problem I'm having is when I run ANY mysql commands in the config file it kills the CometChat script from showing at all on the site. I'm not sure why since the whole config file is in php... so i don't see how mysql statements would keep the script from showing. Another Idea I thought about was running the mysql statements in another php file and somehow get that variable from a php code in the config.php file.. but i'm not really sure what code will run the mysql file and return the variable i need. Any help is greatly appreciated. This topic has been moved to CSS Help. http://www.phpfreaks.com/forums/index.php?topic=356124.0 Hey guys/Gals ok, so if you want code let me know but in essence just a quick question to an unforseen issue. Were creating a virutal hacking simulation (for those who have been following me) and we just finised with the crack.php that compares the passwordcracker to the target users Systemkey (the piece that protects the target's pc from lower level attacks) if the passwordcracker is larger than the systemkey or equal to it, it will start the crack procress (that is simply a progress bar, then forward to userhome.php) issue were having right now is when the user attempts to crack but isn't high enough to do so it gives a message saying your software isn't high enough... ISSUE: if at this point I just go and type in userhome.php it connects me without having to crack it.. there is a session set in place that makes sure users are logged in the whole time but can't figure out how I can stop this from allowing access without a successful crack first.... any ideas would be greatly appreciated Thanks; desjardins, jeff This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=349561.0 Hi all. I am currently trying to build a site that will allow users to manage a self build house project. This is my first php MySQL project but I am getting to grips with it ok. I am currently building this for my own benefit at the moment as I am about to undertake a self build. I have a few questions to put to you guys. This is where I am at the moment I have a database table named materials which contains a list of materials , quantities, unit of measurement, id. My site has a user control panel that has separate forms for each stage of the build where the user enters the quantities of the materials, and the data is displayed as an array for each stage. So, heres question 1. I want to take the data and use it to generate a form which will be completed by several merchants.... so it will display similar to the array but will be a form with a text box for price where the merchant will insert a price. i.e <?php mysql_connect ("localhost", "test", "test") or die ('I cannot connect to the database becuase: ' . mysql_error()); mysql_select_db ("matquant"); // query $query = mysql_query("SELECT * FROM `quantites` ORDER BY `id` ASC"); // results while ($row = mysql_fetch_array($query)) { echo "<br />" .$row ['id']. " " .$row ['material']. " " .$row['quantity']. " " .$row['unit']. "<br />";} echo mysql_error(); ?> I want to add a text input box form which will allow the merchant to add a price to each item that is added to the array. so there the merchant will be able to give me a price for all the items in my list. Heres where I get stuck.... as I am constantly adding to the list I have no fixed amount of items so the form for the merchants needs to be built dynamically, ie each time I add a new material, I want the merchants form to get a new input. I am not very good at describing my problems but I am hoping that someone can help me out from the information given. Thats the first question. i like to get issues sorted in small bites. the second is the next big bite, I want to send the form above to several merchants, so I intend to have a user database with a user for each merchant, and link the tables so that each merchant can provide a price for each material, ideally the form would highlight the materials that have not had a price yet. I'm looking for advice on the best method of doing this basically but if I can get the first question sorted that would be great.. I have the test site local but can upload it it to the server if it will help you see what im trying to do. Any help that anyone can give will be greatly appreciated. hi i am calling some text from a database with php and echoing it to flash. i have no idea the charset anything is in. all i can see is that calling the text from the database and placing into flash yields in some missing chars, but if echo the called text to the browser, copy it and echo that to flash, all characters appear. now, i kow this is not a flash forum, and im not going to ask for a flash solution because there's just too many different charsets and code i cannot control/ nor see so i was thinking of doing something along the line of having php creating a clone of the copied text that appears in the browser, and having it echo that text to flash. it there something php can do, similar to what i described? regards i want to make a forum that nobody can fill that through any software automatically i make this one with java script authentications but some one also make a break of this and and he fill this software only in one click with automatic way this one which i make http://www.ebs-ads.com/advertising/forms.php?id=4&name=Abel any better solutions using php or any other function that users must have to post every input field separately manually ..if yes Please suggest me thanks Hi all, I would like to ask you if somebody could have a better idea to build this function. The current one is working, but I feel this built a bit in an amateur manner. Feel free to add your ideas so we can learn more about PHP and how to deal with arrays.
I have an array: $arrayVideoSpecs = Array( Array( 'aspect' => '4:3', 'density' => '442368', 'resolution' => '768x576' ), Array( 'aspect' => '4:3', 'density' => '307200', 'resolution' => '640x480' ), Array( 'aspect' => '16:9', 'density' => '2073600', 'resolution' => '1920x1080' ), Array( 'aspect' => '16:9', 'density' => '121600', 'resolution' => '1280x720' ) );
and I want an array as output grouped by video aspect ratio where the key is the pixel density and the value is the video resolution, namely like that for aspect ratio 16:9 ... Array ( [2073600] => 1920x1080 [121600] => 1280x720 )
Then I coded this function which is working but seems amateur ... function groupAndExtractByAspect($array, $groupBy, $aspect, $density, $resolution) { $groupByAspect = Array(); foreach ($array as $value) { $groupByAspect[$value[$aspect]][] = Array($value[$density], $value[$resolution]); } $arrayClean = Array(); foreach ($groupByAspect as $key => $value) { if ($key == $groupBy) { $arrayClean[$key] = $value; } } foreach ($arrayClean as $aspectGroup) { $arrayOutput = Array(); for ($i = 0; $i <= count($aspectGroup); $i++) { $densityIsValid = false; $resolutionIsValid = false; if (!empty($arrayClean[$groupBy][$i][0])) { $density = $arrayClean[$groupBy][$i][0]; $densityIsValid = true; } if (!empty($arrayClean[$groupBy][$i][1])) { $resolution = $arrayClean[$groupBy][$i][1]; $resolutionIsValid = true; } if (($densityIsValid === true) && ($resolutionIsValid === true)) { $arrayOutput[$density] = $resolution; } } } return $arrayOutput; }
The usage is as follow ... $showArray = groupAndExtractByAspect($arrayVideoSpecs, '16:9', 'aspect', 'density', 'resolution'); echo '<pre>'; print_r($showArray); echo '</pre>';
Thank you very much for your ideas! Mapg |