pickamaterina Posted September 29, 2013 Share Posted September 29, 2013 (edited) <?php$user="root";$password="";$database="base";$host="localhost";$table="com";mysql_connect($host, $user, $password) or die("error");mysql_select_db($database) or die("error");$name = $_POST['name'];$lastname = $_POST['lastname'];$email = $_POST['email'];$coment= $_POST['coment'];$error = ''; if(empty($name) || empty($lastname) || empty($email) || empty($coment)){ $error .= 'error. ';}if(!preg_match("/^[a-zA-A]+$/i", $name)){ $error .= 'Error ';}if(!preg_match("/^[a-zA-A]+$/i", $lastname)){ $error .= 'Error ';}if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){ $error .= 'errorl. ';}if(!preg_match("/^[a-zA-z0-9.,?!]+$/i", $comment)){ $error .= 'Error ';}if($error == ''){ $result = "INSERT INTO com (name, lastname, email, coment) VALUES('$name', '$lastname', '$email','$coment')"; echo "<script type='text/javascript'>alert('Thanks.'); window.close();</script>";}else{ echo"<script type='text/javascript'>alert ('$error'); window.close(); </script>";} Errors seems to be working its just it won't insert data in table in mysql database.... Thank you so much. Edited September 29, 2013 by pickamaterina Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 29, 2013 Share Posted September 29, 2013 (edited) Because you're not doing anything with the query $result = "INSERT INTO com (name, lastname, email, coment) VALUES('$name', '$lastname', '$email','$coment')"; You have just defined a query to $result. PHP doesn't see it as a query, it just sees it as a string. What do you expect that line to do? Edited September 29, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
pickamaterina Posted September 29, 2013 Author Share Posted September 29, 2013 well I tried connecting it via if $result=".." (mysql_query($result)){ echo "<script type='text..... and it didn't work... sorry I am just a beginner so I can't think of any way how to connect it and leave that if or use it because it is kinda needed for validation? Quote Link to comment Share on other sites More sharing options...
pickamaterina Posted September 29, 2013 Author Share Posted September 29, 2013 misstyped $result=".." if(mysql_query($result)){ echo "<script type='text..... Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 29, 2013 Share Posted September 29, 2013 moving thread to the correct forum section... Please use the forum's bbcode tags (the edit form's <> button) when posting code. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 29, 2013 Share Posted September 29, 2013 Well you need to check if mysql_query is returning any errors $sql = "INSERT INTO com (name, lastname, email, coment) VALUES('$name', '$lastname', '$email','$coment')"; if(mysql_query($sql)) { echo "<script type='text/javascript'>alert('Thanks.'); window.close();</script>"; } else { echo 'Error: ' . mysql_error(); // I have commented out the line below. //echo"<script type='text/javascript'>alert ('$error'); window.close(); </script>"; } Quote Link to comment Share on other sites More sharing options...
pickamaterina Posted September 29, 2013 Author Share Posted September 29, 2013 Actually the point was to post data in form and also use this validation? Quote Link to comment Share on other sites More sharing options...
pickamaterina Posted September 29, 2013 Author Share Posted September 29, 2013 Damnit, I meant to say to post data from form into database and still use this validation? Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted September 29, 2013 Solution Share Posted September 29, 2013 When you perform queries on database you need to make sure they execute ok. During development you need to to output as much debug information as possible when something your expect to happen doesn't happen, if that makes sense. You said earlier when you use mysql_query it didn't work. So I recommended you add echo mysql_error to see if the query returns an error. You can remove the debug code when everything is working. So does your query return any errors when your run your script? Quote Link to comment Share on other sites More sharing options...
pickamaterina Posted September 29, 2013 Author Share Posted September 29, 2013 Thanks I acutally figured it out, and how stupid I was, I should have just typed $result=mysql_query("INSERT....."); its working now thanks everyone ! 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.