如何通过简单文本上传到我的SQL数据库。一个PHP表单?
我的表单如下:<form action="suggestions.php" method="get">
Title: <input type="text" name="title"><br>
Suggestion: <input type="text" name="suggestion"><br>
<input type="submit" value="Submit">
</form>
我一直在网上搜索,但我只能找到如何上传图片,这不是我想要的。我只想上传2个文本区域 - 标题和建议。我该怎么做?
我的表名是建议:)
答案 0 :(得分:1)
您必须设置数据库 - 例如MySQL。然后创建一个表。您可以将其命名为suggestions
,其中包含3列 - id
,title
,suggestion
。然后,您应该在suggestions.php
文件中有一些代码,负责处理提交表单。要向数据库插入值,您应该清理用户输入并设置数据库连接(MySqli)或PDO)。例如:
<?php
if(!empty($_GET['title']) && !empty($_GET['suggestion'])) { // Check if variables are set.
$db = new mysqli('localhost', 'user', 'pass', 'yourTable'); // Connect to your database and select table, that you have created.
if($db->connect_errno > 0){ // Check if any errors occured.
die('Unable to connect to database [' . $db->connect_error . ']');
}
if ($stmt = $db->prepare('INSERT INTO `suggestions`(title, suggestion) VALUES (?, ?);')) { // Prepare SQL statement.
$stmt->bind_param('ss', $_GET['title'], $_GET['suggestion']); // Bind values from form.
$stmt->execute(); // Execute statement.
$stmt->close();
}
}
?>
答案 1 :(得分:0)
这应该有效:
<?php
@$db=mysql_connect("server", "username", "password");
@mysql_select_db("database", $db);
if(isset($_GET)){
$sql="Insert into suggestions SET title='".$_GET['title']."' , suggestion='".$_GET['suggestion']."'";
mysql_query($sql)or die(mysql_error());
}
?>