gpt4 book ai didi

Php:在执行插入查询时检索一个表的主键并存储到另一个表中。

转载 作者:行者123 更新时间:2023-11-29 02:51:29 25 4
gpt4 key购买 nike

我有这两个表“fcategory”和“fthreads”。fcategory 字段:category_id、category_name。fthreads 字段:thread_id、thread_title、category_name、category_id、user_id。

当我在 php 中创建一个新线程时,我希望 category_id 与 category_name 一起获取并将其插入到 fthreads 表中。

这是我的 php 文件:threads.php

<form action="threadsp.php" name="myform" method="post">


<label for="field4"><span>Category</span>
<?php
$query = "select * from fcategory";
$result = mysqli_query($conn, $query);
$resultsearch = mysqli_fetch_array($result);


if(!$result){
die('could not get data:'.mysqli_error($conn));
}
echo '<select name="category" class="select-field">';

while ($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['category_name'].'">'.$row['category_name'].'</option>';
}

echo "</select>";
echo "</label>";

?>
<label for="field1"><span>Thread Title <span class="required">*</span></span>
<input type="text" class="input-field" name="title" value="" />
</label>

<label><span>&nbsp;</span>
<input type="submit" value="Create" />
</label>
</form>

第二个文件:threadsp.php

 <?php

$catg = $_POST['category'];
$title = $_POST['title'];
$userid = $_SESSION['uid'];


$sql = "select category_id from fcategory where category_name = '$catg'";
$result2 = mysqli_query($conn, $sql);
$catgidresult = mysqli_fetch_array($result2);

$query = "insert into fthreads (category_name,thread_title,user_id,category_id) values('".$catg."','".$title."','".$userid."','".$catgidresult."')";
$result = mysqli_query($conn, $query);

if(!$result){
echo "failed".mysqli_error($conn);
}else{
header("Location: question.php");
die();
}


?>

我能够获取 category_name 但 category_id 的值显示为 0。任何帮助将不胜感激。谢谢

最佳答案

为什么要将 category_name 存储在 fthreads 表 中,如果该表中有 category_id ?只需从您的 fcategory 表 中删除 category_name 即可。您需要规范化您的表格。参见 this :所以,只需从您的表单发送 category_id ,如下所示:

<form action="threadsp.php" name="myform" method="post">


<label for="field4"><span>Category</span>
<?php
$query = "select * from fcategory";
$result = mysqli_query($conn, $query);
$resultsearch = mysqli_fetch_array($result);


if(!$result){
die('could not get data:'.mysqli_error($conn));
}
echo '<select name="cat_id" class="select-field">';

while ($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['category_id'].'">'.$row['category_name'].'</option>';
}

echo "</select>";
echo "</label>";

?>
<label for="field1"><span>Thread Title <span class="required">*</span></span>
<input type="text" class="input-field" name="title" value="" />
</label>

<label><span>&nbsp;</span>
<input type="submit" value="Create" />
</label>
</form>

在你的 threads.php 文件中你可以这样做:

<?php

$cat_id = $_POST['cat_id'];
$title = $_POST['title'];
$userid = $_SESSION['uid'];




$query = "insert into fthreads (thread_title,user_id,cat_id) values('".$title."','".$userid."','".$cat_id."')";
$result = mysqli_query($conn, $query);

if(!$result){
echo "failed".mysqli_error($conn);
}else{
header("Location: question.php");
die();
}


?>

此外,您需要清理您的输入,否则您很容易受到 Sql 注入(inject)攻击。参见 here是清理输入的最佳方法。快乐编码 :) .

关于Php:在执行插入查询时检索一个表的主键并存储到另一个表中。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35217698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com