- List item
I am currently trying to insert data to a mysql database from a form but it keeps adding an empty string, if anyone could take a look at my code and let me know if there is an issue it would be greatly appreciated.
the form:
<body>
<form method="post" action="input.php">
<input type="text" name="message" size="55">
<input type="submit"name="submit" value="Send">
</form>
</body>
</html>
and the php
<?php
$host="myhost";
$user="myusername";
$database="mydatabase";
$table="mytable";
$connect=mysql_connect("$host","$user","mypassword");
if($connect){
echo "connected<BR/><hr/>";
$connect1=mysql_select_db("$database");
if($connect1){
mysql_query("INSERT INTO mytable(time,msg) VALUES('1','$message')");
echo "$message<BR/><hr/>";
}
}else{
echo "problem";
}
?>
The form is submitted using POST method then you can access dates using
$_POSTChange
$messageto$_POST['message']Warning
Note: Use
mysqliorPDO. Using mysql functions are not recommended and it will deprecated soon.Also you should add validation before inserting the data to database to avoid SQL injection or hacking. You code is not safe, it’s open to attack.