Jump to content

$_GET inserting at the url not working


kev

Recommended Posts

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 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;
    }
}
?>

 

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
 

Link to comment
Share on other sites

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 by bsmither
Link to comment
Share on other sites

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.

Link to comment
Share on other sites


//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 by joel24
Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

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));

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

"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 by bsmither
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.