inserting into database problem?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

inserting into database problem?

Post by saumya »

hi guys,
I am trying to insert some data in to my data base but nothing happens.Any idea?The code goes like this

Code: Select all

<?php
//getting the form values
 $item=$_GET['item'];
 $tDate=$_GET['tDate'];
 $price=$_GET['price'];
 echo($item.' : '.$price.' : date='.$tDate);
 //.....................Putting them in database......................................
$link=mysql_connect('localhost','root','') or die('Could not connect: '.mysql_error());
echo('<br>Database Server is connected successfully');
mysql_select_db('monthlyexpense') or die('Could not select database:'.mysql_error());
echo('<br>database is selected');
//performing sql querry
  $query="INSERT INTO 'daily'('dt','item','price') VALUES('$tDate','$item','$price');";
  mysql_close($link);
?>
actually i have in my data bse another column which is auto incremented.Do i need to pass any value to that too?or the code is fine?Please guide me in this.
thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: inserting into database problem?

Post by feyd »

try

Code: Select all

<?php
//getting the form values
 $item=$_GET['item'];
 $tDate=$_GET['tDate'];
 $price=$_GET['price'];
 echo($item.' : '.$price.' : date='.$tDate);
 //.....................Putting them in database......................................
$link=mysql_connect('localhost','root','') or die('Could not connect: '.mysql_error());
echo('<br>Database Server is connected successfully');
mysql_select_db('monthlyexpense') or die('Could not select database:'.mysql_error());
echo('<br>database is selected');
//performing sql querry
  $query="INSERT INTO `daily` (`dt`,`item`,`price`) VALUES('$tDate','$item','$price');";
  mysql_close($link);
?>

simply changed your single quotes around table names and field names to backticks.

Warning: you have SQL injection possible with your usage of $_GET variables dumping directly into SQL. Read the security forum for details of how to fix up your code.
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

thank you very much.
but that too does not work.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it should, if you performed a query... mysql_query()
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

thank you verymuch.Actually i forgot to add the query part.

Code: Select all

<?php
//getting the form values
$item=$_GET['item'];
$tDate=$_GET['tDate'];
$price=$_GET['price'];
echo($item.' : '.$price.' : date='.$tDate);
//.....................Putting them in database......................................
$link=mysql_connect('localhost','root','') or die('Could not connect: '.mysql_error());
echo('<br>Database Server is connected successfully');
mysql_select_db('monthlyexpense') or die('Could not select database:'.mysql_error());
echo('<br>database is selected');
//performing sql querry
  $query="INSERT INTO `daily` (`dt`,`item`,`price`) VALUES('$tDate','$item','$price');";
//........................................added this...........................................................
  mysql_query($query);
///................................................................................................................
  echo('<br>Data is inserted into table, successfully');
  mysql_close($link);
?>
thank you verymuch
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

well after all this has happened, i want to take the user back to the form page.Is it possible?I mean after the data is successfully inserted into the database the user should be taken bak to the main form page.Any idea how to do this?Well, this time all the varibales have to be cleared too.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

if you mean like a redirection code here is one

<html>
<meta
http-equiv="refresh" content="0; url=your url address.html">
</html>

just add that at the end of ?> and save it in the same format as it was.

If you are looking for somthing else then I dont know.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there have been many many threads on redirection, a meta-refresh (as nickman013) has said is one. Using header() is another. (they can be combined)
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

thank you so much.that worked.But is this method of redirection happens after the data has been put inside the database or it just redirects when the page loads.I mean do i need to check that all the dats are put inside the table and then redirect it or it just does that.
thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

test it, you'll get your answer pretty quick..
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

i have checked it and puts the data and then re-directs.
But i got confused as i thought it may or maynot put the data inside and then redirects.So to confirm i asked you masters.
Post Reply