0

sry for my amateur question. I wrote this code for get text from input and add in to database:

 <html xmlns="http://www.w3.org/1999/xhtml" lang="fa"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>My Title</title> </head> <body> <form action="" method="POST"> <label for="name" id="app_name_label" >name</label> <input type="text" name="name" id="name_textfield"> <input type="submit" name"Submit" value="send" > </form> <?php if(isset($_POST['Submit'])) { include_once("config.php"); $con = mysql_connect($db_host,$db_user,$db_pass) or die(mysql_error()); $selected=mysql_select_db($db_name, $con) or die(mysql_error()); if($selected){ $name = $_REQUEST['name']; $ins = "INSERT INTO infos (app_name ) VALUES ('$name')"; $saved=mysql_query($ins ); if($saved) { echo "Saved!!"; } else { echo "Don't Saved!!"; } } mysql_close($con); } ?> </body> </html> 

My database doesn't have any problem But when i run it, nothing write to my database! :( what should i do? I think problem is in isset($_POST['Submit']).

2
  • The code should be in two different scripts, first the html part is requested and displayed by the browser. Then the script noted down in the forms action attribute is called and the forms content sent there. That file (a php file) processes the form data. Commented May 24, 2014 at 16:25
  • The code does not have to be in two separate files. Commented May 24, 2014 at 16:31

2 Answers 2

1

If everything else is ok, then I am afraid you have a simple typo in your code that is causing you the problem.

 <input type="submit" name"Submit" value="send" > 

should be

 <input type="submit" name="Submit" value="send" > 

You have missed the equal to sign in your code after name. :)

On a side note, you should not use $_REQUEST['name'] but instead use $_POST['name'] when the form method is post. It is more secure. And secondly, try to use mysqli database extension instead of using mysql. mysql extension is deprecated from PHP 5.5 onward.

Sign up to request clarification or add additional context in comments.

2 Comments

the problem was just equal :(
@Ahmad yeah, sometimes this silly problems happen :)
1

For your information, better use php tags above the body and display errors with sessions, plus why would you include_once("config.php") after submition is executed but not when actual file is loaded? Here is script which does the same what you trying to achieve but handling is much better.

<?php include_once("config.php"); if (isset($_POST['Submit'])){ $err = array(); $name = $_POST['name']; if(!$name){ $err[] = 'All fields required'; } if(!count($err)){ $name = mysql_real_escape_string($name); //Make sure you escape unwanted chars $row = mysql_fetch_assoc(mysql_query("INSERT INTO infos (app_name ) VALUES ('$name')")); if(mysql_affected_rows() == 1) { $_SESSION['msg']['succ'] = 'Sent!'; header("Location: index.php"); } else{ $_SESSION['msg']['err'] = 'Noooooo!'; } } ?> <html xmlns="http://www.w3.org/1999/xhtml" lang="fa"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>My Title</title> </head> <body> <form action="" method="POST"> <?php // Lets output message if($_SESSION['msg']['err']) { echo $_SESSION['msg']['err']; unset($_SESSION['msg']['err']); } if($_SESSION['msg']['succ']) { echo $_SESSION['msg']['succ']; unset($_SESSION['msg']['succ']); } ?> <label id="app_name_label">Name</label> <input type="text" name="name" id="name_textfield"> <input type="submit" name="Submit" value="send"> </form> </body> </html> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.