Page 1 of 1

inserting into database problem?

Posted: Wed Aug 17, 2005 12:53 am
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

Re: inserting into database problem?

Posted: Wed Aug 17, 2005 1:02 am
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.

Posted: Wed Aug 17, 2005 1:13 am
by saumya
thank you very much.
but that too does not work.

Posted: Wed Aug 17, 2005 1:28 am
by feyd
it should, if you performed a query... mysql_query()

Posted: Wed Aug 17, 2005 1:33 am
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

Posted: Wed Aug 17, 2005 1:36 am
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.

Posted: Wed Aug 17, 2005 1:43 am
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.

Posted: Wed Aug 17, 2005 1:45 am
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)

Posted: Wed Aug 17, 2005 2:01 am
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

Posted: Wed Aug 17, 2005 2:18 am
by feyd
test it, you'll get your answer pretty quick..

Posted: Wed Aug 17, 2005 2:21 am
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.