我试图在我的网站上创建一个页面,表单将根据在表单提交时输入数据库的数据在文件结构中创建一个新文件夹。
用户将数据输入表单>表格中的数据被输入数据库> PHP获取表中最新条目的ID,并在名为ID的文件结构中创建一个文件夹。
这是我到目前为止的代码:
<?php
include("includes.php");
if(isset($_POST['submit-button'])){
$post_title = mysqli_real_escape_string($db, $_POST['post-title']);
$post_subheading = mysqli_real_escape_string($db, $_POST['post-subheading']);
$post_main_body = mysqli_real_escape_string($db, $_POST['post-main-body']);
$stmt = mysqli_prepare($db, "INSERT INTO posts(PostTitle, PostSubHead) VALUES (?, ?)");
mysqli_stmt_bind_param($stmt, 'ss', $post_title, $post_subheading);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
$last_post_sql = "SELECT * FROM posts LIMIT 1 ORDER BY DESC";
$last_post_results = mysqli_query($db, $last_post_sql);
if($last_post_results){
while ($row = mysqli_fetch_row($last_post_results)){
$last_post_results_string = $row[0];
echo "<script>alert(".$last_post_results_string.");</script>";
echo "Worked";
}
}
mysqli_close($db);
}
?>
<form method="post" action="create-article.php">
<table style="width: 100%;">
<tr>
<td>
<label>Post Image</label>
</td>
<td>
<input type="file">
</td>
</tr>
<tr>
<td>
<label>Title</label>
</td>
<td>
<input name="post-title" type="text">
</td>
</tr>
<tr>
<td>
<label>Sub-heading</label>
</td>
<td>
<input name="post-subheading">
</td>
</tr>
<tr>
<td>
<label>Main Body</label>
</td>
<td>
<textarea name="post-main-body" style="height: 300px; width: 100%;"></textarea>
</td>
</tr>
</table>
<button type="submit" name="submit-button" style="margin: 5px;">Create Post</button>
</form>
对于格式错误的代码感到抱歉,我不确定如何使用此处的功能。
编辑:似乎是这段特定的代码拒绝在我的网页上工作,我在PHP中没有足够的经验来识别它,谢谢:)
$last_post_sql = "SELECT * FROM posts LIMIT 1 ORDER BY DESC";
$last_post_results = mysqli_query($db, $last_post_sql);
if($last_post_results){
while ($row = mysqli_fetch_row($last_post_results)){
$last_post_results_string = $row[0];
echo "<script>alert(".$last_post_results_string.");</script>";
echo "Worked";
}
}
mysqli_close($db);
}
答案 0 :(得分:1)
您可以使用
获取最后一个记录ID$ last_id = mysqli_insert_id;