Pawan_Agarwal Posted June 23, 2013 Share Posted June 23, 2013 I can connect to database but when I try to insert row using submit button in PHP, it does run without any error and does nothing, pls help me out....... if($_POST[query]!="") { $n1=$_POST['fname']; $n2=$_POST['number']; $n3=$_POST['email'].$_POST['domain1'].$_POST['domain2']; $n4=$_POST['category']; $n5=$_POST['query']; $year=Date("Y"); $month=Date("m"); $day=Date("d"); $now=$year."-".$month."-".$day; $sql=("INSERT INTO table_name (Name,Number,Email,Category,Query,Date) VALUES('$n1','$n2','$n3','$n4','$n5','$now')") or die(mysql_error()); if (!mysql_query($sql,$connect)) { die('Error: ' . mysql_error($connect)); }else{echo $n1." ".$n2." ".$n3." ".$n4." ".$n5." ".$now;} echo "1 record added"; } Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted June 23, 2013 Share Posted June 23, 2013 how do you know your code is running? what output do you get? Quote Link to comment Share on other sites More sharing options...
Pawan_Agarwal Posted June 23, 2013 Author Share Posted June 23, 2013 I am trying to insert the data from text box in my form to database, the whole code is running without any error. The row cannot be inserted after execution of $sql=("INSERT INTO table_name (Name,Number,Email,Category,Query,Date) VALUES('$n1','$n2','$n3','$n4','$n5','$now')") or die(mysql_error()); if (!mysql_query($sql)) { die('Error: ' . mysql_error($connect)); } echo "1 record added"; } Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted June 23, 2013 Share Posted June 23, 2013 i know what you are trying to do. you need to tell us what you saw in front of you because that helps to pin down what is causing the problem. how do you know that the whole code is running? Quote Link to comment Share on other sites More sharing options...
kicken Posted June 23, 2013 Share Posted June 23, 2013 Remove the or die() stuff after your query string. That is something you would use on the line where you run the query using mysql_query, not when you define the query text. Since you are checking mysql_query with an if statement though, you do not need it at all. Quote Link to comment Share on other sites More sharing options...
Pawan_Agarwal Posted June 23, 2013 Author Share Posted June 23, 2013 <?php date_default_timezone_set('Asia/Kolkata'); $username = "**********"; $password = "**********"; $hostname = "**********"; $database = "**********"; $connect = mysql_connect($hostname, $username, $password, $database) or die("Could not connect: ".mysql_error()); if ($connect) { echo ("Connection is success <br>");////it executes } else { echo ("Connection is failed"); } $selected = mysql_select_db($database,$connect) or die("Could not select database"); if ($selected) { echo ("Connection is success to database<br><br>");////it executes } else { echo ("Connection is failed"); } if($_POST['query']!="") { $n1=$_POST['fname']; $n2=$_POST['number']; $n3=$_POST['email'].$_POST['domain1'].$_POST['domain2']; $n4=$_POST['category']; $n5=$_POST['query']; $year=Date("Y"); $month=Date("m"); $day=Date("d"); $now=$year."-".$month."-".$day; echo $n1." ".$n2." ".$n3." ".$n4." ".$n5." ".$now; ///this is not working $sql=("INSERT INTO table_name (Name,Number,Email,Category,Query,Date) VALUES('$n1','$n2','$n3','$n4','$n5','$now')") ///this is not working if (!mysql_query($sql)) { die('Error: ' . mysql_error($connect)); } echo "1 record added"; } mysql_close($connect); ?> I am trying this code and it is executing, but no row inserted after clicking on submit button...........thanks for helping me out...... Quote Link to comment Share on other sites More sharing options...
Barand Posted June 23, 2013 Share Posted June 23, 2013 Turn PHP error reporting on. You should be getting a syntax error with that code Quote Link to comment Share on other sites More sharing options...
Pawan_Agarwal Posted June 23, 2013 Author Share Posted June 23, 2013 Thanks, I enabled the reporting error function......... The syntax error it is showing is here in the mentioned line........ if(!$_POST['query']=="") what might be done to correct this ?? Quote Link to comment Share on other sites More sharing options...
Barand Posted June 23, 2013 Share Posted June 23, 2013 you have no ; at the end of the statement on previous line Quote Link to comment Share on other sites More sharing options...
Pawan_Agarwal Posted June 23, 2013 Author Share Posted June 23, 2013 Undefined index: query in C:\xampp\htdocs\enquiry.php on line if($_POST['query']!=NULL) Quote Link to comment Share on other sites More sharing options...
Pawan_Agarwal Posted June 23, 2013 Author Share Posted June 23, 2013 now there is no error, I am applying if(!empty($_POST['query'])) and this sorts error of syntax, however, I am still not able to insert data into database.......... Quote Link to comment Share on other sites More sharing options...
Pawan_Agarwal Posted June 23, 2013 Author Share Posted June 23, 2013 CODE: $n1=$_POST['fname']; $n2=$_POST['number']; $n3=$_POST['email'].$_POST['domain1'].$_POST['domain2']; Error: Notice: Undefined index: fname in C:\xampp\htdocs\enquiry.php Notice: Undefined index: number in C:\xampp\htdocs\enquiry.php Notice: Undefined index: email in C:\xampp\htdocs\enquiry.php Notice: Undefined index: domain1 in C:\xampp\htdocs\enquiry.php Notice: Undefined index: domain2 in C:\xampp\htdocs\enquiry.php I am facing this error, what can be done here ??? Quote Link to comment Share on other sites More sharing options...
Csharp Posted June 23, 2013 Share Posted June 23, 2013 Are you sure the form you're using is sending that data like this? Would you mind pasting a copy of the HTML used for the form? Quote Link to comment Share on other sites More sharing options...
Pawan_Agarwal Posted June 23, 2013 Author Share Posted June 23, 2013 <form action="enquiry.php" name="myform" method='post'> Name <input type="text" size="65" name="fname"> Cell Number <input type="text" size="65" name="number"> Email Address<input type="text" size="32" name="email"> <select style="align:center" name="domain1" size="1" style="width:50px"> <option>@gmail</option> <option>@yahoo</option> <option>@hotmail</option> <option>@aol</option> <option>@inbox</option> <option>@fastmail</option> <option>@mail</option> <option>@lycos</option> <option>@care2</option> <option>@zenbe</option> <option>@zoho</option> <option>@aim</option> <option>@icloud</option> </select> <select align="center" name="domain2" size="1" style="width:80px"> <option>.in</option> <option>.com</option> <option>.co.in</option> </select> Should I write $_REQUEST or $_POST??? CODE: $n1=$_POST['fname']; $n2=$_POST['number']; $n3=$_POST['email'].$_POST['domain1'].$_POST['domain2']; Quote Link to comment Share on other sites More sharing options...
Solution Pawan_Agarwal Posted June 24, 2013 Author Solution Share Posted June 24, 2013 some how, I solved it................now I am coding isset() instead of if(!$_POST['query']=="") Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.