Jump to content

GET method

- - - - -

  • Please log in to reply
15 replies to this topic

#1
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
So, I know a little php and MySQL but obviously not enough to fix this problem I'm having. I've a calendar where I can add/view event(s) and it works great except the date part.

When I clicked on a day in the calendar, I open popup window and set day, month and year to it's URL. I then read those info in popup info, along with the content and insert that into table.

Here's the (relevant) code:

<form action="add.php" method="post">

	Title: <input type="text" name="title" /><br />

	Content: <textarea name="content" /></textarea><br />

	<input type="submit" name="submit" value="Add" />

</form>

<?php

	

if (isset($_POST['submit'])) {

		

        $date = $_GET['y'] . "-" . $_GET['m'] . "-" . $_GET['d'];


	if ($_POST['content'] == null || $_POST['content'] == "" || $_POST['content'] == " ") {

		?>

		<font style='color: #FF0000; font-size: 12px;'>Empty fields are not allowed.</font><br />

		<a href='javascript: self.close(); '>Close this window!</a>

		<?php

		exit;

	}


	$title = mysql_real_escape_string($_POST['title']);

	$content = mysql_real_escape_string($_POST['content']);

	$uid = 1;

		

	mysql_query("INSERT INTO event(uid, title, content, date) VALUES ($uid, '$title', '$content', '$date') ") or 

            die("<b>Error while inserting:</b> " . mysql_error() . "<br /><a href='javascript: self.close(); '>Close this window!</a>");

}

?>

After I insert I get 0000-00-00 in date field.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Try to echo out the query before you publish it to MySQL, that can likely show you a problem with the data that is being interpreted. Is your date field format of type 'date' and not 'datetime'?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Dreamcatcher

Dreamcatcher

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
Maybe you have already seen that but just in case ;)
MySQL :: MySQL 5.1 Reference Manual :: 10.3.1 The DATETIME, DATE, and TIMESTAMP Types
"UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity." -- Dennis Ritchie

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
My date field is of type 'date'. This will sound stupid, but could I print query? I tried this:

$qwe = mysql_query(...);

echo $qwe;

And nothing was printed. I also tried mysql_error() and nothing showed. And if I print $date variable, it only shows dashes.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
Dreamcatcher

Dreamcatcher

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
If I get the point of Alexander he meant to echo the query (Insert Into ... ) before you pass it through the mysql_query. So you could see the values of the parameters and the final construct of the query just before it's execution...
"UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity." -- Dennis Ritchie

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

INSERT INTO event(uid, title, content, date) VALUES (1, 'title field', 'content field', '--')

This is what I get.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
Dreamcatcher

Dreamcatcher

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
That's why you get the initial date I think. Because the date that you pass is in a non-appropriate form....

Try to configure your code to give in a form as :
The DATE type is used when you need only a date value, without a time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31' Copied by MySQL :: MySQL 5.1 Reference Manual :: 10.3.1 The DATETIME, DATE, and TIMESTAMP Types
"UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity." -- Dennis Ritchie

#8
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I pass the date (day, month and year) to popup window via URL and when I insert into table, I read those values via URL, except that instead of actual values I only get dashes.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#9
gon1387

gon1387

    Newbie

  • Members
  • PipPip
  • 17 posts
Check what values this returns
$date = $_GET['y'] . "-" . $_GET['m'] . "-" . $_GET['d'];
, echo it, so you'll know if the problem was either on the GET variable name or the value being passed from the browser.

#10
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
print_r($_GET); can be useful for debugging actually to save time typing key names, there clearly is one point where the $_GET parameter is not accessible, try writing out the steps to how the form passes the parameters to the main script.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#11
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
After executing the following code

echo "print_r(): <br />";

print_r($_GET);

this is what I get:

print_r(): 

Array ( ) 


So, I have index.php where the calendar is located. To each valid day I add onclick event to anchor tag, which calls the following function:

function add(day, month, year) {

	window.open("add.php?d="+day+"&m="+month+"&y="+year, "",

				"menubar=no, width=400, height=400, toolbar=no, screenX=400, screenY=400");

}

Then, as said, I read date from URL with $_GET.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#12
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
apparently, your variables isn't sent through to the url. Try access the url manually with dates added properly?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users