mysql准备好的Select语句连接不起作用

所以我准备好的声明有效,但我不知道如何告诉 php 加入两列。


我尝试了一个没有准备的声明并且有效。我尝试向 bind_results 添加更多变量。


$stmt = mysqli_prepare($con, "Select tblcategory.CategoryName as catname,tblcategory.id as catid, tblwritter.Writter as writtername, tblwritter.writterdescription as writterdescription, tblwritter.PostingDate as writterpostingdate, tblwritter.UpdationDate as writterupdationdate, tblwritter.WritterId as writterid from  tblwritter join tblcategory on  tblwritter.CategoryId=tblcategory.id where  tblwritter.Is_Active=1 and  WritterId=?");

$stmt->bind_param("i", $writterid);

$writterid=intval($_GET['scid']);

$stmt->execute();

$stmt->bind_result($catname,$catid,$writtername,$writterdescription,$writterpostingdate,$writterupdationdate,$writterid);

$stmt->fetch();


                                                <div class="form-group">

                                                    <label class="col-md-2 control-label">Category</label>

                                                    <div class="col-md-10">

                                                      <select class="form-control" name="category" required>

                                                   <option value="<?php echo $catid;?>"><?php echo $catname;?></option>

<?php

$stmt = $con -> prepare('select id,CategoryName from  tblcategory where Is_Active=?');

$stmt -> bind_param('i', $Is_Active);

$Is_Active = 1;

$stmt -> execute();

$stmt -> store_result();

$stmt -> bind_result($id, $CategoryName);

$stmt -> fetch(); 

// Feching active categories

$ret=mysqli_query($con,"select id,CategoryName from  tblcategory where Is_Active=1");

while($result=mysqli_fetch_array($ret))

{    

?>

<option value="<?php echo htmlentities($result['id']);?>"><?php echo htmlentities($result['CategoryName']);?></option>

<?php } ?>


                                                        </select> 

                                                    </div>

                                                </div>

预期的结果是类别应该下拉以编辑类别。


鸿蒙传说
浏览 169回答 1
1回答

手掌心

您的问题不清楚,因为您未能隔离您的代码究竟在哪里失败,并且没有告诉我们您收到了什么错误。(直到改变,我不能赞成你的问题)不过,您的问题很清楚预期的效果。我编写了一个未经测试的脚本,我认为它应该可以按要求工作。<?php$config = ['localhost', 'root', '', 'dbname'];if (!$con = new mysqli(...$config)) {&nbsp; &nbsp; die("MySQL Connection Error: <b>Check config values</b>");&nbsp; // $con->connect_error}$query = "SELECT cat.CategoryName, cat.id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;writ.Writter, writ.writterdescription,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;writ.PostingDate, writ.UpdationDate, writ.WritterId&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FROM tblwritter AS writ&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOIN tblcategory AS cat ON writ.CategoryId = cat.id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHERE writ.Is_Active = 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AND writ.WritterId = ?"if (!$stmt = $con->prepare($query)) {&nbsp; &nbsp; echo "Prepare Error: ";&nbsp; // $con->error} elseif (!$stmt->bind_param("i", $_GET['scid']) ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; !$stmt->execute() ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; !$stmt->bind_result($catname,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $catid,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $writtername,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $writterdescription,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $writterpostingdate,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $writterupdationdate,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $writterid)) {&nbsp; &nbsp; echo "Statement Error: "; // $stmt->error} elseif (!$stmt->fetch()) {&nbsp; &nbsp; echo "[No qualifying row found]";}$stmt->close();$catid = $catid ?? 0;$query = "SELECT id, CategoryName&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FROM tblcategory&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHERE Is_Active = 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AND id != ?";if (!$stmt = $con->prepare($query)) {&nbsp; &nbsp; echo "Prepare Error: "; // $con->error} elseif (!$stmt->bind_param("i", $catid) ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; !$stmt->execute() ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; !$stmt->bind_result($id, $name)) {&nbsp; &nbsp; echo "Statement Error: "; // $stmt->error} else { ?>&nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; <label class="col-md-2 control-label">Category</label>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-md-10">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select class="form-control" name="category" required>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($catid) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<option value=' , $catid , '>' , htmlentities($catname) , '</option>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ($stmt->fetch()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<option value=' , $id , '>' , htmlentities($name) , '</option>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </select>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div> <?php}维护面向对象的 mysqli 语法而不是混合语法。OO 是我的推荐,因为它比程序更简洁。我已经标记了我的代码段,使其全部适合 Stack Overflow 代码段框,而无需水平滚动。在查询中写入 MySQL 关键字时使用全部大写 - 这将提高可读性。使用短表别名(如“cat”和“writ”,这样您的查询就不会因字符而变得臃肿我在流程中包含了一堆检查点,以帮助您诊断可能出现的任何问题。永远不要向最终用户显示原始错误消息——出于安全考虑。我没有检查是否$_GET['scid']存在,我假设您在其他地方这样做。如果由于某种原因提交的 scid 值不返回任何符合条件的行,则将其默认为零。这将允许下一个查询一致地工作。我更改了您的第二个查询,以便它不会在选择字段中再次提供 scid 值。如果在数据库中找到 $catid,它将在选择字段中显示为第一个选项。只要数据库列是 INT 数据类型(它应该是),就value不需要在选项标签中作为属性写入的整数值htmlentities()。我还删除了整数上的双引号,因为它们不是必需的。
打开App,查看更多内容
随时随地看视频慕课网APP