By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,086 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,086 IT Pros & Developers. It's quick & easy.

Form Sends Data But Also Shows Errors?

P: 27
i have a html form it and some php script to post it to database but every time i open page it show some error on the top but when i input data and send it it also send so how can i remove these errors?

Errors:

Notice: Undefined index: fromcombo in C:\wamp\www\site\booking.php on line 8

Notice: Undefined index: tocombo in C:\wamp\www\site\booking.php on line 8

Notice: Undefined index: DV in C:\wamp\www\site\booking.php on line 8

php script:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. mysql_connect("localhost", "root", "password");
  3.  
  4. mysql_select_db("online_bus_project");
  5.  
  6. mysql_query("INSERT INTO trip (gender, country, date) VALUES ('$_POST[fromcombo]', '$_POST[tocombo]', '$_POST[DV]')");
  7.  
  8. ?>

Html Form:

Expand|Select|Wrap|Line Numbers
  1. <form name="tstest" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
  2.    <CENTER>From : <select name="fromcombo">
  3. <option value="khairpur">Khairpur</option>
  4. <option value="sukkur">Sukkur</option>
  5. </select>       
  6.  
  7. To : <select name="tocombo">
  8. <option value="karachi">Karachi</option>
  9. <option value="hyderabad">Hyderabad</option>
  10. </select></CENTER><br /><br><br>
  11.  
  12. <CENTER>Date : <script type="text/javascript" language="javascript">
  13. (INPUT(NAME("DV")+READONLY())+INPUT(TYPE("button")+VALUE("Calender")+ONCLICK("popCal(this.form.DV)"))).FORM().write();
  14. </script></CENTER><br><br><br>
  15.  
  16. <center><input type="submit" value="Submit Info" /></CENTER>
  17.  
  18. </form>
May 9 '09 #1
Share this Question
Share on Google+
6 Replies


prabirchoudhury
100+
P: 162
This error appears because of your PHP error reporting settings. Usually,
it appears when your variable is not properly set. every time you getinto the page its try to insert the data into table
but your post variables are empry

Check if $_POST['action'] is set before insert into dataabse. if the form is not being submited then $_POST['action'] is not being set, so it would not try to insert into table.

PHP script
Expand|Select|Wrap|Line Numbers
  1. if (isset($_POST['action'])){
  2.  
  3. mysql_connect("localhost", "root", "password");
  4.  
  5. mysql_select_db("online_bus_project");
  6.  
  7. mysql_query("INSERT INTO trip (gender, country, date) VALUES ('$_POST[fromcombo]', '$_POST[tocombo]', '$_POST[DV]')");
  8.  
  9. }

Html Form

Expand|Select|Wrap|Line Numbers
  1. <form name="tstest" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
  2. <CENTER>From : <select name="fromcombo">
  3. <option value="khairpur">Khairpur</option>
  4. <option value="sukkur">Sukkur</option>
  5. </select> 
  6.  
  7. To : <select name="tocombo">
  8. <option value="karachi">Karachi</option>
  9. <option value="hyderabad">Hyderabad</option>
  10. </select></CENTER><br /><br><br>
  11.  
  12. <CENTER>Date : <script type="text/javascript" language="javascript">
  13. (INPUT(NAME("DV")+READONLY())+INPUT(TYPE("button") +VALUE("Calender")+ONCLICK("popCal(this.form.DV)") )).FORM().write();
  14. </script></CENTER><br><br><br>
  15.  
  16. // add this hidden field here as an action flag
  17. <input type="hidden" name="action" value="add">
  18. <center><input type="submit" value="Submit Info" /></CENTER>
  19.  
  20. </form> 
May 9 '09 #2

Markus
Expert 5K+
P: 6,050
On a further note: you're also inserting raw, potentially malicious data into your database. Have a look at SQL Injection, and your array indices should be wrapped with single or double quotes.

Expand|Select|Wrap|Line Numbers
  1. // Wrong
  2. $_POST[key_name];
  3. // Correct
  4. $_POST['key_name'];
  5.  
May 10 '09 #3

P: 27
marcus after adding this it shows this error
// Correct
$_POST['key_name'];
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\wamp\www\site\booking.php on line 8
May 10 '09 #4

P: 27
after addning action isset its working fine thanx for the help prabirchoudhury's & Marcus.



close the tread
May 10 '09 #5

Markus
Expert 5K+
P: 6,050
@obtrs
Of course. I was only showing how you should use quotes when accessing array elements.
May 10 '09 #6

P: 27
thanx man...........
May 10 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.