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

时间:2016-02-05 06:35:15

标签: php mysql

我有这两张桌子' 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。 任何帮助将不胜感激。谢谢

3 个答案:

答案 0 :(得分:1)

如果在该表中有category_id,为什么要在category_name中存储fthreads table?只需从fcategory table中删除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 Injection的攻击。请参阅here是清理输入的最佳方法。 快乐编码:)。

答案 1 :(得分:0)

尝试更改(在threads.php内部):

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

进入这个:

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

删除(在threadsp.php内部):

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

并更改(在threadsp.php内):

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

进入这个:

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

解释:
您不必执行第二次查询来获取category_id,而是将category_id放在value属性下的option标记内。基本上,option标签将返回value属性的内容,因此将category_id放在那里

答案 2 :(得分:0)

在你的threadsp.php文件中,你在$ catgidresult变量中获取数组,基本上你需要在$ catgidresult中传递category_id,所以你在threadsp.php文件中的插入查询应该如下:

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

注意 - $ catgidresult ['category_id']

我建议不要像你那样生成SQL,而是建议使用bind_param。