我是stackoverflow的新手。我想创建这个表单,将信息上传到数据库。当我点击提交时,它不会上传。我检查了我的连接文件,这是正确的。代码如下。请帮忙。
<?php
include("/connections/db_conx.php");
if ( isset($_POST['submit']) ) {
$title = mysqli_real_escape_string($_POST['title']);
$text = mysqli_real_escape_string($_POST['text']);
$picture = mysqli_real_escape_string($_POST['picture']);
$sql = "INSERT INTO news (title, text, picture) VALUES('$title','$text','$picture', now(),now(),now())";
$query = mysqli_query ($db_conx, $sql);
echo 'Entered into the news table';
}
?>
<html>
<head>
</head>
<body>
<table border="0">
<tr>
<form method="post" action="index.php" id="tech">
<td>Title</td><td> <input type="text" name="title"></td> </tr>
<tr> <td>Text</td><td> <textarea rows="4" name="text" cols="50" name="comment" form="tech"> </textarea>
</td> </tr>
<tr> <td>Picture</td><td> <input type="varchar" name="picture"></td> </tr>
<tr> <td><input id="button" type="submit" name="submit" value="Submit"></td>
</tr>
</form>
</table>
</body>
</html>
答案 0 :(得分:1)
以下是您需要使用的代码:
<?php
include("/connections/db_conx.php");
if(isset($_POST['submit'])) {
$title = mysqli_real_escape_string($db_conx, $_POST['title']);
$text = mysqli_real_escape_string($db_conx, $_POST['text']);
$picture = mysqli_real_escape_string($db_conx, $_POST['picture']);
$sql = "INSERT INTO news (`title`, `text`, `picture`) VALUES('$title','$text','$picture');";
if(!$result = $db_conx->query($sql)){
die('There was an error running the query [' . $db_conx->error . ']');
}
echo 'Entered into the news table';
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="index.php" id="tech">
<table border="0">
<tr>
<td>Title</td>
<td> <input type="text" name="title"></td>
</tr>
<tr>
<td>Text</td>
<td><textarea rows="4" name="text" cols="50" name="comment" form="tech"> </textarea></td>
</tr>
<tr>
<td>Picture</td>
<td> <input type="varchar" name="picture"></td>
</tr>
<tr>
<td><input id="button" type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
您的问题是mysqli_real_escape_string()
函数需要2个参数:数据库连接和要转义的字符串。
我还包括完全重新格式化的代码和错误检查。
答案 1 :(得分:0)
试试这个:
$title = mysqli_real_escape_string($db_conx, $_POST['title']);
$text = mysqli_real_escape_string($db_conx, $_POST['text']);
$picture = mysqli_real_escape_string($db_conx, $_POST['picture']);
$sql = "INSERT INTO news (title, text, picture) VALUES('$title','$text','$picture')";
$query = mysqli_query ($db_conx, $sql);
if($query){
echo 'Entered into the news table'; // Your Success Message
}
else {
echo mysqli_error($db_conx);
}