我正在通过php html和ajax创建一个表单,该表单特定于数据库表的每一行。我将表单数据通过ajax发送到另一个页面,然后该页面接收该表单数据并使用它根据给定的结果从另一个数据库中提取数据并显示它们。
我很确定问题是我在recipedisplay.php页面上的select语句,或者我的语法错误,如何回显返回的变量。
select.php
<?php <script>
$('.button').click(function (e){
e.preventDefault();
var id = $(this).attr('id');
$.ajax({
type: 'POST',
url: 'pages/recipes/recipedisplay.php',
data: $('#f'+id).serialize(),
success: function(d){
$('#infodisplay').html(d);
}
});
});
</script>
<div id=\"a".$row['id']."\">
<form id=\"f" . $row['id'] . "\">
<input type=\"hidden\" name=\"recipeid\" id=\"recipeid\" value=\"" . $row['id'] . "\">
<div id=\"reciperesultbutton\" class=\"button\"><div id=\"centering\">" . $row['name'] ." </div></div>
<div id=\"reciperesulttext\"> " . $row['id'] ." " . $row['longdesc'] ."</div>
</form>
<br>
</div>
";
}
?>
recipedisplay.php
<?php
$con=mysqli_connect("localhost","test","test","test");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$id = mysqli_real_escape_string($con, $_POST['recipeid']);
$sql= "SELECT * FROM recipes WHERE 'id' ='".$id."'";
$row = mysqli_fetch_array($sql);
$name = $row['name'];
$longdesc = $row['longdesc'];
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
echo " fail ";
echo " . $name . ";
};
echo " . $id . ";
echo " work ";
echo " . $longdesc . ";
echo "$row[name]";
mysqli_close($con);
?>
答案 0 :(得分:1)
问题在于:
$row = mysqli_fetch_array($sql);
因为mysqli_fetch_array()接受mysqli_query()结果而不是你的 $ sql 查询
因此,请尝试使用以下代码运行查询:
mysqli_query($con,$sql);
$row = mysqli_fetch_array($mysqli_query);
你也可以使用mysqli_fetch_assoc()将mysqli_query()作为参数