kev Posted May 23, 2014 Share Posted May 23, 2014 I am attempting to insert data into a db via '?' in the url. The demonstration I've seen appears to work fine. I am using Wamp 2.2, Apache 2.2.22, PHP 5.4.3 and MySQL 5.5.24. My efforts merely take me off on a standard search. Has the global $_GET changed. My code. <?php//error_reporting(0);require 'db/connect.php';echo 'Connected'; //Added to be sure to be sure connectedif(isset($_GET['first_name'])) { $first_name = trim($_GET['first_name']); if($insert = $db->query(" INSERT INTO people(first_name, last_name, bio, created) VALUES ('{$first_name}', 'Garrett', 'I\'m a web developer', NOW()) ")) { echo $db->affected_rows; }}?> After the file is initiated the idea is to enter into the url ?first_name=Whatever press enter, the file should run and at the same time update the db. What am I missing. kev Quote Link to comment https://forums.phpfreaks.com/topic/288706-_get-inserting-at-the-url-not-working/ Share on other sites More sharing options...
IanA Posted May 23, 2014 Share Posted May 23, 2014 And what does your form look like? Can you provide that? Quote Link to comment https://forums.phpfreaks.com/topic/288706-_get-inserting-at-the-url-not-working/#findComment-1480571 Share on other sites More sharing options...
bsmither Posted May 23, 2014 Share Posted May 23, 2014 (edited) A basic web form will POST the form element name (key) and the user entered data (value) back to the form's action attribute. To get a form to submit data via the querystring (so that the script will use $_GET -- if you insist), the form's method attribute must be 'get' (although this might be the browser's default action). Edited May 23, 2014 by bsmither Quote Link to comment https://forums.phpfreaks.com/topic/288706-_get-inserting-at-the-url-not-working/#findComment-1480582 Share on other sites More sharing options...
kev Posted May 23, 2014 Author Share Posted May 23, 2014 Hello IanA and bsmither.. Thank you for replying. There is no form, The tutorial exercise that I was following was a demonstration on how to insert records into a db via the url, the sort of thing an admin might do. This can be done at the C/: prompt but that wouldn't solve the problem that has arisen. Quote Link to comment https://forums.phpfreaks.com/topic/288706-_get-inserting-at-the-url-not-working/#findComment-1480621 Share on other sites More sharing options...
joel24 Posted May 23, 2014 Share Posted May 23, 2014 (edited) //Could you put some debug code in and test it out? //Do you see your 'Connected' message? <?php //also, lets turn on error reporting whilst it's not working error_reporting(E_ALL); require 'db/connect.php'; echo 'Connected'; //Added to be sure to be sure connected if(isset($_GET['first_name'])) { $first_name = trim($_GET['first_name']); if($insert = $db->query(" INSERT INTO people(first_name, last_name, bio, created) VALUES ('{$first_name}', 'Garrett', 'I\'m a web developer', NOW()) ")) { echo $db->affected_rows; } else { echo "FAILED WRITING"; } } else { exit("$_GET not SET - ".print_r($_GET,true)); } ?> Edited May 23, 2014 by joel24 Quote Link to comment https://forums.phpfreaks.com/topic/288706-_get-inserting-at-the-url-not-working/#findComment-1480623 Share on other sites More sharing options...
kev Posted May 23, 2014 Author Share Posted May 23, 2014 Hello joel24. Thanks for replying. I followed your directions. First I did originally get the connected message and still got connected with following your instructions. No problem there. I didn't get a return from 'FAILED WRITING' I did have a problem with: exit("$_GET not SET - ".print_r($_GET,true)); With the " " in place, error report = Notice: Array to string conversion in C:\wamp\www\phpacademy\MySQLi\Video 6_9\index_7.php on line Without the " " in place. error report = Parse error: syntax error, unexpected 'not' (T_STRING) in C:\wamp\www\phpacademy\MySQLi\Video 6_9\index_7.php on line 21 kev Quote Link to comment https://forums.phpfreaks.com/topic/288706-_get-inserting-at-the-url-not-working/#findComment-1480631 Share on other sites More sharing options...
bsmither Posted May 23, 2014 Share Posted May 23, 2014 Within quotes, PHP will expand variables. $_GET is a variable, and PHP will try to show it's contents - but can't because it is an array. Within apostrophes, PHP will not expand variables. So, the line might work better as: exit('$_GET not SET - '.print_r($_GET,true)); Quote Link to comment https://forums.phpfreaks.com/topic/288706-_get-inserting-at-the-url-not-working/#findComment-1480632 Share on other sites More sharing options...
bsmither Posted May 23, 2014 Share Posted May 23, 2014 I also don't like this part of the SQL query: INTO people(first_name, I like to have plenty of spaces between the various parts of the query, and use backticks appropriately: INTO `people` (`first_name`, The way the statement looks now, people() might be seen as a function. Quote Link to comment https://forums.phpfreaks.com/topic/288706-_get-inserting-at-the-url-not-working/#findComment-1480633 Share on other sites More sharing options...
kev Posted May 23, 2014 Author Share Posted May 23, 2014 So far so good. I've reset with backticks and used single quotes within exit('$_GET not SET - '.print_r($_GET,true)); First result = Connected$_GET not SET -Array ( ) This is what I would expect and it also proves the connection to the db. The next bit is the bit that counts and that is entering ?first_name=whatever into the url and press return. Now with $_GET set when return is pressed it should update the db by inserting another record with a first_name of whatever but it doesn't it returned the following 'a bog standard search'. Your search - localhost/phpacademy/MySQLi/Video 6_9/index_7.php?first_name=whatever - did not match any documents. My problem is that I cannot see any coding error, my code was faithful to the tututrial code (which did work) but I cannot get the url input to function. One thing that shouldn't but may be a problem is that at present I am in France with a UK orientated computer and keyboard. Quote Link to comment https://forums.phpfreaks.com/topic/288706-_get-inserting-at-the-url-not-working/#findComment-1480652 Share on other sites More sharing options...
bsmither Posted May 23, 2014 Share Posted May 23, 2014 (edited) "Your search - xyz - did not match any documents." In your browser, did you enter the address into the address field, or the search field? This address: localhost/phpacademy/MySQLi/Video 6_9/index_7.php?first_name=whatever From this, I will assume you copied the PHP-Academy tutorial directly into the www folder of your WAMP installation? Although it seems to work, Video 6_9 is sometimes not valid as part of the path to a document -- there is a space. But since it seems to work, we can ignore it for now. Edited May 23, 2014 by bsmither Quote Link to comment https://forums.phpfreaks.com/topic/288706-_get-inserting-at-the-url-not-working/#findComment-1480657 Share on other sites More sharing options...
kev Posted May 24, 2014 Author Share Posted May 24, 2014 Hello bsmithers. Video 6_9 is valid for this file as it is part of my file location, I'm following the tutorial and instead of having one index file which would open the file automatically I number them so that I can break down the tutorial into steps for reference, so index_7 identifies at what part of the tutorial I am dealing with. I am also using WAMP with localhost, the url has no problem locating the address that is until I add ?first_name=Whatever I was only required to add to the existing address line ?first_name=Whatever with that done I then pressed enter (not refresh). The url should then locate the file index-7 and open it, it shouldn't read ?last_name=Whatever as part of the address. The file will then read the url address and look for the $_GET has now been set using ? from there it should update the db. P.S I did rename index_7.php to index.php, the problem still exists. Quote Link to comment https://forums.phpfreaks.com/topic/288706-_get-inserting-at-the-url-not-working/#findComment-1480688 Share on other sites More sharing options...
mac_gyver Posted May 24, 2014 Share Posted May 24, 2014 it's likely that the ? character that you are entering isn't an ascii ? character and results in the whole ... .php?.... string looking like a domain name and your browser is going off and trying to run a search for it instead of browsing to your page on your development system. Quote Link to comment https://forums.phpfreaks.com/topic/288706-_get-inserting-at-the-url-not-working/#findComment-1480697 Share on other sites More sharing options...
kev Posted May 24, 2014 Author Share Posted May 24, 2014 Hello mac_gyver I thought something similar. I'm in France at present but using a computer and keyboard I brought with me from the UK and my logged on server is in Paris. There doesn't seem to be a standard answer to my problem. Hopefully the problem will resolve itself in about 4 weeks time when I shall be returning to the UK. I'm not sure of the position of the '?' is on a French keyboard I might have a look later. Cheers for the reply. Quote Link to comment https://forums.phpfreaks.com/topic/288706-_get-inserting-at-the-url-not-working/#findComment-1480700 Share on other sites More sharing options...
kicken Posted May 24, 2014 Share Posted May 24, 2014 Make sure you enter the full URL, including the http://. I've seen some browsers in the past that do not recognize 'localhost' as a domain name and instead treat it as a search term. Adding the http:// resolves the issue and the browser treats it as a URL rather than search query. Quote Link to comment https://forums.phpfreaks.com/topic/288706-_get-inserting-at-the-url-not-working/#findComment-1480725 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.